In this article, we will discuss how to find sum of smallest 2 numbers in an Arrays and List
1. Finding sum of smallest 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 ascending-order sorting
- After sorting,
- Iterate through ascending-order sorted Arrays
- Limit first 2 elements for smallest numbers
- add/sum them to get sum of smallest 2 numbers in an Arrays
- Finally, print addition of 2 smallest number to console
FindSumOfSmallestTwoNumbersInAnArrays.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 FindSumOfSmallestTwoNumbersInAnArrays {
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 i1 - i2; // ascending-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 smallest numbers in an Arrays is - "
+ sum);
// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Sum of 2 smallest numbers in an Arrays is - 3
Execution time - 0 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 arrays to be sorted
- 2nd argument is the anonymous Comparator object with logic for ascending-order sorting
- After sorting,
- Iterate through ascending-order sorted List
- Limit first 2 elements for smallest numbers
- add/sum them to get sum of smallest 2 numbers in List
- Finally, print addition of 2 smallest number to console
FindSumOfSmallestTwoNumbersInList.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 FindSumOfSmallestTwoNumbersInList {
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(i1, i2); // ascending-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 smallest numbers in List is - "
+ sum);
// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Sum of 2 smallest numbers in List is - 3
Execution time - 0 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:
- Java 8 – Find Largest number in an Arrays or List or Stream ?
- Java 8 – Find Smallest number in an Arrays or List or Stream ?
- Java 8 – Find 2nd Largest number in an Arrays or List or Stream ?
- Java 8 – Find 2nd Smallest number in an Arrays or List or Stream ?
- Java 8 – Find sum of Largest 2 numbers in an Arrays or List or Stream ?
- Java 8 – Find sum of Smallest 2 numbers in an Arrays or List or Stream ?
- Java 8 – Find 1st and Last elements in an Arrays ?
- Java 8 – Find 1st and Last elements in a List or ArrayList ?
- Java 8 – Find 1st and Last elements in a Set or HashSet ?
- Java 8 – Find 1st and Last entries in a Map or HashMap ?
- Java 8 – Find sum and average of a List or ArrayList ?
- Java 8 – How to calculate sum and average of an Arrays ?
Happy Coding !!
Happy Learning !!