In the previous article, we have seen Java Program to Check if an Array of Integers without 0 and 1
In this article we will see find all the Triplets Where Sum of All the Three Elements are Equal to a Specified Number.
Java Program to Find all the Triplets Where Sum of All the Three Elements are Equal to a Specified Number
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to find all the Triplets Where Sum of All the Three Elements are Equal to a Specified Number
Method-1: Java Program to Find all the Triplets Where Sum of All the Three Elements are Equal to a Specified Number By Static Initialization of Array Elements
Approach:
- Declare and initialize an array.
- Create three nested for loops.
- First loop runs from start to end (counter i), second loop runs from i+1 to end (counter j) and third loop runs from j+1 to end (loop counter k)
- Find the sum of ith, jth and kth element. If the sum is equal to given sum. Print the triplet and break.
- If there is no triplet, then print that no triplet exists.
Program:
public class Main
{
public static void main(String[] args)
{
int[] arr = { 2, 5, 7, 9, 3, -2, 1 };
int sum = 14;
System.out.println("Finding triplets whose sum are equal to : "+sum);
System.out.println("The triplets are : ");
findTriplet(arr, sum);
}
public static void findTriplet(int[] arr, int sum)
{
int count = 1;
for (int i = 0; i < arr.length; 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] == sum)
{
System.out.println("Triplet " + count + ": " + arr[i] + " " + arr[j] + " " + arr[k]);
count++;
break;
}
}
}
}
}
}
Output: Finding triplets whose sum are equal to : 14 The triplets are : Triplet 1: 2 5 7 Triplet 2: 2 9 3 Triplet 3: 7 9 -2
Method-2: Java Program to Find all the Triplets Where Sum of All the Three Elements are Equal to a Specified Number By Dynamic Initialization of Array Elements
Approach:
- Ask use length of the array.
- Initialize the array with given size.
- Ask the user for array elements.
- Create three nested for loops.
- First loop runs from start to end (counter i), second loop runs from i+1 to end (counter j) and third loop runs from j+1 to end (loop counter k)
- Find the sum of ith, jth and kth element. If the sum is equal to given sum. Print the triplet and break.
- If there is no triplet, then print that no triplet exists.
Program:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
// asking user to enter the number of elements
System.out.println("Enter number of elements in the array: ");
int n = sc.nextInt();
// initializing the array
int[] arr = new int[n];
// asking user to enter the elements
System.out.println("Enter elements of the array: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// asking user to enter the sum
System.out.println("Enter sum: ");
int sum = sc.nextInt();
System.out.println("Finding triplets whose sum are equal to : "+sum);
System.out.println("The triplets are : ");
findTriplet(arr, sum);
}
public static void findTriplet(int[] arr, int sum)
{
int count = 1;
for (int i = 0; i < arr.length; 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] == sum)
{
System.out.println("Triplet " + count + ": " + arr[i] + " " + arr[j] + " " + arr[k]);
count++;
break;
}
}
}
}
}
}
Output: Enter number of elements in the array: 5 Enter elements of the array: 2 2 1 4 5 Enter sum: 5 Finding triplets whose sum are equal to : 5 The triplets are : Triplet 1: 2 2 1
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs:
- Java Program to Find all the Combination of Four Elements Where Sum of All the Four Elements are Equal to a Specified Number
- Java Program to Cyclically Rotate a Given Array Clockwise by One
- Java Program to Arrange the Elements of a Given Array of Integers Where All Negative Integers Appear Before All the Positive Integers
- Java Program to Separate 0s on Left Side and 1s on Right Side of an Array of 0s and 1s in Random Order