Think the first if it is 1 & last if it is 1000
Example 1 2 5 6(1st element is loyal)
Example 998 999 1000(last element is loyal)
Middle elements before & after element must be distance of 2
1 3 4 5 6 9
Think of the 3rd element (4th element-2nd element=1)
Also it works for 4th element
///Bismillahir Rahmanir Rahim
///Author: Tanvir Ahmmad
///CSE,Islamic University,Bangladesh
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<iterator>
#include <functional> ///sort(arr,arr+n,greater<int>()) for decrement of array
/*every external angle sum=360 degree
angle find using polygon hand(n) ((n-2)*180)/n*/
///Floor[Log(b) N] + 1 = the number of digits when any number is represented in base b
using namespace std;
typedef long long ll;
int main()
{
ll n,cnt=0,ans=0;
cin>>n;
ll arr[n+2],vis[n+2];
for(ll i=1; i<=n; i++)
cin>>arr[i],vis[i]=0;
for(ll i=2; i<n; i++)
if((arr[i]-arr[i-1])==1 && (arr[i+1]-arr[i])==1 && 1<=arr[i] && arr[i]<=1000)
vis[i]=1;
if(n>=2 && arr[2]-arr[1]==1 && arr[1]==1) vis[1]=1;
if(n>=2 && arr[n]-arr[n-1]==1 && arr[n]==1000) vis[n]=1;
for(ll i=1;i<=n;i++)
{
if(vis[i]==1) cnt++;
else ans=max(ans,cnt),cnt=0;
}
ans=max(ans,cnt);
cout<<ans<<endl;
return 0;
}
///Alhamdulillah
Problem Link:https://codeforces.com/problemset/problem/1062/A?f0a28=1
Comments
Post a Comment