HackerRank Mini-Max Sum Problem Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Home Contact About Privacy DMCA   

CLOSE ADS CLOSE ADS

Home HackerRank Solutions  Leetcode Solutions Programs  CS Topics  

Home  coding problems  HackerRank Mini-Max Sum problem solution

HackerRank Mini-Max Sum problem solution


 YASH PAL  March 23, 2021

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

Search

Subscribe To Channel
Example
Programmingoneonone

arr = [1,3,5,7,9] YouTube 797

The minimum sum is 1+3+5+7 = 16 and the maximum sum is 3+5+7+9 = 24. The
function prints


Learn DSA For Free
16 24

Function Description
CLOSE ADS CLOSE ADS

Complete the miniMaxSum function in the editor below.

miniMaxSum has the following parameter(s):

arr: an array of  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.)

19% OFF 41% OFF 14% OFF 20% OFF

Most Popular Content

HackerRank Mini-Max Sum


problem solution
 March 23, 2021

18% OFF 19% OFF 17% OFF 17% OFF


HackerRank Plus Minus
problem solution
 March 23, 2021

HackerRank Time Conversion


problem solution
 March 23, 2021

HackerRank Diagonal
Difference problem solution
 March 23, 2021

HackerRank Simple Array Sum


problem solution
 March 23, 2021


Crafted with  by TemplatesYard | Distributed by Blogger

CLOSE ADS CLOSE ADS

19% OFF 41% OFF 14% OFF 20% OFF

18% OFF 19% OFF 17% OFF 17% OFF

Problem solution in Python programming.


Code
#!/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)

HackerRank Mini-Max Sum problem solution in Python Pro…


Pro…


CLOSE ADS CLOSE ADS

Problem solution in Java Programming.


Code
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.


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

CLOSE ADS CLOSE ADS


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.


Code
#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;
CLOSE ADS CLOSE ADS
}

Problem solution in JavaScript programming.


Code
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); 
});
CLOSE ADS CLOSE ADS

Tags: algorithm coding problems

 Facebook  Twitter    

Posted by: YASH PAL


Yash is a Full Stack web developer. he always will to help others. and this approach
takes him to write this page.

You may like these posts

HackerRank Smart Number HackerRank XOR Strings 2


HackerRank Prime Dates problem solution problem solution
problem solution  July 29, 2021  July 29, 2021
 April 15, 2022

Post a Comment

1 Comments

GAJANAN KULKARNI
 January 20, 2022 at 10:47 PM

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 Delete

 Replies
Reply

Add comment
To leave a comment, click the button below to sign in with Blogger.

SIGN IN WITH BLOGGER

You might also like