 # SPRTDAY - Editorial

Induction Code Battle

Author: Tejus
Tester: Tejus
Editorialist: OM Bindal

CAKEWALK

Simple Maths

# PROBLEM:

Pairs all given elements such that their sum is X(given in input).

# QUICK EXPLANATION:

Sort all elements and use the two-pointer technique from the first and last elements.

# EXPLANATION:

First, if no. of elements is odd then no answer is possible(trivial). Now we have even no. of elements. Let’s sort the elements and check if the first and last elements make required pairs. Similarly, check for second and second-last and so on. This is because if the sum of first and last element doesn’t make the required sum then one of them or both will be left unpaired.

# SOLUTIONS:

Editorialist's Solution
``````#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vpii vector<pair<int,int>>
#define pii pair<int,int>
#define ff first
#define ss second
#define endl "\n"
#define print(x) cout<<x<<endl
#define printt(x,y) cout<<x<<" "<<y<<"\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define vvi vector<vector<int>>
#define sz(x) (int)x.size()
#define maxq priority_queue<int>
#define minq priority_queue<int,vector<int>,greater<int>>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
const int mod = 1e9 + 7;
const int N = 200005;

void solve() {
int n, x;
cin >> n >> x;

vi v(n);
for (int i = 0; i < n; i++)
cin >> v[i];

if (n % 2 != 0) {
cout << -1 << endl;
return;
}

sort(all(v));
int i = 0, j = n - 1;
while (i < j) {
if (v[i] + v[j] != x) {
cout << -1 << endl;
return;
}
i++, j--;
}
cout << "Successfully done\n";
}

int32_t main() {
fastio;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
``````