Code is done in JAVA. Please DO NOT post answers with a different language. Sum Multiplier Have the function SumMultipli
Posted: Fri Apr 29, 2022 6:42 am
Code is done in JAVA. Please DO
NOT post answers with a different language.
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.
Examples
Input: new int[] {2, 2, 2, 2, 4, 1}
Output: false
Input: new int[] {1, 1, 2, 10, 3, 1, 12}
Output: true
__________________________________________
Could somebody please help me fix the errors in this code? I
have less than 20 hours to submit this code and am stumped in how
to fix it. Please do not completely rewrite the code and only fix
the errors if possible:
import java.util.*;
import java.io.*;
class Main {
public static int SumMultiplier(int[] arr) {
int doubleSum = Arrays.stream(arr).sum() * 2;
for (int i=0;i<arr.length;i++){
for (int j=0;j<arr.length;j++){
if(i!=j && arr*arr[j]>doubleSum){
return "True";
}
}
}
return "False";
}
public static void main (String[] args) {
Scanner s = new Scanner(System.in);
System.out.print(SumMultiplier(s.nextLine()));
}
}
Main.java:12: error: incompatible types: String cannot
be converted to int
return "True";
^
Main.java:16: error: incompatible types: String cannot be converted
to int
return "False";
^
2 errors
NOT post answers with a different language.
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.
Examples
Input: new int[] {2, 2, 2, 2, 4, 1}
Output: false
Input: new int[] {1, 1, 2, 10, 3, 1, 12}
Output: true
__________________________________________
Could somebody please help me fix the errors in this code? I
have less than 20 hours to submit this code and am stumped in how
to fix it. Please do not completely rewrite the code and only fix
the errors if possible:
import java.util.*;
import java.io.*;
class Main {
public static int SumMultiplier(int[] arr) {
int doubleSum = Arrays.stream(arr).sum() * 2;
for (int i=0;i<arr.length;i++){
for (int j=0;j<arr.length;j++){
if(i!=j && arr*arr[j]>doubleSum){
return "True";
}
}
}
return "False";
}
public static void main (String[] args) {
Scanner s = new Scanner(System.in);
System.out.print(SumMultiplier(s.nextLine()));
}
}
Main.java:12: error: incompatible types: String cannot
be converted to int
return "True";
^
Main.java:16: error: incompatible types: String cannot be converted
to int
return "False";
^
2 errors