-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEQUAL.java
126 lines (110 loc) · 3.95 KB
/
EQUAL.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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Equal
Given an array A of integers, find the index of values that satisfy A + B = C + D,
where A,B,C & D are integers values in the array Note:
1) Return the indices `A1 B1 C1 D1`, so that
A[A1] + A[B1] = A[C1] + A[D1]
A1 < B1, C1 < D1
A1 < C1, B1 != D1, B1 != C1
2) If there are more than one solutions,
then return the tuple of values which are lexicographical smallest.
Assume we have two solutions
S1 : A1 B1 C1 D1 ( these are values of indices int the array )
S2 : A2 B2 C2 D2
S1 is lexicographically smaller than S2 iff
A1 < A2 OR
A1 = A2 AND B1 < B2 OR
A1 = A2 AND B1 = B2 AND C1 < C2 OR
A1 = A2 AND B1 = B2 AND C1 = C2 AND D1 < D2
Example:
Input: [3, 4, 7, 1, 2, 9, 8]
Output: [0, 2, 3, 5] (O index)
If no solution is possible, return an empty list.
*/
package Hashing;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class EQUAL {
public static void main(String[] args) {
ArrayList<Integer> A = new ArrayList<>();
A.add(3);
A.add(4);
A.add(7);
A.add(1);
A.add(2);
A.add(9);
A.add(8);
System.out.println(equal(A));
ArrayList<Integer> B = new ArrayList<>();
B.add(9); B.add(5); B.add(4);
B.add(9); B.add(3); B.add(6);
B.add(8);B.add(7);B.add(1);
B.add(2);B.add(8);B.add(7);
B.add(2); B.add(9); B.add(7);
B.add(1);B.add(3);B.add(9);B.add(7);
B.add(8);B.add(1);B.add(0);B.add(5);B.add(5);
System.out.println(equal(B));
}
static class pair{
int first, second;
pair(int f,int s)
{
first = f; second = s;
}
};
public static ArrayList<Integer> equal(ArrayList<Integer> arr) {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
HashMap<Integer,pair> map = new HashMap<Integer,pair>();
int n = arr.size();
// Traverse through all possible pairs of arr[]
for (int i=0; i<n; ++i){
for (int j=i+1; j<n; ++j){
// If sum of current pair is not in hash,
// then store it and continue to next pair
int sum = arr.get(i)+arr.get(j);
if (!map.containsKey(sum)) {
map.put(sum, new pair(i, j));
}else {
// Else (Sum already present in hash){
// Find previous pair
pair p = map.get(sum);
// Since array elements are distinct, we don't
// need to check if any element is common among pairs
HashSet<Integer> hs = new HashSet<>();
if((p.first != i) && (p.second != j) && (p.second != i) && (p.first != j)) {
ArrayList<Integer> tuple = new ArrayList<>();
tuple.add(p.first);
tuple.add(p.second);
tuple.add(i);
tuple.add(j);
res.add(tuple);
}
}
}
}
res.sort((t1,t2 ) -> {
int t1_1 = t1.get(0);
int t2_1 = t2.get(0);
if(t1_1 == t2_1) {
int t1_2 = t1.get(1);
int t2_2 = t2.get(1);
if(t1_2 == t2_2) {
int t1_3 = t1.get(2);
int t2_3 = t2.get(2);
if(t1_3 == t2_3) {
int t1_4 = t1.get(3);
int t2_4 = t2.get(3);
return Integer.compare(t1_4, t2_4);
}else{
return Integer.compare(t1_3, t2_3);
}
}else{
return Integer.compare(t1_2, t2_2);
}
}else{
return Integer.compare(t1_1, t2_1);
}
});
return res.get(0);
}
}