#### Mini-Max Sum Hackerrank

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.

Input Format:- A single line of five space-separated integers.

Constraints:- Each integer is in the inclusive range [1,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 the 32-bit integer.)

Sample Input:- 1 2 3 4 5

Sample Output:- 10 14

Explanation:- Our initial numbers are 1, 2, 3, 4, and 5. We can calculate the following sums using four of the five integers:

If we sum everything except 5, our sum is 1+2+3+4=10.

Same goes for 4, our sum is 1+2+3+5=11.

Same goes for 3, our sum is 1+2+4+5=12.

Same goes for 2, our sum is 1+3+4+5=13.

Same goes for 1, our sum is 2+3+4+5=14.

As you can see, the minimal sum is 10 and the maximal sum is 14. Thus, we print these minimal and maximal sums as two space-separated integers on a new line.

Hints: Beware of integer overflow! Use 64-bit Integer. Visit the problem at https://www.hackerrank.com/challenges/mini-max-sum/forum

##### How?

First, let’s make some observations:

We can minimize the sum by excluding the largest element from the sum.

We can maximize the sum by excluding the smallest element from the sum.

There are five integers, and the maximum value of each integer is 10^9. This means that worst-case scenario, our final sum could be a maximum of 4*10^9(which is outside of the bounds of an integer).

**How to Solve:**

To solve this, we read in five elements; for each element:

Add the element to a running sum of all elements, sum of five.

If the element is less than the current minimum, min, set the element as the new current minimum.

If the element is greater than the current maximum, max, set the element as the new current maximum.

After checking all five elements, we have the following information:

The sum of five elements, sum of five.

The value of the smallest element, min.

The value of the largest element, max.

C++ solution:-

#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm>//For using sort element using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ long long a[5]; long long sum = 0; for(int i = 0; i < 5; i++) { cin >> a[i]; sum += a[i]; } sort(a, a+5);//Sorting Elements cout << sum - a[4] << " " << sum - a[0] << endl; return 0; }

Simple solution sort the array a[n], for maximum, subtract a[0] with the sum, and for minimum value subtract it from the large value a[n].

C++ solution II

#include <bits/stdc++.h> typedef long long LL; using namespace std; int main(){ LL s[5]; LL d = 0; for(int i = 0; i < 5; i++){ cin >> s[i]; d += s[i]; } sort(s,s+5); cout << d-s[4] << " " << d-s[0] << endl; }

##### C solution

```
int main(){
unsigned long long int a[5],max,min,sum=0;
int i;
scanf("%lld",&a[0]);
max=a[0];min=a[0];sum=a[0]+sum;
for(i=1;i<5;i++){
scanf("%lld",&a[i]);
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
sum=sum+a[i];
}
printf("%lld %lld",sum-max,sum-min);
return 0;
}
```

##### Java solution

```
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long sum = 0;
long min = 1000000000;
long max = 0;
while(scan.hasNext()) {
long n = scan.nextLong();
sum = sum + n;
if (min > n) {
min = n;
}
if (max < n) {
max = n;
}
}
scan.close();
System.out.println((sum - max) + " " + (sum - min));
}
}
```

##### Python solution

```
#!/bin/python
import sys
lst = map(int,raw_input().strip().split(' '))
x = sum(lst)
print (x-(max(lst))), (x-(min(lst)))
```

Video tutorial for this article is also available

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme