Java > Logic-2 > closeFar (CodingBat Solution)

Problem:

Given three ints, a b c, return true if one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other values by 2 or more. Note: Math.abs(num) computes the absolute value of a number.

closeFar(1, 2, 10) → true
closeFar(1, 2, 3) → false
closeFar(4, 1, 3) → true


Solution:

public boolean closeFar(int a, int b, int c) {
 
  if (Math.abs(a - b) <= 1 && Math.abs(a - c) >= 2 && Math.abs(b - c) >= 2){
      return true;
  } else if (Math.abs(a - c) <= 1 && Math.abs(a - b) >= 2 && Math.abs(b - c) >= 2){
      return true;
  }else{
      return false;
  }
  
}


17 comments :

  1. public boolean closeFar(int a, int b, int c) {
       return !close(b,c)&(close(a,b)^close(a,c));
    }
    public boolean close(int a, int b) {
       return Math.abs(a-b)<=1;
    }

    ReplyDelete
  2. return Math.abs(a-b)<=1 && Math.abs(a-c)>=2 && Math.abs(b-c)>=2 || Math.abs(a-c)<=1 && Math.abs(a-b)>=2 && Math.abs(b-c)>=2;

    ReplyDelete
  3. public boolean closeFar(int a, int b, int c) {
    int x=0,y=0,z=0;
    x=Math.abs(a-b);
    y=Math.abs(b-c);
    z=Math.abs(c-a);
    if(a==b||b==c||c==a) return true;
    if(x==y||y==z||x==z) return false;
    return true;

    }

    ReplyDelete
  4. public boolean closeFar(int a, int b, int c) {
    int diffba = Math.abs(b-a); // equal to 1 or diff by +2
    int diffca = Math.abs(a-c); //equal to 1 or diff by +2
    int diffbc = Math.abs(b-c); //diff by +2

    if (diffbc>=2) {
    if (diffba == 1 && diffca>=2){return true;}
    else if (diffca == 1 && diffba>=2){return true;}
    else if (a==b){return true;}
    }
    return false;
    }

    ReplyDelete
  5. public boolean closeFar(int a, int b, int c) {
    int diffba = Math.abs(b-a); // equal to 1 or diff by +2
    int diffca = Math.abs(a-c); //equal to 1 or diff by +2
    int diffbc = Math.abs(b-c); //diff by +2

    if (diffbc>=2) {
    return (diffba == 1 && diffca>=2) ||(diffca == 1 && diffba>=2)||(a==b);
    } return false;
    }

    ReplyDelete
  6. public boolean closeFar(int a, int b, int c) {
    int close1 = Math.abs(a - b);
    int close2 = Math.abs(b - c);
    int close3 = Math.abs(a - c);

    if (close1 <= 1 && close2 >= 2 && close3 >= 2) {
    return true;
    } else if (close2 <= 1 && close1 >= 2 && close3 >= 2) {
    return true;
    } else if (close3 <= 1 && close1 >= 2 && close2 >= 2) {
    return true;
    } else {
    return false;
    }
    }

    ReplyDelete
  7. if (close(a,b) && close(a,c) ){
    return false;
    }
    else if (close(a,b) && far(b,c) ){
    return true;
    }
    else if (close(a,c) && far(b,c) ){
    return true;
    }
    return false;

    }

    private boolean close (int a, int b){
    if (Math.abs(a-b)<=1){
    return true;
    }
    return false;
    }

    private boolean far (int a, int b){
    if (Math.abs(a-b)>=2){
    return true;
    }
    return false;
    }

    ReplyDelete
  8. What does the question want us to do? Really confusing

    ReplyDelete
    Replies
    1. dont confuse .. its the easiest. if you want answer ,. i can tell you.

      Delete
  9. public boolean closeFar(int a, int b, int c) {
    int distAB = Math.abs(a - b);
    int distBC = Math.abs(b - c);
    int distAC = Math.abs(a - c);

    if ((distAB <= 1 && distBC <= 1) || (distAB <= 1 && distAC <= 1) || (distBC <= 1 && distAC <= 1)){
    return false;
    }
    return true;
    }

    ReplyDelete
  10. boom, boom, boom, boom, I want you in my room!

    ReplyDelete
  11. public boolean closeFar(int a, int b, int c) {
    return !(Math.abs(a - b) >= 2 != Math.abs(b - c) >= 2 != Math.abs(c - a) >= 2);
    }

    ReplyDelete
  12. def close_far(a, b, c):
    return (abs(b - a) < 2) ^ (abs(c - a) < 2) and abs(b - c) > 1

    ReplyDelete
  13. public boolean closeFar(int a, int b, int c)
    {
    if (Math.abs(b-a) <= 1) return Math.abs(c-b) >= 2 && Math.abs(c-a) >=
    2;
    if (Math.abs(c-a) <= 1) return Math.abs(b-c) >= 2 && Math.abs(b-a) >= 2;
    return false;
    }

    ReplyDelete
  14. public boolean closeFar(int a, int b, int c) {
    return Math.abs(a-b)>2 ||Math.abs(a-c)>2 || Math.abs(b-a) <1 || Math.abs(c-a) >2 || Math.abs(b - c) >2 ? true:false;
    }

    ReplyDelete
  15. public boolean closeFar(int a, int b, int c) {
    int diff1 = Math.abs(a-b);
    int diff2 = Math.abs(a-c);
    int diff3 = Math.abs(b-c);

    while(diff3 > 1){
    return (diff1<=1&&diff2!=1) || (diff1!=1&&diff2<=1);
    }
    return false;


    }

    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