-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathtriplet_sum.java
76 lines (65 loc) · 3.42 KB
/
triplet_sum.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
/*
Given an array of integers, nums, and an integer value, target,
determine if there are any three integers in nums whose sum equals the target.
Return TRUE if three such integers are found in the array. Otherwise, return FALSE.
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
We sort the input array in non-decreasing order using the Arrays.sort() method, and then loop through
the array using a for loop. We use two pointers, one starting from the left and one starting from the right,
to find triplets that sum up to the target sum. For each iteration of the loop, we set the left pointer to
the index immediately to the right of the current index, and the right pointer to the index of the last
element in the array. We then move the left and right pointers towards the center, checking at each step
whether the triplet formed by the current indices sums up to the target sum. If it does, we add the triplet
to the result list and continue searching for other triplets. If the sum is less than the target sum, we
move the left pointer towards the center to find larger numbers. If the sum is greater than the target
sum, we move the right pointer towards the center to find smaller numbers.
Finally, we print out the result list using a nested for loop to iterate through each triplet and each element in each triplet.
*/
public class ThreeNumberSum {
public static List<List<Integer>> threeNumberSum(int[] nums, int targetSum) {
Arrays.sort(nums); // Sort the input array in non-decreasing order
List<List<Integer>> triplets = new ArrayList<>();
// Loop through the array
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1;
int right = nums.length - 1;
// While the left index is less than the right index
while (left < right) {
int currentSum = nums[i] + nums[left] + nums[right];
if (currentSum == targetSum) {
// If the current triplet sums up to the target sum, add it to the result list
triplets.add(Arrays.asList(nums[i], nums[left], nums[right]));
// Move the left and right indices towards the center to find other triplets
left++;
right--;
} else if (currentSum < targetSum) {
// If the current triplet sums up to less than the target sum, move the left index towards the center to find larger numbers
left++;
} else {
// If the current triplet sums up to more than the target sum, move the right index towards the center to find smaller numbers
right--;
}
}
}
return triplets;
}
public static void main(String[] args) {
// Example usage
int[] nums = { 12, 3, 1, 2, -6, 5, -8, 6 };
int targetSum = 0;
List<List<Integer>> triplets = threeNumberSum(nums, targetSum);
for (List<Integer> triplet : triplets) {
System.out.print("[");
for (int i = 0; i < triplet.size(); i++) {
System.out.print(triplet.get(i));
if (i < triplet.size() - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}
}
}