Java > Logic-2 > evenlySpaced (CodingBat Solution)

Problem:

Given three ints, a b c, one of them is small, one is medium and one is large. Return true if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large.

evenlySpaced(2, 4, 6) → true
evenlySpaced(4, 6, 2) → true
evenlySpaced(4, 6, 3) → false


Solution:

public boolean evenlySpaced(int a, int b, int c) {
  int[] anArray = {a, b, c};
  Arrays.sort(anArray);

  int diffLow = anArray[1] - anArray[0];
  int diffHi = anArray[2] - anArray[1];

  if (diffLow == diffHi)
    return true;
  else
    return false;  
}


8 comments :

  1. public boolean evenlySpaced(int a, int b, int c) {

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


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


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

    if(a==b&&b==a&&c==a&&b==c){
    return true;
    }
    if (c>a&&c>b&&b>a){
    return b-a==c-b;
    }

    return false;

    }

    ReplyDelete
  2. public boolean evenlySpaced(int a, int b, int c) {
    int[] anArray = {a, b, c};
    Arrays.sort(anArray);

    int diffLow = anArray[1] - anArray[0];
    int diffHi = anArray[2] - anArray[1];

    return diffLow == diffHi;
    }

    ReplyDelete
  3. public boolean evenlySpaced(int a, int b, int c) {
    return isSpaced(a, b, c) || isSpaced(a, c, b) || isSpaced(c, b, a);
    }

    public boolean isSpaced(int a, int b, int c) {
    return (a + b) == 2 * c;
    }

    ReplyDelete
  4. above method is short and sweet but below is alternative for begineers.

    if (a > b) { //e.g (4, 6, 2) : I would swap the value to arrange in descending order to find the small, medium large value for right difference
    int temp = a; // temp == 4 && a == empty
    a = b; // a == 2 && b == empty
    b = temp; //b == 4
    // now a and b are in descinding order : (4,2,6) == (2,4,6)
    }

    if (b > c){ //(4, 6, 2) is 6 > 2 ? yes
    int temp = b; // temp = 6
    b = c; // b = 2
    c = temp; // c = 6
    } // (4,6,2) == (4,2,6)

    // now since b value is change, validate again a > b
    if (a > b) { //e.g (4,2,6) :
    int temp = a; // temp == 4 && a == empty
    a = b; // a == 2 && b == empty
    b = temp; //b == 4
    // now a and b are in descinding order : (4,2,6) == (2,4,6)
    }

    return Math.abs(a-b) == Math.abs(b-c);
    }

    ReplyDelete
  5. return b-a==c-b ? true: a-c==c-b ? true : b-a==a-c;

    ReplyDelete
  6. public boolean evenlySpaced(int a, int b, int c) {
    if(a+b==2*c || a+c==2*b || b+c==2*a){
    return true;
    }
    return false;
    }

    ReplyDelete
  7. public boolean evenlySpaced(int a, int b, int c) {
    return (a == c) && (a != b)
    || (a + b) == 2 * c
    || (b + c) == 2 * a
    || (c + a) == 2 * b;

    }

    ReplyDelete
  8. public boolean evenlySpaced(int a, int b, int c) {
    if (b < a) {
    b = a ^ b;
    a = a ^ b;
    b = a ^ b;
    }
    if (b > c) {
    c = b ^ c;
    b = b ^ c;
    c = b ^ c;
    }

    if (b < a) {
    b = a ^ b;
    a = a ^ b;
    b = a ^ b;
    }

    return c - b == b - a;
    }

    ReplyDelete

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