Skip to content
thecscience
THECSICENCE

Learn everything about computer science

  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
thecscience
THECSICENCE

Learn everything about computer science

HackerRank Mini-Max Sum Problem Solution

Yashwant Parihar, April 10, 2023April 11, 2023

In this post, We are going to solve HackerRank Mini-Max Sum 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] < 10 power 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
HackerRank-Mini-Max-Sum-Problem-Solution
HackerRank-Mini-Max-Sum-Problem-Solution

Mini-Max Sum C Solution

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

int main() {

    long long int a[5],temp;
	int i, j;
	scanf("%lld %lld %lld %lld %lld",&a[0],&a[1],&a[2],&a[3],&a[4]);
	for (i=1;i<=4;i++)
    { j=i;
    while (j>0 && (a[j-1] > a[j]))
    {
        temp= a[j];
        a[j]= a[j-1];
        a[j-1]=temp;
        j=j-1;
    }

	}
	long long int min=a[0]+a[1]+a[2]+a[3];
	long long int max=a[1]+a[2]+a[3]+a[4];
	printf("%lld %lld",min,max);    
    return 0;
}

Mini-Max Sum C++ Solution


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a[5];
    cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4];
    sort(a,a+5);
    cout<<a[0]+a[1]+a[2]+a[3]<<" "<<a[4]+a[1]+a[2]+a[3]<<endl;


    return 0;
}

Mini-Max Sum C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        string[] arr = Console.ReadLine().Split(' ');

long sum = 0;
List<long> dizi = new List<long>();
for (int i = 0; i < arr.Length; i++)
{
long temp = Convert.ToInt64(arr[i]);
dizi.Add(temp);
}
for (int i = 0; i < dizi.Count; i++)
{
sum = sum + dizi[i];
}


for (int i = 0; i < dizi.Count - 1; i++)
{
for (int j = 1; j < dizi.Count - i; j++)
{
if (dizi[j] < dizi[j - 1])
{
long gecici = dizi[j - 1];
dizi[j - 1] = dizi[j];
dizi[j] = gecici;
}
}
}
long max = sum - dizi[0];
long min = sum - dizi[4];
Console.WriteLine("{0} {1}", min, max);
Console.ReadKey();
    }
}

Mini-Max Sum Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'miniMaxSum' function below.
     *
     
     256741038 623958417 467905213 714532089 938071625
     
     2063136757 2744467344
     * The function accepts INTEGER_ARRAY arr as parameter.
     */

    public static void miniMaxSum(List<Integer> arr) {
        int max = -1;
        int min = Integer.MAX_VALUE; ;
        long sumMax = 0;
        long sumMin = 0;
        long sum = 0;
        
        int numberChecked =0;
        
        for(int el : arr){
            max = Math.max(el,max);
            min = Math.min(el,min);
            sum+=el;
        }
        
        if(min==max){
            System.out.print(sum -max);
            System.out.print(" ");
            System.out.print(sum -max);
            return;
        }
        
        for(int el : arr){
            if(el!=max) sumMin+=el;
            if(el!=min) sumMax+=el;
        }

        System.out.print(sumMin);
        System.out.print(" ");
        System.out.print(sumMax);
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        Result.miniMaxSum(arr);

        bufferedReader.close();
    }
}

Mini-Max Sum JavaScript Solution

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////
    
Array.prototype.min = function () {
    var min = 777777777777;
    for (var i = 0; i < this.length; i++) {
        if (this[i] < min) min = this[i];
    }
    
    return min;
}

Array.prototype.max = function () {
    var max = 0;
    for (var i = 0; i < this.length; i++) {
        if (this[i] > max) max = this[i];
    }
    
    return max;
}

Array.prototype.sum = function () {
    return this.reduce((x, y) => { return x + y;});
}

function main() {
    var vals = readLine().split(' ').map((i) => {return parseInt(i);});
    //console.log(vals);

    var lo = vals.sum() - vals.max();
    var hi = vals.sum() - vals.min();

    console.log(lo.toString() + ' ' + hi.toString())
}

Mini-Max Sum Python Solution

def swap(arr,x,y):
    mem = arr[x]
    arr[x] = arr[y]
    arr[y] = mem
    return arr

def qsort(ar,start,end):
    # base case
    if end-start <= 0:
        return ar
    # pivot
    pivot = ar[end]
    # init index for pivot
    ind = start
    # loop less final value (pivot)
    for i in range(start,end):
        if ar[i] <= pivot:
            ar = swap(ar,i,ind)
            ind += 1
        else:
            pass
    swap(ar,ind,end)
    # left side
    ar = qsort(ar,start,ind-1)
    # right side
    ar = qsort(ar,ind+1,end)
    return ar

ar=[int(a) for a in input().strip().split(" ")]
ar=qsort(ar,0,len(ar)-1)
print(sum(ar[:4]), sum(ar[1:]))

Other Solutions

  • HackerRank Birthday Cake Candles Solution
  • HackerRank Time Conversion Problem Solution
c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython

Post navigation

Previous post
Next post

Leave a Reply

You must be logged in to post a comment.

  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 THECSICENCE | WordPress Theme by SuperbThemes