Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mini-Max Sum
# Challenge: Mini-Max Sum - HakerRank
# Author: Stella Miranda
# 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.
# Input Format
# A single line of five space-separated integers.
# Constraints
# Each integer is in the inclusive range [1,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 32 bit integer.)
# Sample Input
# 1 2 3 4 5
# Sample Output
# 10 14
a,b,c,d,e = gets.strip.split(' ')
a = a.to_i
b = b.to_i
c = c.to_i
d = d.to_i
e = e.to_i
sum = []
sum << a+b+c+d
sum << a+b+c+e
sum << a+b+d+e
sum << a+c+d+e
sum << b+c+d+e
sum.sort!
puts sum[0].to_s+" "+sum[4].to_s
@ashishsingh1298

This comment has been minimized.

Copy link

ashishsingh1298 commented Mar 27, 2020

#include
using namespace std;
#include <stdint.h>

void miniMaxSum(int64_t arr[])
{
int64_t sum[5]={};
for(int i=0;i<5;i++)
{
if((arr[i]<1) || (arr[i]>1000000000))
exit(0);
int64_t temp=0;
for(int j=0;j<5;j++)
{
if(j!=i)
{
temp +=arr[j];
}
}
sum[i]=temp;
}
int64_t res=sum[0];
for(int i=0;i<5;i++)
res =min(res,sum[i]);
cout<<res<<" ";

res=sum[0];
for(int i=0;i<5;i++)
    res =max(res,sum[i]);
cout<<res<<" ";

}
int main()
{
int64_t arr[5]={};
for(int i=0;i<5;i++)
{
cin>>arr[i];
}

miniMaxSum(arr);
return 0;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.