-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum_of_Three_Values.cpp
More file actions
36 lines (35 loc) · 1.01 KB
/
Sum_of_Three_Values.cpp
File metadata and controls
36 lines (35 loc) · 1.01 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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const char nl = '\n';
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
int x; cin >> x; //size and target
vector<pair<int, int>> a(n); // {value, original index}
for(int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i + 1; //1 indexed
}
sort(a.begin(), a.end());
// Now, keep 1 fixed and do 2sum
for(int i = 0; i < n - 2; i++) {
int target = x - a[i].first;
int left = i + 1, right = n - 1;
while(left < right) {
int sum = a[left].first + a[right].first;
if(sum == target) {
// Output original indices
cout << a[i].second << " " << a[left].second << " " << a[right].second << nl;
return 0;
} else if(sum < target) {
left++;
} else {
right--;
}
}
}
cout << "IMPOSSIBLE" << nl;
return 0;
}