Archive/Code_HackerRank 2018. 1. 17. 00:45

[HackerRank]Mini-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.

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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
 
using namespace std;
 
long minX(vector<int> arr){
    long a=(long)arr[0];
    
    for(int i=1; i<5; i++){
        if(a > arr[i])
            a = arr[i];
    }
    
    return a;
}
long maxX(vector<int> arr){
    long a= (long)arr[0];
    for(int i=1; i<5; i++){
        if(a < arr[i])
            a = arr[i];
    }
    
    return a;
}
 
 
int main() {
    vector<int> arr(5);
    for(int arr_i = 0; arr_i < 5; arr_i++){
       cin >> arr[arr_i];
    }
    long a=0;
    for(int i=0; i<5; i++){
        a += (long)arr[i];
    }
    
    
    cout << a-maxX(arr) << " " <<a-minX(arr);
    
    
    return 0;
}
 
cs



Comment


가장 작은 값과 가장 큰 값을 뽑아내면 된다.

long int가 필요하다.

'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Time Conversion  (0) 2018.01.17
[HackerRank]Birthday Cake Candles  (0) 2018.01.17
[HackerRank]Staircase  (0) 2018.01.17
[HackerRank]Plus Minus  (0) 2018.01.17
[HackerRank]Diagonal Difference  (0) 2018.01.17