-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum2Plus2.java
More file actions
41 lines (35 loc) · 920 Bytes
/
Copy pathSum2Plus2.java
File metadata and controls
41 lines (35 loc) · 920 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
38
39
40
41
import java.util.ArrayList;
import java.util.HashMap;
public class Sum2Plus2 {
public static int[] Sum4DifferenceIndex(int[] a) {
HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
for(int i = 0; i < a.length;i++) {
for(int j=i+1; j< a.length; j++) {
int sum = a[i]+a[j];
if(map.containsKey(sum)) {
int p = map.get(sum).get(0);
int q = map.get(sum).get(1);
if(i!=q && j!=p) {
//System.out.print(true);
return new int[]{i,j,p,q};
}
}
else {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(i);
list.add(j);
map.put(sum, list);
}
}
}
//System.out.print(false);
return new int[] {-1};
}
public static void main(String[] args) {
int[] a = new int[] {1,11,4,2,3};
int[] result = Sum4DifferenceIndex(a);
for(int index: result) {
System.out.print(index+ " ");
}
}
}