In the previous article, we have seen Java Program to Find Maximum Product of Two Integers in an Array
In this article we are going to see how we can find maximum sum of two integers in an array.
Java Program to Find Maximum Sum of Two Integers in an Array
As we have to write a program which will find the maximum sum of two integers in an array of integers so our first target should be finding first two biggest numbers from the array elements. As it is very much clear that sum of two bigger numbers will give maximum result than sum of two smaller numbers.
For example:
If we have an array say arr=[1, 7, 4, 2, 8, 6, 3, 9, 5] Then in this array first two bigger numbers are 9 and 8. So sum is 9+8=17 which is maximum sum value we can find than any other possible combinations of array elements.
Let’s see different ways to find maximum sum of two integers in an array.
Method-1: Java Program to Find Maximum Sum of Two Integers in an Array By Using Sorting Technique
Approach:
- Declare an array and with array elements.
- Sort the array by using
Arrays.sort()
method. - Now array is sorted in ascending order.
- So, find the sum of last element and second last element which will give the maximum sum value.
- Print the result.
Program:
import java.util.Arrays; import java.util.Comparator; class Main { public static void main(String[] args) { //integer array declared along with integer elements Integer[] input = { 1,6,3,4,5 }; // sort the array in ascending order Arrays.sort(input); //as array is sorted in ascending order //so last two elements are biggest elements //so by adding the last two elements we will get maximum sum long result=input[input.length-1]+input[input.length-2]; //input[input.length-1] represents last elements //input[input.length-2] second last element System.out.println("Two numbers are "+ input[input.length-1] +" and "+ input[input.length-2]+" having maximum sum "+result); } }
Output: Two numbers are 6 and 5 having maximum sum 11
Method-2: Java Program to Find Maximum Sum of Two Integers in an Array By Finding First Two Biggest Element
Approach:
- Take user input of length of array.
- Take input of integer array elements.
- Call the user defined method
findBiggest()
to find first 2 big elements in the array sayfirstNumber
,secondNumber
- After getting 2 big elements in the array, inside that
findBiggest()
method, callfindMaxSum(
) method. - Inside
findMaxSum(
) method addfirstNumber
andsecondNumber
which will give the maximum sum value. - Print the result.
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 numbers 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 2 biggest numbers in array : "); //calling findBiggest() method findBiggest(arr); } //user defined method to find first 2 biggest element private static void findBiggest(int[] arr) { int firstNumber = arr[0]; int secondNumber = arr[1]; for (int i = 0; i < arr.length; i++) { if (arr[i] > firstNumber) { secondNumber = firstNumber; firstNumber = arr[i]; } else if (arr[i] > secondNumber) { secondNumber = arr[i]; } } System.out.println("First biggest number : " + firstNumber); System.out.println("Second biggest number : " + secondNumber); //calling findMaxSum() method to find maximum sum value findMaxSum(firstNumber,secondNumber); } public static void findMaxSum(int firstNumber, int secondNumber) { //adding both numbers to find summation value int result= firstNumber+secondNumber; System.out.println("Two numbers are "+ firstNumber +" and "+ secondNumber+" having maximum sum "+result); } }
Output: Enter number of elements in array: 5 Enter elements into array: 40 10 30 50 20 First 2 biggest numbers in array : First biggest number : 50 Second biggest number : 40 Two numbers are 50 and 40 having maximum sum 90
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Related Java Programs: