Find maximum sum of two array elements in Java
Two elements giving the maximum sum in an array means, we have to find two largest array elements which will eventually give the maximum sum possible.
In this article we will see how we can find the maximum sum of two elements in Java.
To show you some instances
The Chinese translation ofInstance-1
is:Instance-1
Suppose we have the below array
[10, 2, 3, -5, 99, 12, 0, -1]
In this array the largest element is 99 and second largest is 12.
Max sum = 99 12
Therefore, the maximum sum of two elements in this array is 111.
Instance-2
Suppose we have the below array
[556, 10, 259, 874, 123, 453, -96, -54, -2369]
In this array, the largest element is 874, and the second largest element is 556.
Max sum = 874 556
Therefore, the maximum sum of two elements in this array is 1430.
Instance-3
Suppose we have the below array
[55, 10, 29, 74, 12, 45, 6, 5, 269]
In this array, the largest element is 269, and the second largest element is 74.
Max sum= 269 74
Therefore, the maximum sum of two elements in this array is 343.
Algorithm
Algorithm-1
Step 1 − Use a for loop to find the largest and second largest elements in the array.
Step 2 - Find their sum.
Step 3 − Print the sum.
The translation ofAlgorithm-2
is:Algorithm-2
Step 1 − Sort the array elements.
Step 2 −Take the last and second last element of the array.
Step 3 − Find their sum.
Step 4 − Print the sum.
Syntax
To sort the array we need to use the sort( ) method of the Arrays class of java.util package.
Following is the syntax to sort any array in ascending order using the method
<span class="typ">Arrays</span><span class="pun">.</span><span class="pln">sort</span><span class="pun">(</span><span class="pln">array_name</span><span class="pun">);</span>
Where, ‘array_name’ refers to the array that you want to sort.
Multiple methods
We have provided the solution in different approaches.
Use a for loop to find the largest sum
Find The Largest Sum Using Arrays.sort
Let’s look at the program and its output one by one.
Method 1: Use for loop
In this approach, we use a for loop to iterate through the array elements to find out the largest and second largest element. These two elements will give the maximum sum.
Example
public class Main { public static void main(String[] args) { // The array elements int arr[] = { 10, 2, 3, -5, 99, 12, 0, -1 }; // Storing the first element in both variables int first = arr[0], second = arr[0]; // For loop to iterate the elements from 1 to n // to find the first largest element for (int i = 0; i < arr.length; i++) { // If array element is larger than current largest element, then swap if (arr[i] > first) first = arr[i]; } // For loop to iterate the elements from 1 to n // to find the second largest element for (int i = 0; i < arr.length; i++) { // If array element is larger than current largest element and not equals to // largest element, then swap if (arr[i] > second && arr[i] != first) second = arr[i]; } // Print the sum System.out.println("Largest sum = " + (first + second)); System.out.println("The elements are " + first + " and " + second); } }
Output
Largest sum = 111 The elements are 99 and 12
Approach-2: By Using Arrays.sort
In this method, we use the Arrays.sort() method to sort the array. Then we take the element at the last and penultimate index. Since the array is already sorted, these two elements will give the maximum sum.
Example
import java.util.Arrays; public class Main { public static void main(String[] args) { // The array elements int arr[] = { 10, 2, 3, -5, 99, 12, 0, -1 }; // Sort the array using the sort method from array class Arrays.sort(arr); // Storing the last element as largest and second last element as second largest int first = arr[arr.length - 1], second = arr[arr.length - 2]; // Print the maximum sum System.out.println("Maximum sum = " + (first + second)); System.out.println("The elements are " + first + " and " + second); } }
Output
Maximum sum = 104 The elements are 99 and 12
In this article, we explored different ways to find the two elements in an array that have the maximum sum in Java.
The above is the detailed content of Find maximum sum of two array elements in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.
