Java > AP-1 > scoresIncreasing (CodingBat Solution)

Problem:

Given an array of scores, return true if each score is equal or greater than the one before. The array will be length 2 or more.

scoresIncreasing({1, 3, 4}) → true
scoresIncreasing({1, 3, 2}) → false
scoresIncreasing({1, 1, 4}) → true


Solution:

public boolean scoresIncreasing(int[] scores) {
  boolean match = false;
  for (int i = 0; i < scores.length-1; i++) {
    if (scores[i+1] >= scores[i]) 
      match = true;
    else
      return false;
  }
  return match;
}

11 comments:

  1. public boolean scoresIncreasing(int[] nums) {
    for (int i = 0; i < nums.length; i++) {
    if(i+1nums[i+1]){
    return false;

    }
    }
    }
    return true;
    }

    ReplyDelete
  2. public boolean scoresIncreasing(int[] nums) {
    for (int i = 0; i < nums.length; i++) {
    if(i+1nums[i+1]){
    return false;

    }
    }
    }
    return true;
    }

    ReplyDelete
  3. Don't know why didn't my code uploaded successfully that's why i am trying it for third time........
    public boolean scoresIncreasing(int[] scores) {
    for (int i = 0; i < scores.length; i++) {
    if(i+1scores[i+1]){
    return false;

    }
    }
    }
    return true;
    }

    ReplyDelete
    Replies
    1. Still not done...Please correct it from line 3
      if(i+1scores[i+1]){
      return false;}}

      Delete
  4. public boolean scoresIncreasing(int[] scores) {
    for(int i = 0; i< scores.length-1;i++){
    if(scores[i]>scores[i+1]){
    return false;
    }
    }
    return true;
    }

    ReplyDelete
  5. won't the code return b at the end regardless of if the else statement executed or not?

    ReplyDelete
  6. wouldn't the return b at the end execute regardless if the else statement executed or not?

    ReplyDelete
  7. int holder = scores[0];
    int tick = 0;
    for( int i = 0; i < scores.length; i++) {

    if(scores[i] >= holder) {
    holder = scores[i];
    tick++;
    }

    }
    if(tick == scores.length){
    return true;
    } else {
    return false;
    }

    ReplyDelete
  8. for (int i = 0; i < scores.length - 1; i++)
    if (scores[i] > scores[i+1])
    return false;
    return true;

    ReplyDelete
  9. public boolean scoresIncreasing(int[] scores)
    {
    boolean greater = false;

    for(int i = scores.length-1; i > 0; i--)
    {
    if(scores[i] >= scores[i-1]) greater = true;
    else return greater = false;
    }


    return greater;
    }

    ReplyDelete
  10. public boolean scoresIncreasing(int[] scores) {
    for(int i=0 ;iscores[i+1])
    return false;
    }
    return true;
    }

    ReplyDelete