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:


	For i &lt;= k &lt; j:

	
		arr[k] &gt; arr[k + 1] when k is odd, and
		arr[k] &lt; arr[k + 1] when k is even.
	
	
	Or, for i &lt;= k &lt; j:
	
		arr[k] &gt; arr[k + 1] when k is even, and
		arr[k] &lt; arr[k + 1] when k is odd.
	
	


&nbsp;
Example 1:

Input: arr = [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: arr[1] &gt; arr[2] &lt; arr[3] &gt; arr[4] &lt; arr[5]


Example 2:

Input: arr = [4,8,12,16]
Output: 2


Example 3:

Input: arr = [100]
Output: 1


&nbsp;
Constraints:


	1 &lt;= arr.length &lt;= 4 * 104
	0 &lt;= arr[i] &lt;= 109


class Solution:
    def maxTurbulenceSize(self, arr: List[int]) -> int:
        def cmp(a,b):
            if a == b: return 0
            if a > b : return 1
            return -1

        n = len(arr)
        ans = 1
        prev = 0
        for i in range(1,n):
            c = cmp(arr[i-1],arr[i])
            if c == 0:
                # we shift prev to i
                prev = i
            elif i == n-1 or c * cmp(arr[i],arr[i+1]) != -1:
                ans = ans if ans > i - prev + 1 else i - prev + 1
                prev = i
        return ans
class Solution {
    public int maxTurbulenceSize(int[] arr) {
        if(arr.length == 1) {
            return 1;
        } 
        int l = 0, r = 1;
        int diff = arr[l] - arr[r];
        int max;
        if(diff == 0) {
            l = 1;
            r = 1;
            max = 1;
        } else {
            l = 0;
            r = 1;
            max = 2;
        }
        for(int i = 1; r < arr.length-1; i++) {
            int nextdiff = arr[i] - arr[i+1];
            if(diff < 0) {
                if(nextdiff > 0) {
                    r++;
                } else if(nextdiff == 0) {
                    l = i+1;
                    r = i+1;
                } else {
                    l = i;
                    r = i+1;
                }
            } else {
                if(nextdiff < 0) {
                    r++;
                } else if(nextdiff == 0) {
                    l = i+1;
                    r = i+1;
                } else {
                    l = i;
                    r = i+1;
                }
            }
            diff = nextdiff;
            max = Math.max(max, r-l+1);
        }
        return max;
    }
}
class Solution {
public:
    int maxTurbulenceSize(vector<int>& arr) {
        vector<int> table1(arr.size(), 0);
        vector<int> table2(arr.size(), 0);
        table1[0] = 1;
        table2[0] = 1;
        int max_len = 1;
        for (int i=1; i<arr.size(); ++i) {
            table1[i] = 1;
            table2[i] = 1;
            if (arr[i] < arr[i - 1] && (i & 1) == 0) {
                table1[i] = table1[i - 1] + 1;
            } else if (arr[i] > arr[i - 1] && (i & 1) == 1) {
                table1[i] = table1[i - 1] + 1;
            }
            if (arr[i] > arr[i - 1] && (i & 1) == 0) {
                table2[i] = table2[i - 1] + 1;
            } else if (arr[i] < arr[i - 1] && (i & 1) == 1) {
                table2[i] = table2[i - 1] + 1;
            }
            max_len = max(max_len, table1[i]);
            max_len = max(max_len, table2[i]);
        }
        return max_len;
    }
};
var maxTurbulenceSize = function(arr) {
    const len = arr.length;
    const dp = Array.from({ length: len + 1 }, () => {
        return new Array(2).fill(0);
    });
    let ans = 0;
    for(let i = 1; i < len; i++) {
        if(arr[i-1] > arr[i]) {
            dp[i][0] = dp[i-1][1] + 1;
        } else if(arr[i-1] < arr[i]) {
            dp[i][1] = dp[i-1][0] + 1;
        }
        ans = Math.max(ans, ...dp[i]);
    }

    // console.log(dp);
    return ans + 1;
};
Longest Turbulent Subarray