-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathThree_Sum.java
More file actions
45 lines (38 loc) · 1.22 KB
/
Three_Sum.java
File metadata and controls
45 lines (38 loc) · 1.22 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
import java.util.*;
class GFG {
// returns true if there is triplet
// with sum equal to 'sum' present
// in A[]. Also, prints the triplet
static boolean find3Numbers(int A[],
int arr_size, int sum)
{
// Fix the first element as A[i]
for (int i = 0; i < arr_size - 2; i++) {
// Find pair in subarray A[i+1..n-1]
// with sum equal to sum - A[i]
HashSet<Integer> s = new HashSet<Integer>();
int curr_sum = sum - A[i];
for (int j = i + 1; j < arr_size; j++)
{
if (s.contains(curr_sum - A[j]))
{
System.out.printf("Triplet is %d,
%d, %d", A[i],
A[j], curr_sum - A[j]);
return true;
}
s.add(A[j]);
}
}
// If we reach here, then no triplet was found
return false;
}
/* Driver code */
public static void main(String[] args)
{
int A[] = { 1, 4, 45, 6, 10, 8 };
int sum = 22;
int arr_size = A.length;
find3Numbers(A, arr_size, sum);
}
}