Min-Max-Sum hacker rank solution

aninarafath6

Anin Arafath

Posted on March 28, 2022

Min-Max-Sum hacker rank solution

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.

Example :-

arr = [1,3,7,5,9]

minimum sum is and the maximum sum is 1+3+5+7 = 16 and the maximum sum is 3+7+5+9 =24

The function prints

16 14

code :-

void miniMaxSum(vector<int> arr) {
     long long int sum =0;int maxVal=arr[0],minVal=arr[0];


    for(int i=0;i<5;i++){
        sum += arr[i];
        minVal = min(minVal,arr[i]);
        maxVal = max(maxVal,arr[i]);
    }

    long long int minSum = sum - maxVal;
    long long int maxSum = sum - minVal;


    cout << minSum << " "<< maxSum << endl;

}

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
aninarafath6
Anin Arafath

Posted on March 28, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Min-Max-Sum hacker rank solution
solution Min-Max-Sum hacker rank solution

March 28, 2022