February 18, 2018 Andrey

Min Max Sum from hackerrank on C++

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.

Full question is over here:hackerrank

Working solution

#include <bits/stdc++.h>

using namespace std;

void miniMaxSum(vector <int> arr) {
    // Complete this function
    long long min64bit = LLONG_MAX;
    long long max64bit = 0;
    long long temp64bit = 0;
    for (int i=0; i<arr.size(); i++){
        //sum everything but i
        temp64bit = 0;
        for (int j=0; j<arr.size(); j++){
            if (i==j)
                continue;
            temp64bit +=arr[j];
        }
        if (temp64bit > max64bit)
            max64bit = temp64bit;
        if (temp64bit < min64bit)
            min64bit = temp64bit;
    }
    cout<<min64bit<<" "<<max64bit;
}

int main() {
    vector<int> arr(5);
    for(int arr_i = 0; arr_i < 5; arr_i++){
       cin >> arr[arr_i];
    }
    miniMaxSum(arr);
    return 0;
}
,