[InterviewBit] Make equal elements Array

Make equal elements Array

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
int Solution::solve(vector<int> &A, int B) {
unordered_map<int, int> freq;
for(int i = 0; i < A.size(); i++) {
int sub = A[i] - B, add = A[i] + B, none = A[i];
freq[sub] += 1;
freq[add] += 1;
freq[none] += 1;
if(freq[sub] != i + 1 and freq[add] != i + 1 and freq[none] != i + 1) return 0;
}
return 1;
}

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