Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #294 ("Consecutive Sequence" and "Next Permutation")
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here: The Weekly Challenge The Weekly Challenge for the week of 2024-11-03 through 2024-11-09 is #294. The tasks for challenge #294 are as follows: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 294-1: Consecutive Sequence Submitted by: Mohammad Sajid Anwar You are given an unsorted array of integers, @ints. Write a script to return the length of the longest consecutive elements sequence. Return -1 if none found. The algorithm must run in O(n) time. Example 1: Input: @ints = (10, 4, 20, 1, 3, 2) Output: 4 The longest consecutive sequence (1, 2, 3, 4). The length of the sequence is 4. Example 2: Input: @ints = (0, 6, 1, 8, 5, 2, 4, 3, 0, 7) Output: 9 Example 3: Input: @ints = (10, 30, 20) Output: -1 I could first sort the array, but that would be O(n*log(n)), not O(n). Faster will be to make a hash and