You are a data scientist analyzing stock market data. You're particularly interested in periods of "turbulence" - where the market alternates between rising and falling in a consistent pattern. This turbulence can be a good indicator of market volatility and potential trading opportunities.
Given an array of integers arr
, your task is to find the length of the longest subarray that is "turbulent".
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, and arr[k] < arr[k+1]
when k
is even.i <= k < j
: arr[k] < arr[k+1]
when k
is odd, and arr[k] > arr[k+1]
when k
is even.In other words, a turbulent subarray alternates between increasing and decreasing pairs of adjacent elements.
Input: arr = [9, 4, 2, 10, 7, 8, 8, 1, 9]
Output: 5
Explanation: The turbulent subarrays are [9, 4, 2, 10, 7] and [8, 1, 9]. The first one has length 5, which is the maximum.
Input: arr = [4, 8, 12, 16]
Output: 2
Explanation: The turbulent subarrays are [4, 8] and [8, 12] and [12, 16]. All of them have length 2, which is the maximum.
Input: arr = [100]
Output: 1
Explanation: The turbulent subarrays are just [100] itself, which has length 1.
To solve this problem, we need to:
Apply string manipulation concepts to solve a real-world problem.
You are a data scientist analyzing stock market data. You're particularly interested in periods of "turbulence" - where the market alternates between rising and falling in a consistent pattern. This turbulence can be a good indicator of market volatility and potential trading opportunities.
Given an array of integers arr
, your task is to find the length of the longest subarray that is "turbulent".
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, and arr[k] < arr[k+1]
when k
is even.i <= k < j
: arr[k] < arr[k+1]
when k
is odd, and arr[k] > arr[k+1]
when k
is even.In other words, a turbulent subarray alternates between increasing and decreasing pairs of adjacent elements.
The turbulent subarrays are [9, 4, 2, 10, 7] and [8, 1, 9]. The first one has length 5, which is the maximum.
The turbulent subarrays are [4, 8] and [8, 12] and [12, 16]. All of them have length 2, which is the maximum.
The turbulent subarrays are just [100] itself, which has length 1.
The key insight is to track the current turbulent subarray length as we iterate through the array.
We need to keep track of the comparison sign between adjacent elements (increasing or decreasing).
When the comparison sign flips, we can extend the current turbulent subarray.
When the comparison sign stays the same or elements are equal, we need to reset or adjust our tracking.
A single element is considered a turbulent subarray of length 1.
This problem has several practical applications:
Detecting periods of market volatility in stock price data.
Identifying turbulent weather patterns in temperature or pressure data.
Finding oscillating patterns in signal data for anomaly detection.
You are a data scientist analyzing stock market data. You're particularly interested in periods of "turbulence" - where the market alternates between rising and falling in a consistent pattern. This turbulence can be a good indicator of market volatility and potential trading opportunities.
Given an array of integers arr
, your task is to find the length of the longest subarray that is "turbulent".
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, and arr[k] < arr[k+1]
when k
is even.i <= k < j
: arr[k] < arr[k+1]
when k
is odd, and arr[k] > arr[k+1]
when k
is even.In other words, a turbulent subarray alternates between increasing and decreasing pairs of adjacent elements.
Input: arr = [9, 4, 2, 10, 7, 8, 8, 1, 9]
Output: 5
Explanation: The turbulent subarrays are [9, 4, 2, 10, 7] and [8, 1, 9]. The first one has length 5, which is the maximum.
Input: arr = [4, 8, 12, 16]
Output: 2
Explanation: The turbulent subarrays are [4, 8] and [8, 12] and [12, 16]. All of them have length 2, which is the maximum.
Input: arr = [100]
Output: 1
Explanation: The turbulent subarrays are just [100] itself, which has length 1.
To solve this problem, we need to:
Apply string manipulation concepts to solve a real-world problem.
You are a data scientist analyzing stock market data. You're particularly interested in periods of "turbulence" - where the market alternates between rising and falling in a consistent pattern. This turbulence can be a good indicator of market volatility and potential trading opportunities.
Given an array of integers arr
, your task is to find the length of the longest subarray that is "turbulent".
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, and arr[k] < arr[k+1]
when k
is even.i <= k < j
: arr[k] < arr[k+1]
when k
is odd, and arr[k] > arr[k+1]
when k
is even.In other words, a turbulent subarray alternates between increasing and decreasing pairs of adjacent elements.
The turbulent subarrays are [9, 4, 2, 10, 7] and [8, 1, 9]. The first one has length 5, which is the maximum.
The turbulent subarrays are [4, 8] and [8, 12] and [12, 16]. All of them have length 2, which is the maximum.
The turbulent subarrays are just [100] itself, which has length 1.
The key insight is to track the current turbulent subarray length as we iterate through the array.
We need to keep track of the comparison sign between adjacent elements (increasing or decreasing).
When the comparison sign flips, we can extend the current turbulent subarray.
When the comparison sign stays the same or elements are equal, we need to reset or adjust our tracking.
A single element is considered a turbulent subarray of length 1.
This problem has several practical applications:
Detecting periods of market volatility in stock price data.
Identifying turbulent weather patterns in temperature or pressure data.
Finding oscillating patterns in signal data for anomaly detection.