Sum of Two Values

Problem Link

Solution by TioPatinhas100 pts

Code / Notes

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, sum;
    cin >> n >> sum;
    vector<pair<int,int>>A(n);
    for (int x = 0; x < n; x++) {
        cin >> A[x].first;
        A[x].second = x; 
    }
    sort(A.begin(), A.end());
    int i = 0;
    int r = n-1;
    int ans = 0;
    while (i < r) {
        if (A[i].first + A[r].first == sum) {
            cout << A[i].second + 1 << ' ' << A[r].second + 1;
            ans = 1;
            break;
        } 
        else if (A[i].first + A[r].first > sum) r--;
        else if (A[i].first + A[r].first < sum) i++;
    }
    if (ans == 0) {
        cout << "IMPOSSIBLE" << endl;
    }
}

Last updated 1 month, 2 weeks ago


« Back to problem