Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #339 (Theme: “Maximum Overdrive”)
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here:
The Weekly Challenge for the week of 2025-09-15 through 2025-09-21 is #339
The tasks for challenge #339 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 339-1: Max Diff Submitted by: Mohammad Sajid Anwar You are given an array of integers having four or more elements. Write a script to find two pairs of numbers from this list (WITH 4 DISTINCT INDEXES WITHIN THE ARRAY) so that the difference between their products is maximum. Return the max difference. With Two pairs (a, b) and (c, d), the product difference is (a * b) - (c * d). Example 1 Input: (5, 9, 3, 4, 6) Output: 42 Pair 1: (9, 6) Pair 2: (3, 4) Product Diff: (9 * 6) - (3 * 4) => 54 - 12 => 42 Example 2 Input: (1, -2, 3, -4) Output: 10 Pair 1: (1, -2) Pair 2: (3, -4) Example 3 Input: (-3, -1, -2, -4) Output: 10 Pair 1: (-1, -2) Pair 2: (-3, -4) Example 4 Input: (10, 2, 0, 5, 1) Output: 50 Pair 1: (10, 5) Pair 2: (0, 1) Example 5 Input: @ints = (7, 8, 9, 10, 10) Output: 44 Pair 1: (10, 10) Pair 2: (7, 8)
To solve this problem, I generate all possible "pairs of pairs" of numbers from the array such that all four numbers have unique indexes. I compute the difference of the products of the pairs, and keep track of the maximum difference seen, then return the maximum.

Robbie Hatley's Perl Solution to The Weekly Challenge 339-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 339-2: Peak Point Submitted by: Mohammad Sajid Anwar You are given an array of altitude changes. Write a script to find the maximum altitude attained, assuming that one starts at altitude 0. Example 1 Input: [-5, 1, 5, -9, 2] Output: 1 Example 2 Input: [10, 10, 10, -25] Output: 30 Example 3 Input: [3, -4, 2, 5, -6, 1] Output: 6 Example 4 Input: [-1, -2, -3, -4] Output: 0 Example 5 Input: [-10, 15, 5] Output: 10
To solve this problem, I keep track of "current" and and "max" altitudes reached after each altitude change, then return max.

Robbie Hatley's Perl Solution to The Weekly Challenge 339-2
That's it for challenge 339; see you on challenge 340!
Comments
Post a Comment