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;
}
Top comments (2)
congrats, but why do you disclose solutions publicly? I think Hackerrank already link users and companies.