Mini-Max Sum – HackerRank Solution

Mini-Max Sum – HackerRank Solution

Problem :

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.

Example :

arr = [1, 3, 5, 7, 9]

The minimum sum is 1 + 3 + 5 + 7 = 16 and the maximum sum is 3 + 5 + 7 + 9 = 24. The function prints

16 24

Function Description :

Complete the miniMaxSum function in the editor below.

miniMaxSum has the following parameter(s):

  • arr: an array of 5 integers

Print :

Print two space-separated integers on one line: the minimum sum and the maximum sum of 4 of 5 elements.

Input Format :

A single line of five space-separated integers.

Constraints :

1 <= arr[i] <= 10^9

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 <bits/stdc++.h>
using namespace std;
vector<string> split_string(string);
// Complete the miniMaxSum function below.
void miniMaxSum(vector<int> arr) {
    long sum = 0;
    int count = 0;
    int size = arr.size();
    vector<long> num;
    
    for (int i = 0; i < size; i++) {
        for (int a = 0; a < size; a++) {
            if (a != i) {
                sum += arr[a];
            }
        }
        num.push_back(sum);
        sum = 0;
    }
    long min = *min_element(num.begin(), num.end()); 
    long max = *max_element(num.begin(), num.end()); 
    cout << min << ' ' << max << endl; 
}
int main()
{
    string arr_temp_temp;
    getline(cin, arr_temp_temp);
    vector<string> arr_temp = split_string(arr_temp_temp);
    vector<int> arr(5);
    for (int i = 0; i < 5; i++) {
        int arr_item = stoi(arr_temp[i]);
        arr[i] = arr_item;
    }
    miniMaxSum(arr);
    return 0;
}
vector<string> split_string(string input_string) {
    string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
        return x == y and x == ' ';
    });
    input_string.erase(new_end, input_string.end());
    while (input_string[input_string.length() - 1] == ' ') {
        input_string.pop_back();
    }
    vector<string> splits;
    char delimiter = ' ';
    size_t i = 0;
    size_t pos = input_string.find(delimiter);
    while (pos != string::npos) {
        splits.push_back(input_string.substr(i, pos - i));
        i = pos + 1;
        pos = input_string.find(delimiter, i);
    }
    splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
    return splits;
}

 45 total views,  2 views today

Post Disclaimer

the above hole problem statement is given by hackerrank.com but the solution is generated by the SLTECHACADEMY authority if any of the query regarding this post or website fill the following contact form thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *