<p>Given an integer array <code>arr</code>, return <em>the length of a maximum size turbulent subarray of</em> <code>arr</code>.</p>

<p>A subarray is <strong>turbulent</strong> if the comparison sign flips between each adjacent pair of elements in the subarray.</p>

<p>More formally, a subarray <code>[arr[i], arr[i + 1], ..., arr[j]]</code> of <code>arr</code> is said to be turbulent if and only if:</p>

<ul>
	<li>For <code>i &lt;= k &lt; j</code>:

	<ul>
		<li><code>arr[k] &gt; arr[k + 1]</code> when <code>k</code> is odd, and</li>
		<li><code>arr[k] &lt; arr[k + 1]</code> when <code>k</code> is even.</li>
	</ul>
	</li>
	<li>Or, for <code>i &lt;= k &lt; j</code>:
	<ul>
		<li><code>arr[k] &gt; arr[k + 1]</code> when <code>k</code> is even, and</li>
		<li><code>arr[k] &lt; arr[k + 1]</code> when <code>k</code> is odd.</li>
	</ul>
	</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [4,8,12,16]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [100]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 4 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public: 
    int rec(int ind ,int currlen , int sign , vector<int> &arr , vector<vector<int>> &dp){
        if(ind == arr.size()-2){
            if(arr[ind] > arr[ind+1] && sign ==1) return currlen+1;
            if(arr[ind] < arr[ind+1] && sign ==0 ) return currlen+1 ;
            if(arr[ind]!=arr[ind+1]) return max(2 , currlen);
            return currlen; 
        }
        if(dp[sign+1][ind]!=-1) return dp[sign+1][ind];
        int l1 = -1e9, l2 = -1e9 ; 
        int tt = currlen ; 
        if(sign == -1){
            if(arr[ind] > arr[ind+1]) {
                l1 = rec(ind+1 , 2 , 0 , arr , dp);
            }
            else if(arr[ind] < arr[ind+1]){
                l1 = rec(ind+1 , 2 , 1 , arr , dp);
            }
            else{
                l1 = rec(ind+1 , 0 , -1 , arr , dp);
            }
        }
        else{
         
            if(arr[ind] > arr[ind+1] && sign == 1) {
                l1 = rec(ind+1 , currlen+1 , 0 , arr , dp);
            }
            else if(arr[ind] < arr[ind+1] && sign ==0){
                l1 = rec(ind+1 , currlen+1 , 1 , arr , dp) ;
            }
            l2 = rec(ind , 0 , -1 , arr, dp);
        }
        return dp[sign+1][ind] =  max(l1,max(l2 , tt));
    }

    int maxTurbulenceSize(vector<int>& arr) {
        if(arr.size()==1) return 1;
        vector<vector<int>> dp(3 , vector<int>(arr.size(),-1));
        return max(1 , rec(0 , 0 , -1 , arr , dp));
    }
};
54200