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
Instance-1
的中文翻译为:实例-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
因此,这个数组中两个元素的最大和为111。
Instance-2
Suppose we have the below array
[556, 10, 259, 874, 123, 453, -96, -54, -2369]
在这个数组中,最大的元素是874,第二大的元素是556。
Max sum = 874+556
因此,该数组中两个元素的最大和为1430。
Instance-3
Suppose we have the below array
[55, 10, 29, 74, 12, 45, 6, 5, 269]
在这个数组中,最大的元素是269,第二大的元素是74。
Max sum= 269+74
因此,该数组中两个元素的最大和为343。
Algorithm
算法-1
步骤 1 − 使用 for 循环在数组中找到最大和第二大的元素。
步骤 2 - 找到它们的和。
Step 3 − Print the sum.
Algorithm-2
的翻译为:算法-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.
多种方法
We have provided the solution in different approaches.
使用for循环找到最大的和
Find The Largest Sum Using Arrays.sort
让我们逐一查看程序及其输出。
方法一:使用for循环
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
在这种方法中,我们使用Arrays.sort()方法对数组进行排序。然后我们取最后一个和倒数第二个索引上的元素。由于数组已经排序过了,这两个元素将给出最大的和。
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
在本文中,我们探讨了在Java中找到数组中具有最大和的两个元素的不同方法。
以上是在Java中查找两个数组元素的最大和的详细内容。更多信息请关注PHP中文网其他相关文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),