@aayushaggarwal although i have not participated in this contest but i would like to tell you O(nlog(n)) approached which is developed by me because i first thought that O(n^2) will not pass the test data.
so here is my approach
.firstly sort the given array
.for this mod(ai+aj-k) to be minimal you required (ai+aj) close to k
now start from the first element do this binary search for k-a[i] you can use lower_bound STL function now this number or the number just before this number are the only two number which can give ai+aj close to k now count the occurence of of this number if anyone of these may be your answer that can be easily find in logn using binary search but there is a flow in this algorithm you have to find the number of unordered pairs which can be find by avoiding duplicates This can also be done in a clever manner .whenever you do binary_search do it between the element next to the current element and the last element of the array .look at this code its a tested code
#include
#include<bits/stdc++.h>
using namespace std;
vector arr;
#define all(b) b.begin(),b.end()
int main(){
int t;
cin>>t;
while(t–){
int n,k;
cin>>n>>k;
arr.resize(n);
for(int i=0;i<n;i++)
cin>>arr[i];
int ans=INT_MAX,ans_count=0,num1;
sort(all(arr));
vector :: iterator it,it1;
for(it=arr.begin();it!=arr.end();++it){
int num=k-*it;
it1=lower_bound(it+1,arr.end(),num);
if(it1!=arr.end()){
num1=*it1;
if(ans>abs(*it+num1-k)){
ans=abs(*it+num1-k);
ans_count=upper_bound(it+1,arr.end(),num1)-lower_bound(it+1,arr.end(),num1);
}else if(ans==abs(*it+num1-k)){
ans_count+=upper_bound(it+1,arr.end(),num1)-lower_bound(it+1,arr.end(),num1);
}
}
it1--;
if(it1!=it){
num1=*it1;
if(ans>abs(*it+num1-k)){
ans=abs(*it+num1-k);
ans_count=upper_bound(it+1,arr.end(),num1)-lower_bound(it+1,arr.end(),num1);
}else if(ans==abs(*it+num1-k)){
ans_count+=upper_bound(it+1,arr.end(),num1)-lower_bound(it+1,arr.end(),num1);
}
}
}
cout<<ans<<" "<<ans_count<<endl;
}
return 0;
}