Question
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
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
The minimum sum is and the maximum sum is . 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 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
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
Explanation
The numbers are , , , , and . Calculate the following sums using four of the five integers:
- Sum everything except , the sum is .
- Sum everything except , the sum is .
- Sum everything except , the sum is .
- Sum everything except , the sum is .
- Sum everything except , the sum is .
Hints: Beware of integer overflow! Use 64-bit Integer.
Here is my solution: It fails 10/15 test cases. for instance input 256741038 623958417 467905213 714532089 938071625, Output supposes to be 2063136757 2744467344. I don't know how to handle when integer overflow. Need help, please!
.*;
importjava.util.concurrent.*;
importjava.util.function.*;
importjava.util.regex.*;
importjava.util.stream.*;
importstaticjava.util.stream.Collectors.joining;
importstaticjava.util.stream.Collectors.toList;
classResult{
/*
*Completethe'miniMaxSum'functionbelow.
*
*ThefunctionacceptsINTEGER_ARRAYarrasparameter.
*/
publicstaticvoidminiMaxSum(Listarr){
//Writeyourcodehere
intmin=0;
intmax=0;
inttotal=0;
min=arr.get(0);
max=arr.get(0);
for(inti=0;i
if(arr.get(i)>max){
max=arr.get(i);
}
if(arr.get(i)
min=arr.get(i);
}
total+=arr.get(i);
}
intminsum=total-max;
intmaxsum=total-min;
//System.out.println(min+""+max);
System.out.println(minsum+""+maxsum);
}
}
publicclassSolution{
publicstaticvoidmain(String[]args)throwsIOException{
BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(System.in));
Listarr=Stream.of(bufferedReader.readLine().replaceAll("s+$","").split(""))
.map(Integer::parseInt)
.collect(toList());
Result.miniMaxSum(arr);
bufferedReader.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
It appears that there is a problem with the miniMaxSum methods reasoning You are correctly computing ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started