-
Notifications
You must be signed in to change notification settings - Fork 1
/
FourSum.java
203 lines (182 loc) · 8.1 KB
/
FourSum.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?
* Find all unique quadruplets in the array which gives the sum of target.
*
* Note:
* Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
* The solution set must not contain duplicate quadruplets.
* For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
*
* A solution set is:
* (-1, 0, 0, 1)
* (-2, -1, 1, 2)
* (-2, 0, 0, 2)
*/
package com.alg.leetup;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
class TestCaseFourSum {
int[] nums;
int target;
List<List<Integer>> triplets;
TestCaseFourSum(int[] nums, int target, List<List<Integer>> triplets) {
this.nums = nums;
this.triplets = triplets;
this.target = target;
}
public int[] getNums() {
return nums;
}
public void setNums(int[] nums) {
this.nums = nums;
}
public int getTarget() {
return target;
}
public void setTarget(int target) {
this.target = target;
}
public List<List<Integer>> getTriplets() {
return triplets;
}
public void setTriplets(List<List<Integer>> triplets) {
this.triplets = triplets;
}
public String toString() {
return "Test case for:" + Arrays.toString(getNums()) + " with target:" + getTarget() + " has expected " + Arrays.toString(triplets.toArray());
}
}
/**
* @author rbaral
*
*/
public class FourSum {
private static DateFormat format = new SimpleDateFormat("YYYY-M-d:H:m:s:S");
private static List<TestCaseFourSum> testCases = new ArrayList<TestCaseFourSum>();
private static List<String> failedCases = new ArrayList<String>();
private static List<List<Integer>> results = new ArrayList<List<Integer>>();
/**
* @param args
*/
public static void main(String[] args) {
boolean isTestMode = false;
if (isTestMode) {
performTest();
} else {
//System.out.println(format.format(new Date()));
int target = 0;
int nums[] = new int[]{1, 0, -1, 0, -2, 2};
nums = new int[]{-1, 0, 1, 2, -1, -4};
target = -1;
System.out.println(Arrays.toString(fourSum(nums, target).toArray()));
//System.out.println(format.format(new Date()));
}
}
public static List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> triplets = new ArrayList<List<Integer>>();
Integer triplet[];
if (nums == null || nums.length < 4)//if null array or if the size of the array is less than 3, return blank
{
return triplets;
} else if (nums.length == 4) {//if only three elements and the sum is zero, then return the sorted integers, else return blank
if (nums[0] + nums[1] + nums[2] + nums[3] == target) {
triplet = new Integer[]{nums[0], nums[1], nums[2], nums[3]};
Arrays.sort(triplet);
triplets.add(Arrays.asList(triplet));
}
return triplets;
} else {
Arrays.sort(nums);//sort the elements in ascending order
System.out.println("OPERATING WITH:" + Arrays.toString(nums) + " and target:" + target);
int low, high, tempSum, tempDiff;//define the two pointers to be used on the two ends of the array
//we iterate till the third last element as after that we won't find any triplets
for (int i = 0; i < nums.length - 3; i++) {
for (int j = i + 1; j < nums.length - 2; j++) {
low = j + 1;
high = nums.length - 1;//check from the next index of the current element
if (nums[j] >= 0 && nums[low] > nums[j])//if current element is zero (or greater) and the next index has greater value, then we won't find the triples for zero (or greater) as the array is sorted in increasing order
{
continue;
} else {
//avoid duplicate solutions
//if(j==0 || nums[j]>nums[j-1]){
while (low < high) {
tempSum = nums[low] + nums[high] + nums[j];
tempDiff = target - nums[i];
if (tempSum == tempDiff) {
triplet = new Integer[]{nums[i], nums[j], nums[low], nums[high]};
triplets.add(Arrays.asList(triplet));
low++;
high--;
//avoid duplicate solutions
//while (low < high && nums[high] == nums[high + 1])
// high--;
//avoid duplicate solutions
//while (low < high && nums[low] == nums[low - 1])
// low++;
} else if (tempSum < tempDiff) {
low++;
} else {
high--;
}
}
//}
}
}
}
}
return triplets;
}
public static void performTest() {
int nums[] = new int[]{1, 0, -1, 0, -2, 2};
int target = 0;
results = new ArrayList<List<Integer>>();
results.add(Arrays.asList(new Integer[]{-2, -1, 1, 2}));
results.add(Arrays.asList(new Integer[]{-2, 0, 0, 2}));
results.add(Arrays.asList(new Integer[]{-1, 0, 0, 1}));
//testCases.add(new TestCaseFourSum(nums, target, results));
nums = new int[]{-1, 0, 1, 2, -1, -4};
target = -1;
results = new ArrayList<List<Integer>>();
results.add(Arrays.asList(new Integer[]{-4, 0, 1, 2}));
results.add(Arrays.asList(new Integer[]{-1, -1, 0, 1}));
testCases.add(new TestCaseFourSum(nums, target, results));
List<List<Integer>> expected = new ArrayList<List<Integer>>();
for (TestCaseFourSum testCase : testCases) {
expected = testCase.getTriplets();
results = fourSum(testCase.getNums(), testCase.getTarget());
if (results.size() != expected.size()) {
String msg1 = "Failed for:{" + Arrays.toString(testCase.getNums()) + "} with target:" + testCase.getTarget() + " got:[", msg2 = " Expected: ";
for (int k = 0; k < results.size(); k++) {
msg1 += Arrays.toString(expected.get(k).toArray());
}
for (int k = 0; k < expected.size(); k++) {
msg2 += Arrays.toString(expected.get(k).toArray());
}
failedCases.add(msg1 + "] " + msg2);
} else {
for (int i = 0; i < results.size(); i++) {
//System.out.println(Arrays.toString(result.get(i).toArray()));
//System.out.println("..."+Arrays.toString(expected.get(i).toArray()));
if (!Arrays.toString(results.get(i).toArray()).equals(Arrays.toString(expected.get(i).toArray()))) {
String msg1 = "Failed for:{" + Arrays.toString(testCase.getNums()) + "} with target:" + testCase.getTarget() + " got:", msg2 = " Expected: ";
for (int k = 0; k < results.size(); k++) {
msg1 += Arrays.toString(results.get(k).toArray());
msg2 += Arrays.toString(expected.get(k).toArray());
}
failedCases.add(msg1 + " " + msg2);
break;
}
}
}
}
System.out.println("TEST RESULT, Passed:" + (testCases.size() - failedCases.size()) + " failed:" + failedCases.size());
for (String failure : failedCases) {
System.err.println(failure);
}
}
}