-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairSumHashMap.java
More file actions
51 lines (45 loc) · 1.32 KB
/
PairSumHashMap.java
File metadata and controls
51 lines (45 loc) · 1.32 KB
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
45
46
47
48
49
50
51
import java.util.HashMap;
public class Solution {
public static void PairSum(int[] arr, int size) {
/* Your class should be named Solution
* Don't write main().
* Don't read input, it is passed as function argument.
* Print the desired output and don't return anything.
* Taking input is handled automatically.
*/
HashMap<Integer,Integer> map=new HashMap();
for(int i=0;i<size;i++)
{
if(map.containsKey(arr[i]))
{
int f=map.get(arr[i]);
map.put(arr[i],f+1);
}
else
map.put(arr[i],1);
}
for(int i=0;i<size;i++)
{
int find = 0 - arr[i];
if(find<0)
{
if(map.containsKey(find))
{
for(int k=0;k<map.get(find);k++)
{
if(find<0)
{
System.out.print(find + " " + arr[i]);
System.out.println();
}
else
{
System.out.print( arr[i]+ " " +find );
System.out.println();
}
}
}
}
}
}
}