forked from black-shadows/InterviewBit-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqual.cpp
More file actions
55 lines (50 loc) · 1.81 KB
/
Copy pathEqual.cpp
File metadata and controls
55 lines (50 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// https://www.interviewbit.com/problems/equal/
vector<int> Solution::equal(vector<int> &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
unordered_map <int, vector<int> > myMap;
vector<int> sol;
vector<int> temp;
int count = 0;
for(int i = 0; i < A.size()-1; i++){
for(int j = i+1; j < A.size(); j++){
int sum = A[i] + A[j];
if(myMap.find(sum) == myMap.end()){
myMap[sum].push_back(i);
myMap[sum].push_back(j);
}
else{
temp.push_back(myMap[sum][0]);
temp.push_back(myMap[sum][1]);
temp.push_back(i);
temp.push_back(j);
for(int a = 0; a < temp.size()-1; a++){
for(int b = a+1; b < temp.size(); b++){
if(temp[a] == temp[b]){
goto COND;
}
}
}
count++;
if(count == 1){
sol = temp;
}
else{
for(int z = 0; z < sol.size(); z++){
if(sol[z] > temp[z]){
sol = temp;
break;
}
else if(sol[z] < temp[z]){
break;
}
}
}
COND:temp.erase(temp.begin(), temp.end());
}
}
}
return sol;
}