Mini-Max Sum - Hacker Rank Solution
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.
values as a single line of two space-separated long integers.
For example, . Our minimum sum is and our maximum sum is . We would print
16 24
Function Description
Complete the miniMaxSum function in the editor below. It should print two space-separated integers on one line: the minimum sum and the maximum sum of of elements.
miniMaxSum has the following parameter(s):
- arr: an array of integers
Input Format
A single line of five space-separated integers.
Constraints
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
Explanation
Our initial numbers are , , , , and . We can calculate the following sums using four of the five integers:
- If we sum everything except , our sum is .
- If we sum everything except , our sum is .
- If we sum everything except , our sum is .
- If we sum everything except , our sum is .
- If we sum everything except , our sum is .
Hints: Beware of integer overflow! Use 64-bit Integer.
Need help to get started? Try the Solve Me First problem
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 . This means that, worst-case scenario, our final sum could be a maximum of (which is outside of the bounds of an integer).
To solve this, we read in five elements; for each element:
- Add the element to a running sum of all elements, .
- If the element is less than the current minimum, , set the element as the new current minimum.
- If the element is greater than the current maximum, , set the element as the new current maximum.
After checking all five elements, we have the following information:
- The sum of five elements, .
- The value of the smallest element, .
- The value of the largest element, .
We then use this to calculate:
- The minimum sum of four of the five elements is .
- The maximum sum of four of the five elements is .
Problem Tester's code:
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));
}
}
Mini-Max Sum - Hacker Rank Solution
#!/bin/python3
ReplyDeleteimport math
import os
import random
import re
import sys
import heapq
# Complete the miniMaxSum function below.
def miniMaxSum(arr):
M = sorted(arr,reverse = True)
m= sorted(arr)
total_sum = 0
total = 0
for i in range(len(M)-1):
total_sum = total_sum +M[i]
for k in range(len(m)-1):
total = total+m[k]
print(total,total_sum)
if __name__ == '__main__':
arr = list(map(int, input().rstrip().split()))
miniMaxSum(arr)
s=eval(input("Enter list of values:"))
ReplyDeletemaxsum=0
minsum=0
maxlist=[]
minlist=[]
for i in s:
if i!=min(s):
maxsum=maxsum+i
maxlist.append(i)
if i!=max(s):
minsum=minsum+i
minlist.append(i)
print(maxsum,tuple(maxlist))
print(minsum,tuple(minlist))
static void miniMaxSum(int[] arr) {
ReplyDeleteList obj=new ArrayList();
long sum=0L;
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length;j++){
sum+=arr[j];
}
sum=sum-arr[i];
obj.add(sum);
sum=0;
}
System.out.println(Collections.min(obj)+" "+Collections.max(obj));
}