Given an array of integers, and an integer
‘K’, find the count of pairs of elements in the array whose sum is equal to
'K'.
Input:
First line of the input contains an
integer T, denoting the number of test cases. Then T test cases follow. Each
test case consists of two lines. First line of each test case contains 2 space
separated integers N and K denoting the size of array and the sum respectively.
Second line of each test case contains N space separated integers denoting the
elements of the array.
Output:
Print the count of pairs of elements in
the array whose sum is equal to the K.
Constraints:
1<=T<=50
1<=N<=50
1<=K<=50
1<=A[i]<=100
Example:
Input
2
4 6
1 5 7 1
4 2
1 1 1 1
Output
2
6
Solution:
#include <iostream>
#include <map>
using namespace std;
int main()
{
int n;
cin>>n;
while(n--)
{
map<int,int> m;
int i,x,t,c=0;
cin>>x>>t;
int a[x];
for(i=0;i<x;i++)
{
cin>>a[i];
m[a[i]]++;
}
for(i=0;i<x;i++)
{
c+=m[t-a[i]];
if(t-a[i]==a[i])
c-=1;
}
cout<<c/2<<endl;
}
return 0;
}