# Longest Turbulent Subarray Problem

0 like 0 dislike
630 views

A subarray `A[i], A[i+1], ..., A[j]` of `A` is said to be turbulent if and only if:

• For `i <= k < j``A[k] > A[k+1]` when `k` is odd, and `A[k] < A[k+1]` when `k` is even;
• OR, for `i <= k < j``A[k] > A[k+1]` when `k` is even, and `A[k] < A[k+1]` when `k` is odd.

That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.

Return the length of a maximum size turbulent subarray of A.

Example 1:

```Input: [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: (A > A < A > A < A)
```

Example 2:

```Input: [4,8,12,16]
Output: 2
```

Example 3:

```Input: 
Output: 1
```

Note:

1. `1 <= A.length <= 40000`
2. `0 <= A[i] <= 10^9`

### For Indian Students- INR 360/- || For International Students- \$9.99/-

S.No.

Course Name

Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon

0 like 0 dislike
by Goeduhub's Expert (2.3k points)

Intuition-

Evidently, we only care about the comparisons between adjacent elements. If the comparisons are represented by -1, 0, 1 (for <, =, >), then we want the longest sequence of alternating 1, -1, 1, -1, ... (starting with either 1 or -1).

These alternating comparisons form contiguous blocks. We know when the next block ends: when it is the last two elements being compared, or when the sequence isn't alternating.

For example, take an array like A = [9,4,2,10,7,8,8,1,9]. The comparisons are [1,1,-1,1,-1,0,-1,1]. The blocks are , [1,-1,1,-1], , [-1,1].

Algorithm-

Scan the array from left to right. If we are at the end of a block (last elements OR it stopped alternating), then we should record the length of that block as our candidate answer, and set the start of the new block as the next element.

class Solution(object):

def maxTurbulenceSize(self, A):

N = len(A)

ans = 1

anchor = 0

for i in xrange(1, N):

c = cmp(A[i-1], A[i])

if c == 0:

anchor = i

elif i == N-1 or c * cmp(A[i], A[i+1]) != -1:

ans = max(ans, i - anchor + 1)

anchor = i

return ans

eg : ans = Solution()

ans.maxTurbulenceSize([9,4,2,10,7,8,8,1,9])

Output-

5