Given an array nums of integers, return the length of the longest arithmetic subsequence in nums.
Note that:
seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1).Example 1:
Input: nums = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3.
Example 2:
Input: nums = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10].
Example 3:
Input: nums = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].
Constraints:
2 <= nums.length <= 10000 <= nums[i] <= 500When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a row of numbers and want to find the longest sequence where the difference between every consecutive number is the exact same. The brute force approach involves trying every single possible combination of starting points and patterns to see how long you can make them go.
Here's how the algorithm would work step-by-step:
def longest_arith_seq_length(numbers_array):
if len(numbers_array) <= 2:
return len(numbers_array)
maximum_length_found = 2
# Test every possible starting pair to establish a baseline difference for the arithmetic sequence
for first_index in range(len(numbers_array)):
for second_index in range(first_index + 1, len(numbers_array)):
current_difference = numbers_array[second_index] - numbers_array[first_index]
current_sequence_length = 2
last_number_in_chain = numbers_array[second_index]
# Scan the remaining array to extend the chain using the established difference
for scan_index in range(second_index + 1, len(numbers_array)):
if numbers_array[scan_index] - last_number_in_chain == current_difference:
current_sequence_length += 1
last_number_in_chain = numbers_array[scan_index]
# Keep track of the highest count achieved across all evaluated combinations
if current_sequence_length > maximum_length_found:
maximum_length_found = current_sequence_length
return maximum_length_found
Instead of testing every single combination of numbers, the optimal approach remembers the pattern history for every step we take. By building our understanding forward and keeping track of the exact difference between numbers, we can quickly find the longest matching sequence.
Here's how the algorithm would work step-by-step:
def longest_arithmetic_subsequence(sequence_numbers):
if len(sequence_numbers) <= 2:
return len(sequence_numbers)
maximum_sequence_length = 1
scorecard_map_list = [{} for _ in range(len(sequence_numbers))]
# We evaluate all pairs to establish base sequence lengths for every potential gap.
for current_index in range(1, len(sequence_numbers)):
for previous_index in range(current_index):
arithmetic_gap = sequence_numbers[current_index] - sequence_numbers[previous_index]
# We extend existing sequences if the exact gap was previously observed.
if arithmetic_gap in scorecard_map_list[previous_index]:
sequence_length = scorecard_map_list[previous_index][arithmetic_gap] + 1
else:
sequence_length = 2
scorecard_map_list[current_index][arithmetic_gap] = sequence_length
maximum_sequence_length = max(maximum_sequence_length, sequence_length)
# We return the global maximum to provide the length of the longest arithmetic sequence.
return maximum_sequence_length| Case | How to Handle |
|---|---|
| Empty or null array input | Return zero immediately as no arithmetic subsequence can be formed from fewer than one element. |
| Array with a single element | Return one since a single element trivially forms a valid arithmetic subsequence of length one. |
| Array with exactly two elements | Return two because any pair of numbers forms an arithmetic subsequence with a difference equal to their separation. |
| Array with all identical elements | Return the total count of the array since the difference between consecutive identical elements is always zero. |
| Array with negative numbers and mixed signs | Use difference calculations that properly account for signs so negative shifts do not corrupt the hash map lookups. |
| Extremely large array with maximum constraints | Apply an O(n squared) dynamic programming approach with a hash map to avoid the O(n cubed) brute force time limit. |
| Large differences between elements causing integer overflow | Ensure differences are computed within standard integer ranges or use language-specific wider data types if necessary. |
| Array in strictly descending order | Track negative common differences correctly in the state table to successfully identify decreasing arithmetic subsequences. |