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.
- How to Declare an Array in Java?
- How to Instantiate an Array in Java?
- How to Initialize an Array in Java?
Base Condition:
- The number is a 2 digited number (Ex-54, 89, 71 etc.)
- We have to form 2 numbers using array elements.
- 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 sayfirstNumber
,secondNumber
andthirdNumber
. - After getting 3 big elements in the array, inside that
findBiggest()
method, callfindMaxNumber(
) method. - Inside
findMaxNumber(
) method combinefirstNumber
andsecondNumber
element which is the 1st biggest number that can be formed using array elements. - Next combine
firstNumber
andthirdNumber
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:
- Java Program to Form Two Numbers (of 2 digit) with Minimum Sum Using Array Elements
- Java Program to Find Number of 1’s in an Integer Array
- Java Program to Move An Array Element From One Array Position to Another Position
- Java Program to Find Sum of Elements of an Array
- Java Program to Find Average of all Array Elements