Min-Max Sum

algorithmprogramming

In this blog post we are going to discuss an interesting problem on hacerrank available at hackerRank link

This question is taken from hackerrank warmup challange. Input statement states that you are given 5 numbers and you have to add 4 out of those 5 numbers to find the max and min values from that set of sum.

The sample input and output is given as follow

Input : A single line of 5 spaced integers

Output : 2 spaced integers containing the minimum value and maximum value

Sample input and output

Input : 1 2 3 4 5

Ouput : 10 14

Explaination : Sum of 1+2+3+4 = 10 , similarly sum of 2+3+4+5 = 14

This problem can have many solution. We will see all the possible ways, build a pseudo code and write a program related to the same using vector programming in c++.

First approach to solve the is to find the sum of all the element in a loop and use another loop to store the sum-ith element value in a different array, and finally to sort the array and take the first and last value of the array to get the max and min value of the array.

Another approach is to keep on parsing the loop containing the array values and find the min and max value while parsing the array as shown in the pseudocode

Using any of the above approach we can solve the problem stated. The program link implemented in C++ using vector programming is shown here available on github.