DEV Community

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

Posted on

7 2

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)

Collapse
 
spo0q profile image
spO0q

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

Collapse
 
hesoyamm profile image
Kishore kunal

xD

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay