forked from mayankchaudhary26/HacktoberFest-Practice-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeSum.java
More file actions
74 lines (62 loc) Β· 1.97 KB
/
ThreeSum.java
File metadata and controls
74 lines (62 loc) Β· 1.97 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
70
71
72
73
74
import java.util.*;
public class ThreeSum {
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static int segregate(int[] arr, int pivot, int si, int ei) {
swap(arr, pivot, ei);
int p = si - 1, itr = si;
while (itr <= ei) {
if (arr[itr] <= arr[ei])
swap(arr, ++p, itr);
itr++;
}
return p;
}
public static void quickSort(int[] arr, int si, int ei) {
if (si > ei)
return;
int pivot = ei;
int pIdx = segregate(arr, pivot, si, ei);
quickSort(arr, si, pIdx - 1);
quickSort(arr, pIdx + 1, ei);
}
// int[] arr = {2,3,4,5,6};
public static ArrayList<int[]> twoSum(int[] arr, int tar, int si, int ei) {
ArrayList<int[]> ans = new ArrayList<>();
while (si < ei) {
int sum = arr[si] + arr[ei];
if (sum == tar)
ans.add(new int[] { arr[si++], arr[ei--] });
else if (sum > tar)
ei--;
else
si++;
}
return ans;
}
public static ArrayList<int[]> threeSum(int[] arr, int tar, int si, int ei) {
ArrayList<int[]> ans = new ArrayList<>();
for (int i = si; i <= ei; i++) {
ArrayList<int[]> smallAns = twoSum(arr, tar - arr[i], i + 1, ei);
for (int[] a : smallAns) {
ans.add(new int[] { arr[i], a[0], a[1] });
}
}
return ans;
}
public static void threeSum(int[] arr, int tar) {
int n = arr.length;
quickSort(arr, 0, n - 1);
ArrayList<int[]> ans = threeSum(arr, tar, 0, n - 1);
for (int[] a : ans) {
System.out.println(a[0] + ", " + a[1] + ", " + a[2]);
}
}
public static void main(String[] args){
int[] arr = { -2, -3, 7, 5, 8, 15, 3, 2, 9, 10, 19 };
threeSum(arr, 25);
}
}