Mini-Max Sum on Hacker Rank C++

mini max sum hackerrank solution in java
mini max sum hackerrank solution in c github
mini-max sum hackerrank solution in python
mini-max sum hackerrank solution in c#
mini max sum hackerrank solution in c
mini-max sum hackerrank solution in javascript
minimum sum hackerrank solution
max min hackerrank solution java

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.

void min_max(unsigned  long  int arr[]){
            unsigned  long  int max = arr[0];
            unsigned  long  int min = arr[0];
            for(int i=1; i<5; i++){
            if(arr[0] > arr[i]) min = arr[i];
            if(arr[0] < arr[i]) max = arr[i];
        }
        cout << min << " " << max;
    }

unsigned  long  int Sum(unsigned  long  int arr[], int n){
    unsigned  long  int sum = 0;
    for(int i = 0; i<5; i++){
        sum += arr[i];
    }
    sum = sum - arr[n];
    return sum;
}

int main(){
    unsigned  long  int arr[5];
    for(int i=0; i<5; i++){
        cin >> arr[i];
    }

    unsigned  long  int SUM[5];
    for(int j=0; j<5; j++){
        SUM[j] = Sum(arr, j);
    }
    min_max(SUM);
}

If the input number is small, then my code has no problem, but when, let say, some really big numbers, like 293247447, then it says the test cases had failed. I don't know why, can someone help me please?

Your min_max function is wrong. You're always comparing against arr[0], which will only return the correct result if arr[0] is the smallest/second smallest (for min) or largest/second largest (for max) of the values.

The correct comparisons are

        if (min > arr[i]) min = arr[i];
        if (max < arr[i]) max = arr[i];

Or

        if (arr[i] < min) min = arr[i];
        else if (arr[i] > max) max = arr[i];

which makes it slightly easier to understand what is being done. The else is optional but avoids the second comparison if the first is true (because the second will always be false if the first is true).

Mini-Max Sum Discussions | Algorithms, Yes I think that's why I believe a C/C++ solution iterating through the values once, performing summing, min'ing, and max'ing should be a lot faster. Although I� The minimum sum is and the maximum sum is . 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 integers ; Print Print two space-separated integers on one line: the minimum sum and the maximum sum of of elements.

You should know your algorithms. First sort the array with std::sort, then accumulate the 3 mid elements with std::accumulate and then add the first resp. last element:

#include <algorithm>
#include <array>
#include <iostream>
using std::array;
using std::cin;

int main() {
    std::array<unsigned long int, 5> arr;
    for(auto &el : arr){
        cin >> el;
    }

    std::sort(arr.begin(), arr.end());
    auto sum = std::accumulate(arr.begin() + 1, arr.end() - 1, 0);
    std::cout << arr[0] + sum << ' ' << arr[4] + sum;
}

How To Solve Mini-Max Sum HackerRank Problem [Trick Revealed , In this video, I have explained hackerrank mini-max sum solution algorithm. hackerrank mini Duration: 11:23 Posted: Feb 2, 2019 Find out the Mini-Max Sum Hackerrank Solution in C++| This Find out the Mini-Max Sum Hackerrank Solution in C++| problem is very popular and important in hacker rank. In min-max sum problem, we have 5 number as input and we have to find the sum of 4 elements in this way that 4 number out of 5 number sum should be maximum and same for a minimum

Easy solution you will ever see , just two loops only but maybe high complexity O(N*N)

void miniMaxSum(vector<int> arr)
{
    // LLONG_MAX --> set min to the highest value in long long 
    // LLONG_MIN --> set max to the lowest value in long long but why lowest value ? because you want highest value
    long long min = LLONG_MAX , max = LLONG_MIN , sum ;
    for(int i = 0 ;i < arr.size() ; ++i)
    {
        sum = 0;
        for(int j = 0; j < arr.size() ; ++j)
        {
            if(i != j)
                sum += arr[j];
        }

        if(sum > max)
            max = sum;

        if (sum < min)
            min = sum;
    }

    cout << min << " " << max << endl;
}

Hacker Rank Solutions: Mini-Max Sum, Mini-Max Sum - Hacker Rank Solution Given five positive integers, find the minimum and maximum values that can be calculated by summing� In this video tutorial, I have explained hackerrank mini-max sum solution algorithm. hackerrank mini-max sum problem can be solved by using one for loop. The complexity of mini-max sum hackerrank solution is O (n). If interested to know more about the generic algorithm in details-

HackerRank 'Mini-Max Sum' Solution, Then print the respective minimum and maximum values as a single line of two space-separated long integers. Link Mini-Max Sum Complexity:� Find the maximum and minimum values obtained by summing four of five integers.

Mini-Max Sum Hackerrank Solution in C++, Free Download Most Popular 500+ Programs with Solutions in C, CPP, and Java. Get a Complete Hackerrank 30 Days of Code Solutions in C� Sum and Difference of Two Numbers. Easy Max Score: 5 Success Rate: 93.78%. Solve Challenge. Functions in C. Easy Max Score: 10 Success Rate: 95.69%. Solve Challenge

Mini-Max Sum on Hacker Rank C++, Your min_max function is wrong. You're always comparing against arr[0] , which will only return the correct result if arr[0] is the smallest/second� You may try in c++: Bubble sorting the elements: Then Summing and printing the max and min: When you subtract the maximum value of the array from the sum you will get the minimum sum and if you subtract theminimum value of the array from the sum you will get the maximum sum.

Comments
  • cin is the standard input syntax of C++
  • but i can't write it on the Ask Question section, don't know why though
  • Note that if this was in an interview, the interviewer would ask "what if there were a thousand numbers, and you are to pick the 4 that adds to the min and max"? I'm surprised hackerrank didn't throw that wrinkle into the question, as your solution with only 5 elements would probably time out.
  • @PaulMcKenzie so what should i do if the interviewer asked me that, I was thinking about changing the for(int i=1; i<5; i++) to for(int i=1; i<n; i++), while n is any number the interviewer want me to input? is that correct or not? thank you
  • If n is 1000, you would be looping a million times, i.e. O(n*n). That is not efficient. Anytime you are writing nested loops to solve a problem, it is usually not going to be efficient. You should strive to get a logarithmic or even linear approach to the problem, not n*n. The answer (which you didn't accept) of first sorting the data and simply taking the "middle" terms is a better approach.