Sum of Two Values

Problem Link

Solution by Mr.Olimpia100 pts

Code / Notes

#include <bits/stdc++.h>
using namespace std;

int main() {
    int N, S;
    cin >> N >> S;

    vector<pair<int, int>> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i].first;
        A[i].second = i;
    }

    sort(A.begin(), A.end());
    int r = 0;
    int s = N-1;
    int ans = 0;
    while(r<s){
        if(A[r].first+A[s].first<S){
            r++;
        }
        else if(A[r].first+A[s].first>S){
            s--;
        }
        else{
            ans=1;
            cout << A[r].second+1 << " " << A[s].second+1;
            break;
        }
    }
    if(ans==0){
        cout << "IMPOSSIBLE";
    }
}

Last updated 1 month, 2 weeks ago


« Back to problem