Mini Max Sum HackerRank Solution in C, C++, Java, Python

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  of  elements.

Input Format

A single line of five space-separated integers.

Constraints

1<=arr[i]<=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 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 .
  2. Sum everything except 2, the sum is 1+3+4+5.
  3. Sum everything except 3, the sum is 1+2+4+5.
  4. Sum everything except 4, the sum is 1+2+3+5.
  5. Sum everything except 5, the sum is 1+2+3+4.

Hints: Beware of integer overflow! Use 64-bit Integer.

Mini Max Sum HackerRank Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

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

Mini Max Sum HackerRank Solution in C++

#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 HackerRank Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long sum = 0;
        long max = Long.MIN_VALUE;
        long min = Long.MAX_VALUE;
        for (int i = 0; i < 5; i++){
            long n = in.nextLong();
            sum += n;
            max = Math.max(max, n);
            min = Math.min(min, n);
        }
        System.out.println((sum - max) + " " + (sum - min));
    }
}

Mini Max Sum HackerRank Solution in Python

a = sorted(map(int,raw_input().split()))
print sum(a[:4]),sum(a[1:])

Mini Max Sum HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
    static void Main(String[] args) {
        long[] A = Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);

        long max = A.Sum(), min = max;

        max -= A.Min();
        min -= A.Max();
        Console.WriteLine(min + " " + max);
    }
}

 

Attempt – Mini Max Sum HackerRank Challenge

Link – https://www.hackerrank.com/challenges/mini-max-sum

Next Challenge – Birthday Cake Candles HackerRank Solution

Link – https://exploringbits.com/birthday-cake-candles-hackerrank-solution/

 

2 thoughts on “Mini Max Sum HackerRank Solution in C, C++, Java, Python”

  1. public static void miniMaxSum(List arr) {
    Collections.sort(arr);
    long min = 0;
    long max = 0;
    int count = 0;
    int len = arr.size();
    do{
    min +=arr.get(count);
    max += arr.get(len-1);
    count++;
    len–;
    }while(count < 4);
    System.out.println(min + " " + max);
    }

    Reply
  2. For JavaScript
    /*
    * Complete the ‘miniMaxSum’ function below.
    *
    * The function accepts INTEGER_ARRAY arr as parameter.
    */

    function miniMaxSum(arr) {
    // Write your code here
    let total = [];
    let temp = 0 ;
    for (let i=0; i< arr.length; i++){
    temp = arr.reduce(function(a,b){return a+b},-arr[i]);
    total.push(temp);
    }
    console.log(Math.min(…total)+" "+Math.max(…total));
    }

    Reply

Leave a Comment