Algorithm


Problem Name: Algorithms - Mini-Max Sum

Problem Link: https://www.hackerrank.com/challenges/mini-max-sum/problem?isFullScreen=true

In this HackerRank Functions in Algorithms - Java programming problem 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.

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

 

 

 

 

 

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>

int main() {
    long long a[5];
    scanf("%lld %lld %lld %lld %lld", &a[0], &a[1], &a[2], &a[3], &a[4]);
    
    long long tot = 0;
    long long max = 0;
    long long min = 1000000000;
    int i;
    for (i = 0; i  <  5; ++i) {
        tot += a[i];
        if (a[i] > max) max = a[i];
        if (a[i]  <  min) min = a[i];
    }
    printf("%lld %lld", tot-max, tot-min);
}
Copy The Code & Try With Live Editor

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
using namespace std;

int main() {
  int a[5];
  for (int i = 0; i  <  5; i++) {
    cin >> a[i];
  }
  int minVal = a[0], maxVal = a[0];
  long long sum = a[0];
  for (int i = 1; i  <  5; i++) {
    minVal = min(minVal, a[i]);
    maxVal = max(maxVal, a[i]);
    sum += a[i];
  }
  cout << sum - maxVal << " " << sum - minVal << endl;
  return 0;
}
Copy The Code & Try With Live Editor

#3 Code Example with Java Programming

Code - Java Programming


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 min = Long.MAX_VALUE;
        long max = 0;
        long sum = 0;
        for(int i=0; i < 5; i++)
        {
            long curr = in.nextLong();  
            if(max < curr>
            {
                max = curr;
            }
            if(min > curr)
            {
                min = curr;
            }
            
            sum += curr;
        }
        long minSum = sum - max;//Removes the largest of the 5 numbers to get the min sum
        long maxSum = sum - min;//Removes the smallest of the 5 numbers to get the max sum
        System.out.println(minSum + " " + maxSum);
    }
}
Copy The Code & Try With Live Editor

#4 Code Example with Javascript Programming

Code - Javascript Programming


'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the miniMaxSum function below.
function miniMaxSum(arr) {
    arr.sort((a, b) => a - b)
    let first = arr.slice(0, -1);
    let last = arr.slice(1)

    let minSum = first.reduce((a, b) => a + b, 0);
    let maxSum = last.reduce((a, b) => a + b, 0);

    console.log(minSum + " " + maxSum)

}

function main() {
    const arr = readLine().split(' ').map(arrTemp => parseInt(arrTemp, 10));

    miniMaxSum(arr);
}
Copy The Code & Try With Live Editor

#5 Code Example with Python Programming

Code - Python Programming


import math
import os
import random
import re
import sys

def miniMaxSum(arr):
    l1=[]
    for i in arr:
        x=-i
        for j in arr:
            x+=j
        l1.append(x)
    print(min(l1),max(l1))    
        

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

    miniMaxSum(arr)
Copy The Code & Try With Live Editor

#6 Code Example with C# Programming

Code - C# Programming


using System;
using static System.Console;
using System.Linq;

class Solution
{

    static void Main(String[] args)
    {
        var numbers = ReadLine().Split(' ').Select(x => long.Parse(x)).ToList();
        var sumOfAllNumbers = 0L;
        var minimum = long.MaxValue;
        var maximum = 0L;
        for (int i = 0; i  <  5; i++)
        {
            sumOfAllNumbers += numbers[i];
            if (numbers[i]  <  minimum)
                minimum = numbers[i];

            if (numbers[i] > maximum)
                maximum = numbers[i];

        }
        WriteLine(string.Format("{0} {1}", sumOfAllNumbers - maximum, sumOfAllNumbers - minimum));
    }
}
Copy The Code & Try With Live Editor

#7 Code Example with PHP Programming

Code - PHP Programming



Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Staircase in Java solution in Hackerrank - Hacerrank solution Java
Next
[Solved] Birthday Cake Candles in C, C++,Java, JavaScript, Python, PHP & C# solution in Hackerrank