+
>write a separate helper method which finds the largest special score in an array
actually, the answer is fully given there, apart from actually giving out the code itself.
public int scoresSpecial(int[] a, int[] b) {
int result1=0;
int result2=0;
int initialA=0;
int intitiaB=0;
for(int i=0;i<a.length;i++){
if(a[i]%10==0){
result1=result1+a[i];
}
}
for(int j=0;j<b.length;j++){
if(b[j]%10==0){
result2=result1+b[j];
}
}
return (result1+result2);
}
xpected Run
scoresSpecial([12, 10, 4], [2, 20, 30]) → 40 50 X
scoresSpecial([20, 10, 4], [2, 20, 10]) → 40 70 X
scoresSpecial([12, 11, 4], [2, 20, 31]) → 20 20 OK
scoresSpecial([1, 20, 2, 50], [3, 4, 5]) → 50 70 X
scoresSpecial([3, 4, 5], [1, 50, 2, 20]) → 50 20 X
scoresSpecial([10, 4, 20, 30], [20]) → 50 140 X
scoresSpecial([10, 4, 20, 30], [20]) → 50 140 X
scoresSpecial([10, 4, 20, 30], [3, 20, 99]) → 50 140 X
scoresSpecial([10, 4, 20, 30], [30, 20, 99]) → 60 140 X
scoresSpecial([], [2]) → 0 0 OK
scoresSpecial([], [20]) → 20 20 OK
scoresSpecial([14, 10, 4], [4, 20, 30]) → 40 50 X
other tests
X
public int scoresSpecial(int[] a, int[] b) {
int result1=0;
int result2=0;
int initialA=0;
int initialB=0;
for(int i=0;i<a.length;i++){
if(a[i]%10==0&&initialA<a[i]){
initialA=a[i];
result1=initialA;
}
}
for(int j=0;j<b.length;j++){
if(b[j]%10==0&&initialB<b[j]){
initialB=b[j];
result2=initialB;
}
}
return (result1+result2);
}
something like above. Above passes all tests.public int scoresSpecial(int[] a, int[] b) {
return (scoresUtil(a)+scoresUtil(b));
}
public int scoresUtil(int[] x){
int result1=0;
int result2=0;
int initialA=0;
int initialB=0;
for(int i=0;i<x.length;i++){
if(x[i]%10==0&&initialA<x[i]){
initialA=x[i];
result1=initialA;
}
}
return result1;
}
more refactored to helper method as above.
public int scoresSpecial(int[] a, int[] b) {
return (scoresUtil(a) + scoresUtil(b));
}
public int scoresUtil(int[] x){
int largest = 0;
for(int i = 0; i < x.length; i++){
if(x[i]%10 == 0 && largest < x[i]){
largest = x[i];
}
}
return largest;
}
So what's the difference now?The difference is that I was just rewriting gudii9 code that he posted at
Title | # Comments | Views | Activity |
---|---|---|---|
endX challenge | 2 | 50 | |
nestparen challenge | 4 | 57 | |
groupSum6 challenge | 6 | 48 | |
Problem to open Excel file | 15 | 45 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
16 Experts available now in Live!