## DEV Community is a community of 636,824 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

loading...

# Min-maxSum

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

Sample Input

1 2 3 4 5
Sample Output

## 10 14

Here is my solution..
i am using JAVA programming language:

static void miniMaxSum(int[] arr)
{
long min=1000000000,max=-1;
long sum=0,minS,maxS;

for(int i=0;i<arr.length;i++)
{

if(arr[i]<min)

{
min=arr[i];
}
if(arr[i]>max)
{
max=arr[i];
}
}
for(int i=0;i<arr.length;i++)
{
sum=sum+arr[i];
}
minS=sum-max;
maxS=sum-min;
System.out.println(minS+" "+maxS);
}

HappyCoding❤️
I hope you like it!

## Discussion (2) Suchitra

yeah! sure..
Here i am just design a function for MinimumSum and MaximumSum but keep in you mind that in this task the given constraints are: 1. 1<=arr[i]<1000000000(10^9)

1. we have to sum of only n-1 integers(max and mini), here n is a No. of elements in the array.

Then here ,1st you can see that output requirement in long data type right?
So, i declare my variable in long.
and here i initialize max=-1 and min=1000000000
bcoz we know the constraint range of the array elements which is 1<=arr[i]<1000000000
then i use for loop for extract elements from array and with the help of if condition
i check..this way
1st if(arr[i] This condition used for finding minimum element in the array.
2nd condition if(arr[i]>max) if true the max=arr[i]. This is used for finding max element in the array.
after finding max and min elements in array our task is to calculate sum of elements in array
so, i used another for loop for finding sum of array elements.
like this
for(int i=0;i<arr.length;i++)
{
sum=sum+arr[i];
}
finally we have sum of all the elements and also max element and mini element.
So, we can easily find the sum of minimum/maximum n-1 elements :
By subtracting from sum to max we will find miniSum and by subtracting from to min we will find miniSum.
like..
miniS=sum-max;
maxS=sum-mini;
then print them.
I hope got my point!
HappyCoding❤️