-
Notifications
You must be signed in to change notification settings - Fork 1
/
pairs.java
executable file
·44 lines (35 loc) · 1.08 KB
/
pairs.java
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
38
39
40
41
42
43
44
//Count pairs with given sum
// Input : arr[] = {1, 5, 7, -1, 5},
// sum = 6
// Output : 3
// Pairs with sum 6 are (1, 5), (7, -1) &
// (1, 5)
import java.util.*;
class pairs{
public static void main(String args[]){
int arr[]={3,3,3,1};
int target_sum=6;
System.out.println("Count of pairs= "+count(arr,target_sum));
}
static int count(int arr[], int target_sum){
Map<Integer,Integer> map=new HashMap<Integer,Integer>();
int count_twice=0;
// Creating a frequency table here
for (int e:arr){
if(map.containsKey(e))
map.put(e,map.get(e)+1);
else
map.put(e,1);
}
for (int e:arr){
if(map.containsKey(target_sum-e)) // iterate through each element and increment the
count_twice+=map.get(target_sum-e); // count (Notice that every pair is counted twice)
//if (e,e) pair of same element satisfies the condition,
// then we need to ensure that the count is
// // decreased by one.
if(target_sum-e==e)
count_twice--;
}
return count_twice/2; // count_twice always even
}
}