The idea is to maintain a boolean array to store set of absolute difference of successive elements.
a) If absolute difference between two elements is more than n-1 or 0, return false.
b) If an absolute difference repeated, then all absolute differences from 1 to n-1 can’t be present [Pigeon Hole Principle](Pigeon Hole Principle), return false.
<h2>
#include<bits/stdc++.h>
using namespace std;
set<int > S;
int A[1000001];
int main(){
int t ;
cin >> t ;
while(t--){
int N ;
cin >> N ;
for(int i = 1 ; i<= N ; i++) cin >> A[i];
for(int i = 2 ; i<= N ; i++){
S.insert(abs(A[i] - A[i-1]));
}
if(S.size() == N - 1) {
int ind = 1;
bool found = true ;
for(auto it = S.begin() ; it!= S.end() ; it++ , ind++){
if(*it != ind) {
found = false ;
break;
}
}
if(found)
cout << "Jolly";
else
cout << "Not Jolly";
}
else
cout << "Not Jolly";
S.clear();
}
} </pre>