Monday, September 27, 2021

min max sum

 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

The minimum sum is  and the maximum sum is . The function prints

Output:

16   24
Answer:
void miniMaxSum(vector<int> arr) {
  long summax=0,summin=0;
   int n = arr.size();
   sort(arr.begin(),arr.end());
   long min=arr[0];
   long max= arr[n-1];
   for(int i=0;i<n;i++)
   {
       summax = summax+arr[i];
   }
   min=summax-min;
   max=summax-max;
   cout<<max<<" "<<min;
}

No comments:

Post a Comment

Java Regex

You are updating the username policy on your company's internal networking platform. According to the policy, a username is considered v...