Skip to content

Instantly share code, notes, and snippets.

@jaimemin
Created October 21, 2019 07:36
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector<int> v(N);
for (int i = 0; i < N; i++)
{
cin >> v[i];
}
sort(v.begin(), v.end());
int i = 0, j = v.size() - 1;
int total = 2e9;
pair<int, int> result;
while (i < j)
{
int a = v[i];
int b = v[j];
if (abs(a + b) < total)
{
total = abs(a + b);
result.first = v[i];
result.second = v[j];
}
if (a + b < 0)
{
i++;
}
else
{
j--;
}
}
cout << result.first << " " << result.second << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment