Java > Logic-1 > inOrderEqual (CodingBat Solution)

Problem:

Given three ints, a b c, return true if they are in strict increasing order, such as 2 5 11, or 5 6 7, but not 6 5 7 or 5 5 7. However, with the exception that if "equalOk" is true, equality is allowed, such as 5 5 7 or 5 5 5.

inOrderEqual(2, 5, 11, false) → true
inOrderEqual(5, 7, 6, false) → false
inOrderEqual(5, 5, 7, true) → true


Solution:

public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
  if (!equalOk && a < b && b < c)
    return true;
  if (equalOk && a <= b && b <= c)
    return true;
  else
    return false;
}

6 comments:

  1. return (!equalOk) ? a < b && b < c : a <= b && b <= c;

    ReplyDelete
  2. Good compromise of ease of understanding logic and verbosity -


    if (equalOk) {
    return c >= b && b >= a;
    } else {
    return c > b && b > a;
    }

    ReplyDelete
  3. return (equalOk && a <= b && b <= c) || (!equalOk && a < b && b < c);

    ReplyDelete
  4. public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
    return (a < b && b < c) != (equalOk && a <= b && b <= c);
    }

    ReplyDelete
  5. if((a % 10 == b % 10)||(a % 10 == c % 10) || (b % 10 == c % 10)) return true;
    else return false;

    ReplyDelete
  6. return((c>b&&b>a)||(c>=b&&b>=a&&equalOk));

    ReplyDelete