Find the minimum and maximum sum of N-1 elements of the array

Given an unsorted array A of size N, the task is to find the minimum and maximum values that can be calculated by adding exactly N-1 elements.

Examples:

Input: a[] = {13, 5, 11, 9, 7}
Output: 32 40
Explanation: Minimum sum is 5 + 7 + 9 + 11 = 32 and maximum sum is 7 + 9 + 11 + 13 = 40.

Input: a[] = {13, 11, 45, 32, 89, 21}
Output: 122 200
Explanation: Minimum sum is 11 + 13 + 21 + 32 + 45 = 122 and maximum sum is 13 + 21 + 32 + 45 + 89 = 200.

Input: a[] = {6, 3, 15, 27, 9}
Output: 33 57
Explanation: Minimum sum is 3 + 6 + 9 + 15 = 33 and maximum sum is 6 + 9 + 15 + 27 = 57.



Simple Approach:

  1. Sort the array in ascending order.
  2. Sum of the first N-1 elements in the array gives the minimum possible sum.
  3. Sum of the last N-1 elements in the array gives the maximum possible sum.

Time complexity: O(NlogN)

Efficient Approach:

  1. Find the minimum and maximum element of the array.
  2. Calculate the sum of all the elements in the array.
  3. Excluding maximum element from the sum gives the minimum possible sum.
  4. Excluding the minimum element from the sum gives the maximum possible sum.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the minimum and maximum
// sum from an array.
class GFG {
  
    // Function to calculate minimum and maximum sum
    static void miniMaxSum(int[] arr)
    {
  
        // Initialize the minElement, maxElement
        // and sum by 0.
        int minElement = 0, maxElement = 0, sum = 0;
  
        // Assigning maxElement, minElement
        // and sum as the first array element
        minElement = arr[0];
        maxElement = minElement;
        sum = minElement;
  
        // Traverse the entire array
        for (int i = 1; i < arr.length; i++) {
  
            // calculate the sum of
            // array elements
            sum += arr[i];
  
            // Keep updating the
            // minimum element
            if (arr[i] < minElement) {
                minElement = arr[i];
            }
  
            // Keep updating the
            // maximum element
            if (arr[i] > maxElement) {
                maxElement = arr[i];
            }
        }
  
        // print the minimum and maximum sum
        System.out.println((sum - maxElement) + " "
                           + (sum - minElement));
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Test Case 1:
        int a1[] = { 13, 5, 11, 9, 7 };
        // Call miniMaxSum()
        miniMaxSum(a1);
  
        // Test Case 2:
        int a2[] = { 13, 11, 45, 32, 89, 21 };
        miniMaxSum(a2);
  
        // Test Case 3:
        int a3[] = { 6, 3, 15, 27, 9 };
        miniMaxSum(a3);
    }
}

chevron_right


Output:

32 40
122 200
33 57

Time complexity: O(N)

competitive-programming-img




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : prashantsrivastava1