-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriplet_sum.java
More file actions
36 lines (29 loc) · 1011 Bytes
/
Triplet_sum.java
File metadata and controls
36 lines (29 loc) · 1011 Bytes
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
package Arrays;
public class Triplet_sum {
boolean find3Numbers(int A[], int arr_size, int sum)
{
int l, r;
// Fix the first element as A[i]
for (int i = 0; i < arr_size - 2; i++) {
// Fix the second element as A[j]
for (int j = i + 1; j < arr_size - 1; j++) {
// Now look for the third number
for (int k = j + 1; k < arr_size; k++) {
if (A[i] + A[j] + A[k] == sum) {
System.out.print("Triplet is " + A[i] + ", " + A[j] + ", " + A[k]);
return true;
}
}
}
}
// If we reach here, then no triplet was found
return false;
}
public static void main(String[] args) {
Triplet_sum triplet = new Triplet_sum();
int A[] = { 1, 4, 45, 6, 10, 8 };
int sum = 22;
int arr_size = A.length;
triplet.find3Numbers(A, arr_size, sum);
}
}