Java > Logic-1 > twoAsOne (CodingBat Solution)

Problem:

Given three ints, a b c, return true if it is possible to add two of the ints to get the third.

twoAsOne(1, 2, 3) → true
twoAsOne(3, 1, 2) → true
twoAsOne(3, 2, 2) → false


Solution:

public boolean twoAsOne(int a, int b, int c) {
  if ( a + b == c || c + b == a || c + a == b)
    return true;
  else
    return false;
}

4 comments:

  1. public boolean twoAsOne(int a, int b, int c) {
    return (a == b + c) || (b == c + a) || (c == a + b);
    }

    ReplyDelete
  2. return((a+b==c||a+c==b||c+b==a)?true:false);

    ReplyDelete
  3. (int a,int b,int c)
    {
    If(a+b==c)||(b+c==a)||(c+a==b);
    }

    ReplyDelete
  4. public boolean twoAsOne(int a, int b, int c) {
    int maximum = Math.max(Math.max(a,b),c);
    int res = a + b + c;
    int n = res - maximum;

    if(n == maximum){
    return true;
    }
    if(c == -2){
    return true;
    }
    return false;
    }
    // bu ishlavotti ammo kod juda kop

    ReplyDelete