Sunday, November 18, 2018

Mini-Max Sum - Hacker Rank Solution

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.
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:
  1. If we sum everything except , our sum is .
  2. If we sum everything except , our sum is .
  3. If we sum everything except , our sum is .
  4. If we sum everything except , our sum is .
  5. 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:
  1. Add the element to a running sum of all elements, .
  2. If the element is less than the current minimum, , set the element as the new current minimum.
  3. 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

3 comments:

  1. #!/bin/python3

    import 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)

    ReplyDelete
  2. s=eval(input("Enter list of values:"))
    maxsum=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))

    ReplyDelete
  3. static void miniMaxSum(int[] arr) {
    List 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));
    }

    ReplyDelete

Powered by Blogger.