sum of three values

This commit is contained in:
2024-07-22 18:11:31 +02:00
parent 08c87b425b
commit 1181d6aa96
2 changed files with 41 additions and 1 deletions

File diff suppressed because one or more lines are too long

View File

@@ -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;
}