-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxSumArrayList.java
39 lines (32 loc) · 1.31 KB
/
MaxSumArrayList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.ArrayList;
import java.util.Scanner;
public class HighestAdjacentSum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();
// Taking input from the user
System.out.println("Enter numbers (enter any non-integer value to stop):");
while (scanner.hasNextInt()) {
int num = scanner.nextInt();
numbers.add(num);
}
// Finding the sum of two adjacent numbers with the highest value
int maxSum = Integer.MIN_VALUE;
int maxIndex = -1;
for (int i = 0; i < numbers.size() - 1; i++) {
int sum = numbers.get(i) + numbers.get(i + 1);
if (sum > maxSum) {
maxSum = sum;
maxIndex = i;
}
}
// Displaying the result
if (maxIndex != -1) {
System.out.println("The sum of the two adjacent numbers with the highest value is: " + maxSum);
System.out.println("Those numbers are: " + numbers.get(maxIndex) + " and " + numbers.get(maxIndex + 1));
} else {
System.out.println("There are no adjacent numbers to find the sum.");
}
scanner.close();
}
}