sum of three values
This commit is contained in:
@@ -0,0 +1,40 @@
|
|||||||
|
// Sum of Three Values
|
||||||
|
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//also possible with sorting
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n, x;
|
||||||
|
cin >> n >> x;
|
||||||
|
vector<int> array(n);
|
||||||
|
map<int, set<int>> map;
|
||||||
|
for (int i{}; i < n; i++) {
|
||||||
|
cin >> array[i];
|
||||||
|
map[array[i]].insert(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i{}; i < n; i++) {
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
int sum = x - array[i] - array[j];
|
||||||
|
if (map.count(sum)) {
|
||||||
|
bool first = map[sum].count(i);
|
||||||
|
bool second = map[sum].count(array[j]);
|
||||||
|
map[sum].erase(i);
|
||||||
|
map[sum].erase(j);
|
||||||
|
if (!map[sum].empty()) {
|
||||||
|
cout << i + 1 << " " << j + 1 << " " << *map[sum].begin() + 1 << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (first) map[sum].insert(i);
|
||||||
|
if (second) map[sum].insert(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "IMPOSSIBLE" << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user