My code is not working in this question https://www.codechef.com/submit/PERMEXIS?

#include <bits/stdc++.h>

using namespace std;

int main() {
// your code goes here
int t;
cin>>t ;
while(t–){
int n;
cin>>n;
int arr[n];
int flag=0;

    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    sort(arr,arr+n);
    for(int i=0;i<n;i++){
       if(arr[i+1]-arr[i]<=1){
           flag=1;
           
       }
    }
    if(flag==1){
        cout<<"YES"<<endl;
        
    }
    if(flag==0){
        cout<<"NO"<<endl;
    }
}
return 0;

}

The absolute value of arr[i+1]-arr[i] should be less than 1
So you should use: abs(arr[i+1]-arr[i])<=1 in the if statement

Better, use:

for(int i=0;i<n-1;i++)
{
   if(abs(arr[i+1]-arr[i]) > 1)
   {
      flag = 1;
      break;
   }
}
if(flag==0)
cout<<"YES"<<endl;
if(flag==1){
cout<<"NO"<<endl;