Home > database >  Consistently calculating minimum and maximum sums from a list using Java 8 streams
Consistently calculating minimum and maximum sums from a list using Java 8 streams

Time:06-28

I am trying to solve the following issue:

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

My code which can be seen bellow is able to handle some test cases but fails others which I cannot seem to figure out, the issue seems to lie with the long max assignment statement, and probably with the .sorted(Collections.reverseOrder()) component as that is the only tangible difference between the min vs max statements.

Example test case where it works:

Input = [1, 2, 3, 4, 5]

Output = 10 (minimum), 14 (maximum)

Example test case where it does not work:

Input = [256741038, 623958417, 467905213, 714532089, 938071625]

Output = 2063136757 (minimum), -1550499952 (maximum)

Expected Output = 2063136757 (minimum), 2744467344 (maximum)

class Result {

    /*
     * Complete the 'miniMaxSum' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */

    public static void miniMaxSum(List<Integer> arr) {
    long max = arr.stream().sorted(Collections.reverseOrder()).limit(4).reduce(0, (subtotal, element) -> subtotal   element);
    long min = arr.stream().sorted().limit(4).reduce(0, (subtotal, element) -> subtotal   element);
    System.out.println(min   " "   max);
    }

}

public class Solution {
    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());

        Result.miniMaxSum(arr);

        bufferedReader.close();
    }
}

CodePudding user response:

As @Benjamin W. has said, there seems to be some overflowing occurring here. It might have to do with the list containing Integer objects instead of longs, but you would have to look into it further. Your buffered reader seams like it is working perfectly fine.

If you just want a simple solution, you could just find the smallest number in the list, and omit it from the largest sum, and find the largest element from the list, and omit it from the smallest sum.

public static void minmaxSum(List<Integer> arr) {
    if (arr.size == 0) return;

    long max = arr.get(0):
    long min = arr.get(0);

    long maxSum = 0;
    long minSum = 0;

    for (Integer num : arr) {
        long longNum = (long) num.intValue();
        
        maxSum  = longNum;
        minSum  = longNum;

        if (longNum < min) min = longNum;
        if (longNum > max) max = longNum;
    }

    maxSum -= min;
    minSum -= max;

    System.out.println(minSum   " "   maxSum);
}       

Besides, code like this is much more clear anyway.

  • Related