-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcPairsSum.java
More file actions
37 lines (35 loc) · 740 Bytes
/
cPairsSum.java
File metadata and controls
37 lines (35 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
//count pairs with given sum
public class cPairsSum {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int j=0;j<n;j++)
{
int t=sc.nextInt();
int sum=sc.nextInt();
int arr[]=new int[t];
for(int i=0;i<t;i++)
{
arr[i]=sc.nextInt();
}
getPairsCount(arr,sum);
}
}
public static void getPairsCount(int[] arr,int B)
{
int n = arr.length, i=0, j=n-1, c=0;
while(i<j){
int sum = arr[i] + arr[j];
if(sum==B ){
c++;
i++;
}
else if(sum>B)
j--;
else
i++;
}
System.out.println(c);
}
}