<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>



/**ank**/
#include <bits/stdc++.h>
using namespace std;
#define ll                     long long int
#define all(v)                 v.begin(), v.end()
#define maxi(v)                *max_element(v.begin(), v.end())
#define vecsum(v)              accumulate(v.begin(), v.end(), 0)
#define mini(v)                *min_element(v.begin(), v.end())
#define sortd(v)                sort(v.begin(), v.end(), greater<int>())
#define read(a, n)             for(int i=0;i<n;i++)    cin >> a[i]
#define print(a, n)            for(int i=0;i<n;i++)    cout << a[i] << " "; cout << endl
#define rep(a,n,b,i)           for(int i=a;i<n;i+=b)       
#define M                       1000000007
#define pb                      push_back
#define sz                      size()
#define len                     length()
#define fir                     first
#define sec                     second
#define cy                      cout << "YES" << endl
#define cn                      cout << "NO" << endl 
#define mll                     map<ll,ll>
#define mcl                     map<char,ll>
#define msl                     map<string,ll>
#define si                      set<ll>
#define sc                      set<char>
using pp = pair<ll,ll>;
using vl = vector<ll>;
using vc = vector<char>;
using vi = vector<int>;
using vvl=vector<vector<ll>>;
using vvc=vector<vector<char>>;
class Solution {
public:

    int maxTurbulenceSize(vector<int>& arr) {
        ll n=arr.sz,ans=1;
        vector<pp> dp(n,{1,1});
        rep(1,n,1,i){
            if(arr[i-1]<arr[i]){
                dp[i].fir=1+dp[i-1].sec;
            }
            else if(arr[i-1]>arr[i]){
                dp[i].sec=1+dp[i-1].fir;
            }
            ans=max({ans,dp[i].fir,dp[i].sec});
        }
        return ans;
    }
};
49400