Java > AP-1 > scoresClump (CodingBat Solution)

Problem:

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}.

scoresClump({3, 4, 5}) → true
scoresClump({3, 4, 6}) → false
scoresClump({1, 3, 5, 5}) → true


Solution:

public boolean scoresClump(int[] scores) {
  if (scores.length < 3)
    return false;
  
  for (int i = 0; i < scores.length-2; i++) {
    if (scores[i+2] - scores[i+1] <= 2 && scores[i+2] - scores[i] <= 2)
      return true;
  }
  return false;
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact