Count such pairs in array whose sum is equal to particular number - codewithshiva

Latest

Search Bar

Friday, July 2, 2021

Count such pairs in array whose sum is equal to particular number

 Count such pairs in array whose sum is equal to particular number

Description

You are given an array A of N integers along with a target integer. Your task is to find out the number of pairs of integers present in the array whose sum is equal to the target value.

Input

Input Format :

First line contains 2 integers: N and the target respectively.

Second line contains N integers which are the elements of the array.

Constraints :

N<100

Output

Print one number which is number of such pairs.

Sample Input 1 

5 9
3 0 6 2 7

Sample Output 1

2

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
int sum = sc.nextInt();
int []arr = new int[len];

for (int i =0;i<len;i++){
arr[i]=sc.nextInt();
}
int count = 0;
for (int j = 0 ; j< len; j++){
for (int k = 0;k<len;k++){
if(arr[j]+arr[k]==sum && j!=k){
count++;
}
}
}
System.out.println(count/2);

}
}

No comments:

Post a Comment