In competitive programming, the **Min-Max Sum** challenge asks you to take five positive integers and find the minimum and maximum values that can be calculated by summing exactly four of those five integers. While it sounds simple, the primary trap in this challenge is **integer overflow**.
In this guide, we will solve the problem efficiently using Java 8 Streams and review how to avoid numerical overflow bounds.
Imagine you have 5 weights: 1g, 2g, 3g, 4g, and 5g. You want to pick exactly 4 weights:
- Minimum Sum: To get the lightest sum, you leave behind the heaviest weight (5g). The sum is:
1 + 2 + 3 + 4 = 10. - Maximum Sum: To get the heaviest sum, you leave behind the lightest weight (1g). The sum is:
2 + 3 + 4 + 5 = 14.
Mathematically, you don't need to try every combination. You simply calculate the **total sum** of all 5 weights, and then:
Min Sum = Total Sum - Max ValueMax Sum = Total Sum - Min Value
1. The Stream Reduce Solution
Using Java 8 Streams, we can scan the array to find the minimum value, maximum value, and total sum using reduce():
public static void miniMaxSum(List<Integer> arr) {
// 1. Find the minimum value
int minValue = arr.stream().reduce(arr.get(0), (min, element) -> {
return min > element ? element : min;
});
// 2. Find the maximum value
int maxValue = arr.stream().reduce(arr.get(0), (max, element) -> {
return max < element ? element : max;
});
// 3. Find the total sum
int totalSum = arr.stream().reduce(0, (sum, element) -> {
return sum + element;
});
// 4. Print sums excluding extremes
System.out.println((totalSum - maxValue) + " " + (totalSum - minValue));
}
If the five input values are large (e.g. 1,000,000,000), their total sum will be 5,000,000,000.
Because a standard 32-bit Java int only supports values up to 2,147,483,647, summing them will overflow, returning incorrect negative results!
Solution: Always store the total sum using a 64-bit long type.
Java Implementation
Below is the complete solution including IO reading:
package io.practise.hackerrank;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class MinMaxSum {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
miniMaxSum(arr);
bufferedReader.close();
}
public static void miniMaxSum(List<Integer> arr) {
long minValue = arr.get(0);
long maxValue = arr.get(0);
long totalSum = 0;
for (int val : arr) {
totalSum += val; // Prevents overflow via long arithmetic
if (val < minValue) minValue = val;
if (val > maxValue) maxValue = val;
}
System.out.println((totalSum - maxValue) + " " + (totalSum - minValue));
}
}
Conclusion
The Min-Max Sum challenge is a great study in optimization. By calculating the total sum once, we avoid nesting loops, and by using long data structures, we protect the program against data truncation errors.