Find the indexes using two pointer theory
#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define int long long int
#define FAST ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
void fond(vector<int>& v, vector<int>& v1, int x) {
int l = 0, r = v1.size() - 1;
bool bug = false;
while (l < r) {
int sum = v1[l] + v1[r];
if (sum == x) {
bug = true;
break;
} else if (sum < x) {
l++;
} else {
r--;
}
}
if (!bug) {
cout << "IMPOSSIBLE" << nl;
} else {
// Find indices in the original vector v
auto it1 = find(v.begin(), v.end(), v1[l]);
auto it2 = find(v.begin(), v.end(), v1[r]);
if (it1 != v.end() && it2 != v.end()) {
int idx1 = it1 - v.begin() + 1;
int idx2 = it2 - v.begin() + 1;
if (idx1 == idx2) {
// If both indices are the same, find the next occurrence
it2 = find(it1 + 1, v.end(), v1[r]);
if (it2 != v.end()) {
idx2 = it2 - v.begin() + 1;
}
}
cout << idx1 << " " << idx2 << nl;
}
}
}
signed main() {
FAST
int n, num;
cin >> n >> num;
vector<int> v(n);
vector<int> v1(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
v1 = v;
sort(v1.begin(), v1.end());
fond(v, v1, num);
return 0;
}
Comments
Post a Comment