[HackerRank]Mini-Max Sum

jh Seo·2024년 2월 4일
0

HackerRank

목록 보기
2/15

개요

[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.

접근 방식

정렬을 돌린 후, 앞에서 네 값, 뒤에서 네 값을 더하는 기본적인 문제다.

전체 코드

/*
 * Complete the 'miniMaxSum' function below.
 *
 * The function accepts INTEGER_ARRAY arr as parameter.
 */

void miniMaxSum(vector<int> arr) {
    long long min=0,max=0;
    sort(arr.begin(),arr.end());
    for(int i=0;i<4;i++){
        min+=arr[i];
    }
    for(int i=1;i<5;i++){
        max+=arr[i];
    }
    cout<<min<<" "<<max;
}
profile
코딩 창고!

0개의 댓글