# Get the Triplets in an Array Whose Sum is Equal to a Specific Number in Java

In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to get all the triplets in the array whose sum is equal to a specific number. Here triplets refer to any three elements of the array which meet a specific condition.

Note − The array must be an integer array.

Let’s explore the article to see how it can be done by using Java programming language.

### To Show You Some Instances

#### Instance-1

Suppose the original array is {3, 4, 25, 6, 13, 9}.


After performing the triplet operation to get sum equal to 37, the result will be −

The triplet is 3, 25, 9.


#### Instance-2

Suppose the original array is {11, 4, 5, 6, 21, 29}


After performing the triplet operation to get sum equal to 22, the result will be −

The triplet is 11, 5, 6.


#### Instance-3

Suppose the original array is {8, 2, 15, 6, 11, 1}


After performing the triplet operation to get sum equal to 14, the result will be −

The triplet is 2, 11, 1.


### Algorithm

• Step 1 − Declare and initialize an integer array.

• Step 2 − Declare the sum whose triplet needs to be found out.

• Step 3 − Initialize three for loops one inside another to find out the triplet element.

• Step 4 − Check if the three elements are equal to the sum.

• Step 5 − Print the triplet.

### Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length

Below refers to the syntax of it −

array.length


where, ‘array’ refers to the array reference.

### Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Initialization of Array

• By Using User Defined Method

Let’s see the program along with its output one by one.

## Approach-1: By Using Static Initialization of Array

### Example

In this approach, array elements will be initialized in the program. Then as per the algorithm get all the triplets in the array whose sum is equal to a specific number.

public class Main{
//main method
public static void main(String[] args){

//Declare and initialize the array elements
int arr[] = {11, 4, 5, 6, 21, 29};

//a particular number whose triplet meeds to be find out
int sum = 15;

// Fix the first element as a[i]
for (int i = 0; i < arr.length - 2; i++) {

// Fix the second element as a[j]
for (int j = i + 1; j < arr.length - 1; j++) {
// Now look for the third number in an array
for (int k = j + 1; k < arr.length; k++) {
if (arr[i] + arr[j] + arr[k] == sum) {

//printing the triplet
System.out.print("The triplet is " + arr[i] + ", " + arr[j] + ", " + arr[k] + ".");
}
}
}
}
}
}


### Output

The triplet is 4, 5, 6.


## Approach-2: By Using User Defined Method

### Example

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside the method as per the algorithm get all the triplets in the array whose sum is equal to a specific number.

public class Main{
//main method
public static void main(String[] args){

//Declare and initialize the array elements
int arr[] = { 8, 2, 15, 6, 11, 1 };

//calling user defined method
triplet(arr);
}

//user defined method
public static void triplet(int []arr){

//a particular number whose triplet meeds to be find out
int sum = 14;

// Fix the first element as a[i]
for (int i = 0; i < arr.length - 2; i++) {
// Fix the second element as a[j]
for (int j = i + 1; j < arr.length - 1; j++) {
// Now look for the third number in an array
for (int k = j + 1; k < arr.length; k++) {
if (arr[i] + arr[j] + arr[k] == sum) {

//printing the triplet
System.out.print("The triplet is " + arr[i] + ", " + arr[j] + ", " + arr[k] + ".");
}
}
}
}
}
}


### Output

The triplet is 2, 11, 1.


In this article, we explored how to get all the triplets in the array whose sum is equal to a specific number in Java.

Updated on: 19-Jan-2023

184 Views