forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqual.java
More file actions
69 lines (56 loc) · 1.73 KB
/
Equal.java
File metadata and controls
69 lines (56 loc) · 1.73 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package Hashing;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* Author - archit.s
* Date - 30/10/18
* Time - 12:04 PM
*/
public class Equal {
class Pair{
int key;
int value;
public Pair(int key, int value) {
this.key = key;
this.value = value;
}
}
public ArrayList<Integer> equal(ArrayList<Integer> A) {
ArrayList<Integer> r = new ArrayList<>();
Map<Integer,Pair> map = new HashMap<>();
for(int i=0;i<A.size();i++){
for(int j=i+1;j<A.size();j++){
int sum = A.get(i) + A.get(j);
if(map.containsKey(sum)){
Pair p = map.get(sum);
if(p.key != i && p.value!= i && p.value!= j && p.key != j){
ArrayList<Integer> temp = new ArrayList<>();
temp.add(p.key);
temp.add(p.value);
temp.add(i);
temp.add(j);
if(r.size()==0){
r = temp;
}
else{
for(int k=0;k<4;k++){
if(r.get(k) < temp.get(k)){
break;
}
else if(r.get(k) > temp.get(k)){
r = temp;
}
}
}
}
}
else{
Pair p = new Pair(i,j);
map.put(sum,p);
}
}
}
return r;
}
}