DEV Community

Cover image for Min-Max-Sum hacker rank solution
Anin Arafath
Anin Arafath

Posted on

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

Top comments (2)

spo0q profile image
spO0q 🐒

congrats, but why do you disclose solutions publicly? I think Hackerrank already link users and companies.

hesoyamm profile image
Kishore kunal