Java Program to Form Two Numbers (2 digit) with Minimum Sum Using Array Elements

In the previous article, we have seen Java Program to Form Two Numbers (of 2 digit) with Maximum Sum Using Array Elements

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

Java Program to Form Two Numbers (2 digit) with Minimum 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 minimum sum.

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

Approach:

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

Program:

import java.util.Arrays;
import java.util.Comparator;
 
class Main
{
    // Find two numbers with a minimum sum formed by digits of an array
    public static void findMainimum(Integer[] input)
    {
 
        // sort the array in ascending order
        Arrays.sort(input);
 
        //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 minimum sum are " + x + " and " + y);
        int sum=x+y;
        System.out.println("And the minimum sum is "+sum);        
    }
 
    public static void main(String[] args)
    {
        Integer[] input = { 1,5,3,4,5 };
 
        findMinimum(input);
    }
}
Output:

The two numbers formed by using array elements with minimum sum are 13 and 14
And the minimum sum is 27

Method-2: Java Program to Form Two Numbers (of 2 digits) with Minimum 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 findSmallest() to find 3 big elements in the array say firstNumber, secondNumber and thirdNumber.
  • After getting 3 small elements in the array, inside that findSmallest() method, call findMinNumber() method.
  • Inside findMinNumber() method combine firstNumber and secondNumber element which is the 1st smallest number that can be formed using array elements.
  • Next combine firstNumber and thirdNumber element which is the 2nd smallest number that can be formed using array elements.
  • As we have two smallest numbers so sum will be minimum.
  • 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 smallest numbers in array : ");
        //calling findSmallest() method
        findSmallest(arr);
    }
    
    //user defined method to find first 3 smallest element
    private static void findSmallest(int[] arr) 
    {
        int firstNumber = arr[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 smallest number : " + firstNumber);
        System.out.println("Second smallest number : " + secondNumber);
        System.out.println("Third smallest number : " + thirdNumber);
        
        //calling findMinNumber() method
        findMinNumber(firstNumber,secondNumber,thirdNumber);
    }
    
    //findMinNumber() user defined method to form two numbers whose sum is minimum
    public static void findMinNumber(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 minimum sum are " + x + " and " + y); 
        int sum=x+y; 
        System.out.println("And the minimum sum is "+sum);
    }
}
Output:

Enter number of elements in array: 
6
Enter elements into array: 
9 2 5 7 1 4
First 3 smallest numbers in array : 
First smallest number : 1
Second smallest number : 2
Third smallest number : 4
The two numbers formed by using array elements with minimum sum are 12 and 14
And the minimum sum is 26

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: