-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtripletSum.java
57 lines (50 loc) · 1.2 KB
/
tripletSum.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
/*Triplet Sum
You have been given a random integer array/list(ARR) and a number X. Find and return the number of triplets in the array/list which sum to X.*/
package Array_1D;
import java.util.Scanner;
public class tripletSum
{
public static int searchTripletSum(int arr[] , int x)
{
int count = 0;
for (int i = 0;i < arr.length - 1; i++)
{
for (int j = i + 1; j < arr.length; j++)
{
for (int k = j + 1; k < arr.length; k++)
{
if (arr[i] + arr[j] +arr[k] == x)
{
count++;
}
}
}
}
return count;
}
public static void printArray(int arr[])
{
for(int i= 0; i<arr.length; i++)
{
System.out.print(arr[i]+ ", ");
}
System.out.println();
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array : ");
int size = sc.nextInt();
System.out.print("Enter the number for search : ");
int x = sc.nextInt();
int[] input = new int[size];
for(int i= 0; i<size; i++)
{
System.out.print("Enter the element at "+ i + "th term : ");
input[i] = sc.nextInt();
}
printArray(input);
int s = searchTripletSum(input, x );
System.out.println("triple sum is "+ s);
}
}