HackerRank Mini Max Sum Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Mini Max Sum Solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank Mini Max Sum Solution
HackerRank Mini Max Sum Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Mini Max Sum Solution

Problem

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.

Example

arr = [1, 3, 5, 7, 9]
The minimum sum is 1 + 3 +5 +7 = 16 and the maximum sum is 3 + 5 + 7 + 9 = 24. 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 5 integers

Print

Print two space-separated integers on one line: the minimum sum and the maximum sum of 4 of 5 elements.

Input Format

A single line of five space-separated integers.

Constraints

  • 1 <= arr[i] <= 109

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

The numbers are 1, 2, 3, 4, and 5. Calculate the following sums using four of the five integers:

  1. Sum everything except 1, the sum is 2 + 3 + 4 + 5 = 14.
  2. Sum everything except 2, the sum is 1 + 3 + 4 + 5 = 13.
  3. Sum everything except 3, the sum is 1 + 2 + 4 + 5 = 12.
  4. Sum everything except 4, the sum is 1 + 2 + 3 + 5 = 11.
  5. Sum everything except 5, the sum is 1 + 2 +3 + 4 = 10.

HackerRank Mini Max Sum Solution

Mini Max Sum Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
    long long int i,j,k,l,m=0,n=0,t=2000000000;
    for(i=0;i<5;i++)
        {
        scanf("%lld",&k);
        m+=k;
        if(n<k)
            {
            n=k;
        }
        if(t>k)
            {
            t=k;
        }
    }
    printf("%lld %lld",m-n,m-t);
    return 0;
}

Mini Max Sum Solution in Cpp

#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;
}

Mini Max Sum Solution in Java

import java.io.*;
import java.math.BigInteger;
import java.util.*;
 
 
class Main {
	static int ans[]=new int[1000001];
		public static void main(String[] args) throws IOException {
		InputReader in = new InputReader(System.in);
		PrintWriter pw = new PrintWriter(System.out);
int prime[]=new int[1000001];
prime[0]=1;
prime[1]=1;
for(int i=2;i*i<1000001;i++)
	if(prime[i]==0)
		for(int j=i*i;j<1000001;j+=i)
			prime[j]=1;
int n=5;
int a[]=in.nextIntArray(n);
Arrays.sort(a);
long sum=(long)a[0]+(long)a[1]+(long)a[2]+(long)a[3];
long sum1=sum+(long)a[4]-(long)a[0];
System.out.println(sum+" "+sum1);
		}	
		private static int firstOccurrenceBinarySearch(int[] source, int needle) {
		    int low = 0;
		    int high = source.length - 1;
		    int firstOccurrence = Integer.MIN_VALUE;
		    while (low <= high) {
		        int middle = low + ((high - low) >>> 1);
		        if (source[middle] == needle) {
		            // key found and we want to search an earlier occurrence
		            firstOccurrence = middle;
		            high = middle - 1;
		        } else if (source[middle] < needle) {
		            low = middle + 1;
		        } else {
		            high = middle - 1;
		        }
		    }
		    if (firstOccurrence != Integer.MIN_VALUE) {
		        return firstOccurrence;
		    }
		    return -(low + 1);  // key not found
		}
	public static	int binarySearchLastOccurrence(int arr[], int low, int high, int data)
		{
		    int mid;
		 
		    // A simple implementation of Binary Search
		    if(high >= low)
		    {
		        mid = low + (high - low)/2; // To avoid overflow
		        if((mid == high && arr[mid] == data) || (arr[mid] == data && arr[mid+1] > data))
		            return mid;
		 
		        // We need to give preference to right part of the array
		        // since we are concerned with the last occurrence
		        else if(arr[mid] <= data)
		            return binarySearchLastOccurrence(arr, mid+1, high, data);
		        else
		            // We need to search in the left half
		            return binarySearchLastOccurrence(arr, low, mid-1, data);
		    }
		    return -1;
		}
		public static long pow(long n,long p,long m)
	{
		 long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    if(result>=m)
		    result%=m;
		    p >>=1;
		    n*=n;
		    if(n>=m)
		    n%=m;
		}
		return result;
	}
	public static int BS(int val,int a[])
	{
		int low=0;
		int high=a.length-1;
		int tt=0;
		while(low<high)
		{
			int mid=(low+high)/2;
			if(a[mid]<val)
			{
				tt=low;
				low=mid+1;
			}
			else
				high=mid-1;
		}
		return low;
	}
	 
 
 
	static class Pair implements Comparable<Pair>{
		int r;
		int v;
		Pair(int mr,int er){
			r=mr;v=er;
		}
		@Override
		public int compareTo(Pair o) {
			if(o.r>this.r)
				return -1;
			else if(o.r<this.r)
				return 1;
			else
			{
				if(o.v>this.v)
					return -1;
				else
					return 1;
			}
			
			}
	}
	static class TVF implements Comparable<TVF>{
		int index,size;
		TVF(int i,int c){
			index=i;
			size=c;
		}
		@Override
		public int compareTo(TVF o) {
			if(o.size>this.size)
				return -1;
			else if(o.size<this.size)
				return 1;
			else return 0;
		}
	}
	public static long gcd(long a, long b) {
		  if (b == 0) return a;
		  return gcd(b, a%b);
		}
	static class InputReader {
 
		private InputStream stream;
		private byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;
 
		public InputReader(InputStream stream) {
			this.stream = stream;
		}
 
		public int snext() {
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}
 
		public   int nextInt() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}
 
		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}
 
		public int[] nextIntArray(int n) {
			int a[] = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}
 
		public String readString() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		}
 
		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
 
		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}
}
 
Ezoicreport this ad

Mini Max Sum Solution in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT
a = sorted(map(int,raw_input().split()))
print sum(a[:4]),sum(a[1:])

Mini Max Sum Solution using JavaScript

function processData(input) {
    var m = input.map(function(e) { return parseInt(e) })
    m.sort(function(n1, n2){
        return n1-n2;
    })
    var min = 0, max = 0;
    for(var i=0; i<5; i++){
        i!=4 && (min+=m[i]);
        i!=0 && (max+=m[i]);
    }
    console.log(min+' '+max);
} 
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});
process.stdin.on("end", function () {
   processData(_input.split(" "));
});

Mini Max Sum Solution in Scala

object Solution {
    def main(args: Array[String]) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution
*/
        val nums = io.StdIn.readLine.split(" ").map(_.toLong).sorted
        val mini = nums.take(4).sum;
        val maxi = nums.drop(1).sum;
        
        println(mini + " " + maxi)
    }
}

Mini Max Sum Solution in Pascal

{$mode objfpc}
program A;
var
  ai: array [0..5] of Int64;
  S: Int64;
  i: Integer;
  Min, Max: Int64;
  
begin
  S := 0;
  Min := MaxInt;
  Max := -1;
  for i := 1 to 5 do
  begin
    Read(ai[i]);
    S := S + ai[i];
    if Max < ai[i] then
      Max := ai[i];
     if ai[i] < Min then
       Min := ai[i];
  end;
  
  WriteLn(S - Max, ' ', S - Min);
end.

Disclaimer: This problem (Mini Max Sum) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Birthday Cake Candles Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad