[InterviewBit] Equal

Equal

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vector<int> Solution::equal(vector<int> &A) {
unordered_map<int, pair<int,int>> mp;
vector<pair<int,int>> res {{INT_MAX,INT_MAX},{INT_MAX,INT_MAX}};
for(int i = 1; i < A.size(); i++) {
for(int j = 0; j < i; j++) {
int sum = A[i] + A[j];
if(mp.count(sum)) {
if(i != mp[sum].first and i != mp[sum].second and j != mp[sum].first and j != mp[sum].second) {
vector<pair<int,int>> now = {mp[sum], {j,i}};
sort(begin(now), end(now));
res = min(res, now);
}
mp[sum] = min(mp[sum], {j,i});
} else mp[sum] = {j,i};
}
}
return {res[0].first, res[0].second, res[1].first, res[1].second};
}

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