fork download
  1. // TC : O(N^2)
  2. // SC : O(N)
  3.  
  4. #include <iostream>
  5. #include<unordered_map>
  6. using namespace std;
  7.  
  8. int main() {
  9. int n;
  10. cin >> n;
  11.  
  12. int arr[n];
  13. for(int i=0; i<n; i++) {
  14. cin >> arr[i];
  15. }
  16.  
  17. int count = 0;
  18. int k;
  19. cin >> k;
  20.  
  21. int sum = 0;
  22.  
  23. for(int i=0; i<n; i++) {
  24. unordered_map<int, int> mp;
  25.  
  26. for(int j=i; j<n; j++) {
  27. mp[arr[j]]++;
  28. int distinct = mp.size();
  29. if(distinct <= k) {
  30. count++;
  31. } else {
  32. break;
  33. }
  34. }
  35. }
  36. cout << "Count : " << count;
  37. return 0;
  38. }
Success #stdin #stdout 0s 5320KB
stdin
5
1
2
2
5
8
4
stdout
Count : 15