Mini-Max Sum

  • + 0 comments

    SIMPLE JAVA8 CODING SOLUTION

    public static void miniMaxSum(List<Integer> arr) {
    // Write your code here
        long sum = 0; // Using long to avoid overflow issues
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
    
        // Calculate the total sum and find min and max values
        for (int num : arr) {
            sum += num;
            min = Math.min(min, num);
            max = Math.max(max, num);
        }
    
        // Calculate the minimum and maximum sums
        long minSum = sum - max; // Sum of all except max
        long maxSum = sum - min; // Sum of all except min
    
        System.out.println(minSum + " " + maxSum);
    
    }