Can any one explain OR-thodox distinction problem code give TLE by my approach any idea to remove TLE from this code?

#include<bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long int t;
    cin>>t;
    while(t--){
        long long int n;
        cin>>n;
        long long int a[n],i;
        for(i=0;i<n;i++){
            cin>>a[i];
           
        }
       long long int  ans=a[0],flag=0,res[n],j;
       
        for(i=1;i<n;i++){
        ans=ans|a[i];
        res[i]=ans;
        }
        for(i=0;i<n;i++){
                for(j=0;j<n;j++){
                    if(res[i]==a[j]){
                     flag=1;
                     break;
            
                 }
        }
            }
        
      if(flag==1){
          cout<<"NO"<<endl;
      }
      else{
          cout<<"YES"<<endl;
      }
        
    }
    
	// your code goes here
	return 0;
}

I think you did not understand the question. You have create sub arrays (like we create substring from a string) and OR all values in sub array. If any of the OR value matches with another sub array value then answer is No else Yes.

Your solution is an O(n^2) implementation, where n is the input size. Optimize it to O(n).