Given an integer array arr, return the length of a maximum size turbulent subarray of arr.
A subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.
More formally, a subarray [arr[i], arr[i + 1], ..., arr[j]] of arr is said to be turbulent if and only if:
i <= k < j:
arr[k] > arr[k + 1] when k is odd, andarr[k] < arr[k + 1] when k is even.i <= k < j:
arr[k] > arr[k + 1] when k is even, andarr[k] < arr[k + 1] when k is odd.Example 1:
Input: arr = [9,4,2,10,7,8,8,1,9] Output: 5 Explanation: arr[1] > arr[2] < arr[3] > arr[4] < arr[5]
Example 2:
Input: arr = [4,8,12,16] Output: 2
Example 3:
Input: arr = [100] Output: 1
Constraints:
1 <= arr.length <= 4 * 1040 <= arr[i] <= 109When 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:
We need to find the longest stretch of numbers that go up and down alternately, like a bumpy road. The brute force way means we'll try every possible stretch to see if it's bumpy and then find the longest one.
Here's how the algorithm would work step-by-step:
def longest_turbulent_subarray_brute_force(array):
max_length = 0
for start_index in range(len(array)):
for end_index in range(start_index, len(array)):
sub_array = array[start_index : end_index + 1]
if len(sub_array) <= 1:
max_length = max(max_length, len(sub_array))
continue
is_turbulent = True
# Check if the current subarray is turbulent.
for index in range(1, len(sub_array)):
if index % 2 == 1:
if sub_array[index] >= sub_array[index - 1]:
is_turbulent = False
break
else:
if sub_array[index] <= sub_array[index - 1]:
is_turbulent = False
break
#Alternative check, different start.
if not is_turbulent and len(sub_array) > 1:
is_turbulent = True
for index in range(1, len(sub_array)):
if index % 2 == 1:
if sub_array[index] <= sub_array[index - 1]:
is_turbulent = False
break
else:
if sub_array[index] >= sub_array[index - 1]:
is_turbulent = False
break
# Update max length if turbulent
if is_turbulent:
current_length = len(sub_array)
max_length = max(max_length, current_length)
return max_lengthThe key is to efficiently track the length of turbulent subarrays as we move through the data. We maintain two counts representing the lengths of increasing and decreasing turbulent subarrays ending at the current position. By updating these counts strategically, we avoid redundant calculations.
Here's how the algorithm would work step-by-step:
def longestTurbulentSubarray(data):
increasing_count = 1
decreasing_count = 1
max_length = 1
for i in range(1, len(data)):
if data[i] > data[i - 1]:
# Extending decreasing, so reset increasing.
decreasing_count = increasing_count + 1
increasing_count = 1
elif data[i] < data[i - 1]:
# Extending increasing, so reset decreasing.
increasing_count = decreasing_count + 1
decreasing_count = 1
else:
# Reset both counts if the numbers are equal.
increasing_count = 1
decreasing_count = 1
max_length = max(max_length, increasing_count, decreasing_count)
return max_length| Case | How to Handle |
|---|---|
| Null or Empty Input Array | Return 0 immediately as there's no subarray. |
| Array with only one element | Return 1 as a single element is technically a turbulent subarray of length 1. |
| Array with two identical elements | Return 1, as a turbulent subarray requires alternating signs. |
| Array with two different elements | Return 2, since any two different elements form a turbulent subarray. |
| Array with all identical values | Return 1, as no two adjacent elements satisfy the turbulent condition. |
| Array with alternating values (e.g., [1, 2, 1, 2, 1]) | The algorithm should correctly identify the entire array as a turbulent subarray and return its length. |
| Array with consecutive identical values breaking a turbulent sequence | The algorithm should reset the turbulent sequence counter when consecutive identical values are encountered. |
| Large input array to assess time complexity | The solution should ideally have linear time complexity to handle large arrays efficiently; check that it scales to the maximum constraint size (e.g., 4 * 10^4). |