Min-Max-Sum hacker rank solution
Anin Arafath
Posted on March 28, 2022
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;
}
💖 💪 🙅 🚩
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.