I wrote codes for the following question but the output is not as expected. I don’t know if there is something wrong with my code. Logic seems fine. Can anyone see if there is something wrong with my code.
Given an array of scores sorted in increasing order, return true if the array contains 3 adjacent scores that differ from each other by at most 2, such as with {3, 4, 5} or {3, 5, 5}.
My source code is as follows:
public boolean scoresClump(int[] scores) {
boolean result = false;
for(int i=0; i<scores.length-2; i++){
if((scores[i+1]-scores[i])<=2 && (scores[i+2]-scores[i+1])<=2){
result = true;
break;
}
}
return result;
}
This is the link to the question.
Here you go. Your previous post was not taking into account the delta between the non-adjacent values.