Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.
public boolean scoresIncreasing(int[] scores) {
boolean result=false;
int len=scores.length;
for(int i=0;i<len;i++){
if(scores[i+1]>scores[i]&&scores[i+2]>scores[i+1]){
result=true;
return result;
}
}
return result;
}
Expected Run
scoresIncreasing([1, 3, 4]) → true true OK
scoresIncreasing([1, 3, 2]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 3 (line number:5) X
scoresIncreasing([1, 1, 4]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 3 (line number:5) X
scoresIncreasing([1, 1, 2, 4, 4, 7]) → true true OK
scoresIncreasing([1, 1, 2, 4, 3, 7]) → false true X
scoresIncreasing([-5, 4, 11]) → true true OK
Your progress graph for this problem
public boolean scoresIncreasing(int[] scores) {
boolean result=true;
int len=scores.length;
for(int i=0;i<len-2;i++){
if(scores[i+1]<scores[i]||scores[i+2]<scores[i+1]){
result=false;
return result;
}
}
return result;
}
public boolean scoresIncreasing(int[] scores) {
boolean result=true;
int len=scores.length;
for(int i=0;i<len-1;i++){
if(scores[i+1]<scores[i]){
result=false;
return result;
}
}
return result;
}
public boolean scoresIncreasing(int[] scores) {
int[] sorted = scores.clone();
Arrays.sort(sorted);
return Arrays.equals(scores, sorted);
}
public boolean scoresIncreasing(int[] scores) {
for (int i = 0; i < scores.length - 1; i++) {
if (scores[i+1] < scores[i]) {
return false;
}
}
return true;
}
public boolean scoresIncreasing(int[] scores) {
for (int i = 0; i < scores.length - 1; i++) {
if (scores[i+1] < scores[i]) {
return false;
}
}
return true;
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.