-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeSum.java
34 lines (30 loc) · 883 Bytes
/
ThreeSum.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
package com.aman.data_structures;
import java.util.Arrays;
public class ThreeSum {
public static void main(String[] args) {
int[] arr = {2, 4, 3, 7, 1, 8, 9, 0};
int target = 6;
threeSum(arr, target);
}
public static void threeSum(int[] arr, int target){
Arrays.sort(arr);
for(int i = 0; i < arr.length - 2; i++){
int j = i + 1;
int k = arr.length - 1;
while(j < k){
int sum = arr[i] + arr[j] + arr[k];
if(sum == target){
System.out.println("{" + arr[i] + "," + arr[j] + "," + arr[k] + "}");
j++;
k--;
}
else if(sum < target){
j++;
}
else{
k--;
}
}
}
}
}