Java – Find sum of Largest 2 numbers in an Arrays or List ?

In this article, we will discuss how to find sum of largest 2 numbers in an Arrays and List

1. Finding sum of largest 2 numbers in an Arrays

  • First, we will sort arrays of integer numbers using Arrays.sort() method by passing 2 input-arguments where,
    • 1st argument is the actual/original arrays to be sorted
    • 2nd argument is the anonymous Comparator object with logic for descending-order sorting
  • After sorting,
    • Iterate through descending-order sorted Arrays
    • Limit first 2 elements for largest numbers
    • add/sum them to get sum of largest 2 numbers in an Arrays
  • Finally, print addition of 2 largest number to console

FindSumOfLargestTwoNumbersInAnArrays.java

package in.bench.resources.java.finding.sum;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;

public class FindSumOfLargestTwoNumbersInAnArrays {

	public static void main(String[] args) {

		// random numbers
		Integer[] numbers = {5, 9, 11, 2, 8, 21, 1};


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// sorting integers in reverse order using Comparator
		Arrays.sort(numbers, new Comparator<Integer>() {

			@Override
			public int compare(Integer i1, Integer i2) {
				return i2 - i1; // descending-order sorting
			}
		});


		// variable sum
		int sum = 0;


		// summing first 2 largest numbers
		for(int index = 0; index < numbers.length && index < 2; index++) {

			sum += numbers[index];
		}


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("Sum of 2 largest numbers in an Arrays is - "
				+ sum);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");
	}
}

Output:

Sum of 2 largest numbers in an Arrays is - 32

Execution time - 2000000 ns

2. Finding sum of largest 2 numbers in List

  • First, we will sort list of integer numbers using Collections.sort() method by passing 2 input-arguments where,
    • 1st argument is the actual/original list to be sorted
    • 2nd argument is the anonymous Comparator object with logic for descending-order sorting
  • After sorting,
    • Iterate through descending-order sorted List
    • Limit first 2 elements for largest numbers
    • add/sum them to get sum of largest 2 numbers in List
  • Finally, print addition of 2 largest number to console

FindSumOfLargestTwoNumbersInList.java

package in.bench.resources.java.finding.sum;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class FindSumOfLargestTwoNumbersInList {

	public static void main(String[] args) {

		// random numbers
		List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// sorting integers in reverse order using Comparator
		Collections.sort(numbers, new Comparator<Integer>() {

			@Override
			public int compare(Integer i1, Integer i2) {
				return Integer.compare(i2, i1); // descending-order sorting
			}
		});


		// variable sum
		int sum = 0;


		// summing first 2 largest numbers
		for(int index = 0; index < numbers.size() && index < 2; index++) {

			sum += numbers.get(index);
		}


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("Sum of 2 largest numbers in List is - "
				+ sum);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");
	}
}

Output:

Sum of 2 largest numbers in List is - 32

Execution time - 1000000 ns

3. Points to remember w.r.t execution time:

  • Execution time differs in different platforms
  • With small set of numbers, we may not find large difference in execution time
  • But with large set of numbers, difference will be significant to consider

Related Articles:

Happy Coding !!
Happy Learning !!

Java – Find sum of Smallest 2 numbers in an Arrays or List ?
Java 8 – How to find duplicate and its count in an Arrays ?