Min-Max

Given A Series Of N Positive Integers a1,a2,a3........an. , Find The Minimum And Maximum Values That Can Be Calculated By Summing Exactly N-1 Of The N Integers. Then Print The Respective Minimum And Maximum Values As A Single Line Of Two Space-Separated Long Integers.

Example:

Input:  n = 5, a = [1,2,3,4,5]
Output: 10 14

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void minMax(long long nlong long a[])
{
    long long sum = 0;
    for (long long i = 0i < ni++)
    {
        sum += a[i];
    }
    sort(aa + n);
    long long min1 = sum - a[n - 1];
    long long max1 = sum - a[0];
    cout << min1 << " " << max1 << "\n";
}
int main()
{
    long long n = 5;

    long long a[n] = {12345};

    minMax(na);

    return 0;
}


No comments:

Post a Comment