You are given an array of integers nums
. Return the length of the longest subarray
of nums
which is either strictly increasing or strictly decreasing.
For example:
[1,4,3,3,2]
should return 2
because the strictly increasing subarrays are [1]
, [2]
, [3]
, [3]
, [4]
, and [1,4]
, and the strictly decreasing subarrays are [1]
, [2]
, [3]
, [3]
, [4]
, [3,2]
, and [4,3]
. Thus, the longest is length 2
.[3,3,3,3]
should return 1
because the strictly increasing subarrays are [3]
, [3]
, [3]
, and [3]
, and the strictly decreasing subarrays are [3]
, [3]
, [3]
, and [3]
. Thus, the longest is length 1
.[3,2,1]
should return 3
because the strictly increasing subarrays are [3]
, [2]
, and [1]
, and the strictly decreasing subarrays are [3]
, [2]
, [1]
, [3,2]
, [2,1]
, and [3,2,1]
. Thus, the longest is length 3
.Write a function that takes in an array of integers and returns the length of the longest increasing or decreasing subarray.
When 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:
The brute force approach to this problem is all about trying every single possibility. We will look at every possible consecutive group of numbers in the list and check if that group is strictly increasing or strictly decreasing.
Here's how the algorithm would work step-by-step:
def longest_strictly_increasing_or_decreasing_subarray_brute_force(numbers):
list_length = len(numbers)
longest_length = 0
for starting_index in range(list_length):
for ending_index in range(starting_index, list_length):
# Determine the subarray to analyze
sub_array = numbers[starting_index:ending_index + 1]
sub_array_length = len(sub_array)
if sub_array_length > longest_length:
is_increasing = True
is_decreasing = True
# Check if the sub_array is strictly increasing
for index in range(1, sub_array_length):
if sub_array[index] <= sub_array[index - 1]:
is_increasing = False
break
# Check if the sub_array is strictly decreasing
for index in range(1, sub_array_length):
if sub_array[index] >= sub_array[index - 1]:
is_decreasing = False
break
# Record the current longest length if applicable
if is_increasing or is_decreasing:
longest_length = sub_array_length
return longest_length
The most efficient way to find the longest increasing or decreasing section is to look at the input in one go. We keep track of the current increasing and decreasing lengths and update the longest one we've seen so far. This way, we don't need to re-check parts of the input.
Here's how the algorithm would work step-by-step:
def find_longest_subarray(input_array):
array_length = len(input_array)
if array_length == 0:
return 0
longest_subarray_length = 1
increasing_count = 1
decreasing_count = 1
for i in range(1, array_length):
if input_array[i] > input_array[i - 1]:
# Increasing sequence continues
increasing_count += 1
decreasing_count = 1
elif input_array[i] < input_array[i - 1]:
# Decreasing sequence continues
decreasing_count += 1
# Reset the increasing_count
increasing_count = 1
else:
# Reset both counts if elements are equal
increasing_count = 1
decreasing_count = 1
# Update the longest length found so far
longest_subarray_length = max(
longest_subarray_length,
increasing_count,
decreasing_count
)
return longest_subarray_length
Case | How to Handle |
---|---|
Null or empty input array | Return 0 as the length of the longest subarray in such cases is zero; handle null arrays gracefully to prevent NullPointerExceptions. |
Array with a single element | Return 1, as a single element array is trivially both strictly increasing and strictly decreasing. |
Array with two identical elements | Return 1, as neither an increasing nor decreasing subarray of length 2 exists. |
Array with many consecutive identical elements | The algorithm should correctly reset the increasing/decreasing subarray length when encountering such sequences. |
Array with integer overflow issues (very large numbers) | Use appropriate data types (e.g., long) to prevent integer overflow when comparing elements. |
Array where the longest strictly increasing and strictly decreasing subarrays have the same length | The algorithm should return the correct length regardless of whether increasing or decreasing sequence is first. |
Input array is very large, leading to memory constraints | The solution should be optimized for space complexity, ideally using O(1) extra space by iterating through array and tracking lengths instead of storing subarrays. |
Array with alternating increasing and decreasing sequences | The algorithm should correctly handle the transition between increasing and decreasing sequences and accurately track the longest one. |