fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int distinctSubarrayCount(vector<int> &arr, int k){
  5. int count = 0;
  6. for (int i = 0; i < arr.size(); i++){
  7.  
  8. unordered_map<int, int> freq;
  9. for (int j = i; j < arr.size(); j++){
  10. freq[arr[j]]++;
  11.  
  12. int n = freq.size();
  13.  
  14. if (n <= k) count++;
  15. }
  16. }
  17.  
  18. return count;
  19. }
  20.  
  21. int main() {
  22. vector<int> arr = {1, 2, 3};
  23. cout << distinctSubarrayCount(arr, 2);
  24. return 0;
  25. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
5