In this HackerRank Mini-Max Sum problem you have 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.

HackerRank Mini-Max Sum problem solution


Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the miniMaxSum function below.
def miniMaxSum(arr):
    arr.sort()
    hold = [None]*int(len(arr)-3)
    for i in range(0,len(arr)-3):
        temp = 0
        for j in range(i,i+4):
            temp = temp + arr[j]
        hold[i] = temp
    
    print(hold[0],hold[-1])


if __name__ == '__main__':
    arr = list(map(int, input().rstrip().split()))

    miniMaxSum(arr)




Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        long[] m=new long[5];
        for(int i=0;i<5;i++){
            m[i]=in.nextLong();
        }
        Arrays.sort(m);
        long x=0;
        long y=0;
        for(int i=0;i<4;i++){
            x+=m[i];
        }
        for(int i=1;i<5;i++){
            y+=m[i];
        }
        System.out.println(x+" "+y);
    }
}


Problem solution in C++ programming.

#include <algorithm>
#include <string.h>
#include <vector>
#include <cstdio>
#include <climits>
#include <iostream>
using namespace std;
typedef long long lld;

lld arr[6], N = 5;

int main ()
{
    //freopen("input.txt", "r", stdin);

    lld allsum = 0;
    lld MN = LLONG_MAX, MX = LLONG_MIN;

    for (int i=1; i<=N; i++)
    {
        scanf("%lld", &arr[i]);
        allsum += arr[i];
    }

    for (int i=1; i<=N; i++)
    {
        lld cur = allsum - arr[i];
        MN = min(MN, cur);
        MX = max(MX, cur);
    }

    printf("%lld %lld\n", MN, MX);
}


Problem solution in C programming.

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

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


Problem solution in JavaScript programming.

function processData(input) {
    var v = input.split(' ');
    for (var i = 0; i < v.length; i++) {
        v[i] = parseInt(v[i])
    }
    var max = -Infinity;
    var min = Infinity;
    for (var i = 0; i < v.length; i++) {
        var sum = 0;
        for (var j = 0; j < v.length; j++) {
            if ( i != j ) {
                sum += v[j];
            }
        }
        if (sum < min) min = sum;
        if (sum > max) max = sum;
    }
    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);
});