Java Program to Find all the Combination of Four Elements Where Sum of All the Four Elements are Equal to a Specified Number

In the previous article, we have seen Java Program to Find all the Triplets Where Sum of All the Three Elements are Equal to a Specified Number

In this article we are going to see how to find all the Combination of Four Elements Where Sum of All the Three Elements are Equal to a Specified Number.

Java Program to Find all the Combination of Four Elements Where Sum of All the Four 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 Combination of Four Elements Where Sum of All the Four Elements are Equal to a Specified Number By Static Initialization of Array Elements

Approach:

  1. Create four nested for loops and compare each quadruple with the required sum.
  2. If it’s equal, print the quadruple.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        int[] arr = { 2, 3, 6, 7, 4, 1, 5, 0 };
        int sum = 14;
        System.out.println("Finding quadruple whose sum are equal to : "+sum); 
        System.out.println("The quadruple are : ");
        findQuadruples(arr, sum);
    }

    static void findQuadruples(int[] arr, int sum) 
    {
    int count = 1;
        int n = arr.length;
        for (int i = 0; i < n - 3; i++) 
        {
            for (int j = i + 1; j < n - 2; j++) 
            {
                for (int k = j + 1; k < n - 1; k++) 
                {
                    for (int l = k + 1; l < n; l++) 
                    {
                        if (arr[i] + arr[j] + arr[k] + arr[l] == sum)
                        {
                            System.out.print("Quadruple " + count + ": " + arr[i] + " " + arr[j] + " " + arr[k] + " " + arr[l] + "\n");
                            count++; 
                            break;
                        }
                    }
                }
            }
        }
    }
}

Output:

Finding quadruple whose sum are equal to : 14
The quadruple are : 
Quadruple 1: 2 3 4 5
Quadruple 2: 2 6 1 5
Quadruple 3: 2 7 4 1
Quadruple 4: 2 7 5 0
Quadruple 5: 3 6 4 1
Quadruple 6: 3 6 5 0
Quadruple 7: 3 7 4 0
Quadruple 8: 6 7 1 0

Method-2: Java Program to Find all the Combination of Four Elements Where Sum of All the Four Elements are Equal to a Specified Number By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Create four nested for loops and compare each quadruple with the required sum.
  • If it’s equal, print the quadruple.

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 required sum
        System.out.println("Enter the required sum: ");
        int sum = sc.nextInt();

        System.out.println("Finding quadruple whose sum are equal to : "+sum); 
        System.out.println("The quadruple are : ");
        findQuadruples(arr, sum);
    }

    static void findQuadruples(int[] arr, int sum) 
    {
    int count = 1;
        int n = arr.length;
        for (int i = 0; i < n - 3; i++) 
        {
            for (int j = i + 1; j < n - 2; j++) 
            {
                for (int k = j + 1; k < n - 1; k++) 
                {
                    for (int l = k + 1; l < n; l++) 
                    {
                        if (arr[i] + arr[j] + arr[k] + arr[l] == sum)
                        {
                            System.out.print("Quadruple " + count + ": " + arr[i] + " " + arr[j] + " " + arr[k] + " " + arr[l] + "\n");
                            count++; 
                            break;
                        }
                    }
                }
            }
        }
    }
}

Output:
Enter number of elements in the array: 
8
Enter elements of the array: 
6 3 1 2 4 7 5 4
Enter the required sum: 
12
Finding quadruple whose sum are equal to : 12
The quadruple are : 
Quadruple 1: 6 3 1 2
Quadruple 2: 3 1 4 4
Quadruple 3: 1 2 4 5
Quadruple 4: 1 2 5 4

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: