[InterviewBit] Subarray with equal occurences!

Subarray with equal occurences!

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
int Solution::solve(vector<int> &A, int B, int C) {
unordered_map<int, int> freq{{0,1}};
int b = 0, c = 0, res = 0;
for(auto a : A) {
if(a == B) b += 1;
if(a == C) c += 1;
res += freq[b-c];
freq[b-c] += 1;
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/07/PS/interviewbit/subarray-with-equal-occurences/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.