-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashmaps:Pair Sum to 0
43 lines (32 loc) · 1.03 KB
/
Hashmaps:Pair Sum to 0
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
import java.util.HashMap;
public class Solution {
public static void PairSum(int[] input, int size) {
if(size==0)
return;
HashMap<Integer,Integer> h=new HashMap<>();
for(int i=0;i<size;i++)
{
if(h.containsKey(-input[i]) && h.get(-input[i])!=0){
int count=h.get(-input[i]);
while(count!=0)
{ if(input[i]>0)
System.out.println(-input[i] +" "+ input[i]);
else
System.out.println(input[i] +" "+( -input[i]));
count--;
}
if(h.containsKey(input[i]))
h.put(input[i],h.get(input[i])+1);
else
h.put(input[i],1);
}
else{
if(h.containsKey(input[i])){
h.put(input[i],h.get(input[i])+1);
}else{
h.put(input[i] ,1);
}
}
}
}
}