vendredi 1 mai 2020

what is the most efficient way to find total number of distinct subarrays having distinct elements

here's the brute force method, counting distinct sets in set of sets. but well it's so inefficient can hardly call it a solution, appreciate your help. i have already tried hashing but couldn't come up with a solution

int main(){
  int n;
  cin>>n;
  int v[n];
  set<int>s;
  set<set<int>>a;
  for(int h=0;h<n;h++)
  cin>>v[h];
  for(int h=0;h<n;h++){
    for(int j=h;j<n;j++){
    s.insert(v[j]);
    a.insert(s);
    }
    s.clear();
  }
cout<<a.size();
}

Aucun commentaire:

Enregistrer un commentaire