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

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:

Happy Coding !!
Happy Learning !!