Find the minimum and maximum values that can be calculated by summing exactly four of the five integers


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.
Input Format
A single line of five space-separated integers.
Output Format
Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than a 32 bit integer.)
Sample Input
1   2   3   4   5
Sample Output
10   14

SOLUTION

#include <stdio.h>
int main()
{
    long int a[5],b[5],i,j,sum,max,min;
    for(i=0;i<5;i++)
        scanf("%ld",&a[i]);
    i=min=max=0;
    while(i<5)
    {
        sum=0;
        for(j=0;j<5;j++)
            if(i!=j)
                sum=sum+a[j];
        if(min>sum||min==0)
            min=sum;
        if(max<sum)
            max=sum;
        i++;
    }
    printf("%ld %ld",min,max);
}

No comments:

Post a Comment

Total Pageviews