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
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution{
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
int n=sc.nextInt();
int sum=sc.nextInt();
int [] a= new int[n];
for(int j=0;j<n;j++){
a[j]=sc.nextInt();
}
System.out.println(Countpairs(a,sum,n));
}
}
public static int Countpairs(int[] arr, int sum, int n){
HashMap<Integer, Integer> hm = new HashMap<>();
// Store counts of all elements in map hm
for (int i=0; i<n; i++){
// initializing value to 0, if key not found
if(!hm.containsKey(arr[i]))
hm.put(arr[i],0);
hm.put(arr[i], hm.get(arr[i])+1);
}
int twice_count = 0;
// iterate through each element and increment the
// count (Notice that every pair is counted twice)
for (int k=0; k<n; k++)
{
if(hm.get(sum-arr[k]) != null)
twice_count += hm.get(sum-arr[k]);
// if (arr[i], arr[i]) pair satisfies the condition,
// then we need to ensure that the count is
// decreased by one such that the (arr[i], arr[i])
// pair is not considered
if (sum-arr[k] == arr[k])
twice_count--;
}
// return the half of twice_count
return (twice_count/2);
}
}
EXECUTION TIME: 0.11s
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
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution{
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
int n=sc.nextInt();
int sum=sc.nextInt();
int [] a= new int[n];
for(int j=0;j<n;j++){
a[j]=sc.nextInt();
}
System.out.println(Countpairs(a,sum,n));
}
}
public static int Countpairs(int[] arr, int sum, int n){
HashMap<Integer, Integer> hm = new HashMap<>();
// Store counts of all elements in map hm
for (int i=0; i<n; i++){
// initializing value to 0, if key not found
if(!hm.containsKey(arr[i]))
hm.put(arr[i],0);
hm.put(arr[i], hm.get(arr[i])+1);
}
int twice_count = 0;
// iterate through each element and increment the
// count (Notice that every pair is counted twice)
for (int k=0; k<n; k++)
{
if(hm.get(sum-arr[k]) != null)
twice_count += hm.get(sum-arr[k]);
// if (arr[i], arr[i]) pair satisfies the condition,
// then we need to ensure that the count is
// decreased by one such that the (arr[i], arr[i])
// pair is not considered
if (sum-arr[k] == arr[k])
twice_count--;
}
// return the half of twice_count
return (twice_count/2);
}
}
EXECUTION TIME: 0.11s
Comments
Post a Comment