Page 1 of 1

Code is done in JAVA. Please do not rewrite this code completely or hard code the input into the code itself. The user w

Posted: Fri Apr 29, 2022 6:56 am
by answerhappygod
Code is done in JAVA. Please do not rewrite this code
completely or hard code the input into the code itself. The
user will be inputting the data.
Sum Multiplier
Have the
function SumMultiplier(arr) take the
array of numbers stored in arr and
return the string true if any two
numbers can be multiplied so that the answer is greater than double
the sum of all the elements in the array. If not, return the
string false. For example:
if arr is [2, 5, 6, -6, 16, 2, 3, 6, 5,
3] then the sum of all these elements is 42 and doubling it is 84.
There are two elements in the array, 16 * 6 = 96 and 96 is greater
than 84, so your program should return the
string true.
Completed code is below, however, it returns an error
message:
import java.util.*;
import java.io.*;
class Main {
public static String SumMultiplier(int[] arr) {
int doubleSum = Arrays.stream(arr).sum() * 2;
for (int i=0;i<arr.length-1;i++){
for (int j=i+1;j<arr.length;j++){
if(arr*arr[j] > doubleSum){
return "true";
}
}
}
return "false";
}
public static void main(String[] args) {
System.out.println(SumMultiplier(new int[] {}));
}
}
ERROR MESSAGE:
== RUNNING TEST CASES ==
== INPUT ==
new int[] {2, 2, 2, 2, 4, 1}
== OUTPUT ==
false
<< CORRECT >>
== INPUT ==
new int[] {1, 1, 2, 10, 3, 1, 12}
== OUTPUT ==
false
<< WRONG >>
<< EXPECTED OUTPUT: true >>