Youyou’s longest stable subarray
idea: double pointer algorithm
#include<iostream>
#include<cmath>
using namespace std;
int a[100010];
int res = 0;
int main()
{
int n;
cin>> n;
int j = 0;
for(int i = 1; i <= n ;++ i)
cin >> a[i];
for(int i = 1,j = 1 ; i <= n;++ i)
{
j = max(i + 1 , j);
while(j <= n && abs(a[j-1] - a[j]) <= 1) j++;
res = max(res, j - i);
}
cout << res << endl;
return 0;
}