Skip to main content

Posts

Showing posts with the label hashmap

Count pair with given sum [EASY-Hashmap]-Very Important

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(...