[Hackerrank]Max min 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.

Input Format

A single line of five space-separated integers.

Constraints

  • Each integer is in the inclusive range .

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 32 bit integer.)

Sample Input

1 2 3 4 5

Sample Output

10 14

Explanation

Our initial numbers are , and . We can calculate the following sums using four of the five integers:

  1. If we sum everything except , our sum is .
  2. If we sum everything except , our sum is .
  3. If we sum everything except , our sum is .
  4. If we sum everything except , our sum is .
  5. If we sum everything except , our sum is .

As you can see, the minimal sum is  and the maximal sum is . Thus, we print these minimal and maximal sums as two space-separated integers on a new line.

Hints: Beware of integer overflow! Use 64-bit Integer.



#include <iostream>
using namespace std;


int main() {
    long long arr[5];
    long long max=0,sum=0,min;
    for(int arr_i = 0; arr_i < 5; arr_i++){
       cin >> arr[arr_i];
    }
    min = arr[0];
    for(int i=0;i<5;i++)
    {
        if(arr[i]>max)  max = arr[i];
        if(arr[i]<min)  min = arr[i];
        sum += arr[i];
    }   
    max = sum-max;
    min = sum-min;
    cout<<max<<' '<<min;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值