fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8. vector<char> arr = {'x', 'y', 'x', 'x', 'y', 'x', 'y', 'y'};
  9. int n = arr.size();
  10. int x_count = 0 ; int y_count = 0 ;
  11. int count = 0 ;
  12. unordered_map<int,int>hash;
  13. hash[0]=1;
  14. for(int i = 0 ; i<n;i++){
  15. if(arr[i]=='x') x_count++;
  16. else {
  17. y_count++;
  18. }
  19. int diff = x_count-y_count;
  20. count+=hash[diff];
  21. hash[diff] ++;
  22. }
  23. cout<<count;
  24.  
  25.  
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
10