fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int getCount(int n,vector<int>arr,int k){
  4. int count=0;
  5. for(int i=0;i<n;i++){
  6. unordered_map<int,int>mp;
  7. for(int j=i;j<n;j++){
  8. mp[arr[j]]++;
  9. int s=mp.size();
  10. if(s<=k){
  11. count++;
  12. }
  13. }
  14. }
  15. return count;
  16. }
  17.  
  18. int main() {
  19. // your code goes here
  20. int n;
  21. cin>>n;
  22. vector<int>arr(n,0);
  23. for(int i=0;i<n;i++){
  24. cin>>arr[i];
  25. }
  26. int k;
  27. cin>>k;
  28. cout<<"The number of subarrays with distinct elemets less tha k is:"<<getCount(n,arr,k);
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5280KB
stdin
3
1 2 3
2
stdout
The number of subarrays with distinct elemets less tha k is:5