Java Program to Form Two Numbers (of 2 digits) with Maximum Sum Using Array Elements

In the previous article, we have seen Java Program to Find All the Elements in an Array that are Greater than All Elements to Their Right

In this article, we will see how to form two numbers of 2 digits with maximum sum using array elements in Java programming language.

Java Program to Form Two Numbers (of 2 digits) with Maximum Sum Using Array Elements

Prerequisite: 

See below articles to know more about Array in Java, array declaration, array instantiation and array initialization.

Base Condition:

  1. The number is a 2 digited number (Ex-54, 89, 71 etc.)
  2. We have to form 2 numbers using array elements.
  3. All the array elements need to be single digited only.

Let’s see different ways to form two numbers of 2 digits with maximum sum.

Method-1: Java Program to Form Two Numbers (of 2 digits) with Maximum Sum Using Array Elements By Using Sorting

Approach:

  • Declare an array and with single digit numbers as array elements.
  • Call the user defined method findMaximum().
  • Inside method, sort the array in descending order.
  • So first element of array is biggest element in array and elements are ordered in decreasing order.
  • So combine index-0 and index-1 element which is the 1st biggest number that can be formed using array elements.
  • Next combine index-0 and index-2 element which is the 2nd biggest number that can be formed using array elements.
  • As we have two largest numbers so sum will be maximum.
  • So, return those two numbers.

Program:

import java.util.Arrays;
import java.util.Comparator;
 
class Main
{
    // Find two numbers with a maximum sum formed by digits of an array
    public static void findMaximum(Integer[] input)
    {
 
        // sort the array in descending order
        Arrays.sort(input, Comparator.reverseOrder());
 
        //find first number 
        int x = 0;
        x = x * 10 + input[0];
        x = x * 10 + input[1];
        
 
        //find second number
        int y=0;
        y = y * 10 + input[0];
        y = y * 10 + input[2];
       
        // print `x` and `y`
        System.out.println("The two numbers formed by using array elements with maximum sum are " + x + " and " + y);
        int sum=x+y;
        System.out.println("And the maximum sum is "+sum);        
    }
 
    public static void main(String[] args)
    {
        Integer[] input = { 1,5,3,4,5 };
 
        findMaximum(input);
    }
}
Output:

The two numbers formed by using array elements with maximum sum are 55 and 54
And the maximum sum is 109

Method-2: Java Program to Form Two Numbers (of 2 digits) with Maximum Sum Using Array Elements By Finding 1st, 2nd and 3rd Big Element from Array

Approach:

  • Take user input of length of array.
  • Take input of array elements (only single digits)
  • Call the user defined method findBiggest() to find 3 big elements in the array say firstNumber, secondNumber and thirdNumber.
  • After getting 3 big elements in the array, inside that findBiggest() method, call findMaxNumber() method.
  • Inside findMaxNumber() method combine firstNumber and secondNumber element which is the 1st biggest number that can be formed using array elements.
  • Next combine firstNumber and thirdNumber element which is the 2nd biggest number that can be formed using array elements.
  • As we have two largest numbers so sum will be maximum.
  • So, return those two numbers.

Program:

import java.util.Scanner;

public class Main 
{
     public static void main(String args[]) 
    {
        int length = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of elements in array: ");
        //array length taken as input
        length = sc.nextInt();
        //array declared
        int[] arr = new int[length];
        
        //take input of single digited number as array element
        System.out.println("Enter elements into array: ");
        //taking input of array elements
        for (int i = 0; i < length; i++) 
        {
            arr[i] = sc.nextInt();
        }

        System.out.println("First 3 biggest numbers in array : ");
        //calling findBiggest() method
        findBiggest(arr);
    }
    
    //user defined method to find first 3 biggest element
    private static void findBiggest(int[] arr) 
    {
        int firstNumber = 0;
        int secondNumber = 0;
        int thirdNumber = 0;

        for (int i = 0; i < arr.length; i++) 
        { 
            if (arr[i] > firstNumber) 
            {
                thirdNumber = secondNumber;
                secondNumber = firstNumber;
                firstNumber = arr[i];
            } 
            else if (arr[i] > secondNumber) 
            {
                thirdNumber = secondNumber;
                secondNumber = arr[i];
            }
            else if (arr[i] > thirdNumber) 
            {
                thirdNumber = arr[i];
            }
        }

        System.out.println("First biggest number : " + firstNumber);
        System.out.println("Second biggest number : " + secondNumber);
        System.out.println("Third biggest number : " + thirdNumber);
        
        //calling findMaxNumber() method
        findMaxNumber(firstNumber,secondNumber,thirdNumber);
    }
    
    //findMaxNumber() user defined method to form two numbers whose sumis maximum
    public static void findMaxNumber(int firstNumber,int secondNumber,int thirdNumber)
    {
        //find first number 
        int x = 0; 
        x = x * 10 + firstNumber; 
        x = x * 10 + secondNumber;
        
        //find second number 
        int y = 0; 
        y = y * 10 + firstNumber; 
        y = y * 10 + thirdNumber;
        
        // print `x` and `y` 
        System.out.println("The two numbers formed by using array elements with maximum sum are " + x + " and " + y); 
        int sum=x+y; 
        System.out.println("And the maximum sum is "+sum);
    }
}
Output:

Enter number of elements in array: 
6
Enter elements into array: 
5 1 6 4 2 3 
First 3 biggest numbers in array : 
First biggest number : 6
Second biggest number : 5
Third biggest number : 4
The two numbers formed by using array elements with maximum sum are 65 and 64
And the maximum sum is 129

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Related Java Articles: