Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

towAsOne challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p113261

I tried my code as below
public boolean twoAsOne(int a, int b, int c) {
  if((a+b)==c)
  {
  return true;
  }
  
   else if((b+c)==a)
  {
  return true;
  }
  
   else if((a+c)==b)
  {
  return true;
  }
  else 
  return false;
}

Open in new window



I am getting below result
Expected      Run            
twoAsOne(1, 2, 3) → true      true      OK         
twoAsOne(3, 1, 2) → true      true      OK         
twoAsOne(3, 2, 2) → false      false      OK         
twoAsOne(2, 3, 1) → true      true      OK         
twoAsOne(5, 3, -2) → true      true      OK         
twoAsOne(5, 3, -3) → false      false      OK         
twoAsOne(2, 5, 3) → true      true      OK         
twoAsOne(9, 5, 5) → false      false      OK         
twoAsOne(9, 4, 5) → true      true      OK         
twoAsOne(5, 4, 9) → true      true      OK         
twoAsOne(3, 3, 0) → true      true      OK         
twoAsOne(3, 3, 2) → false      false      OK         
other tests
OK       

how to  improve my approach and design of this challenge. How do i make a

graphical venn or some other relevant diagram to design it before writing single

line of code to decide best strategy?
 Please advise
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)
// overkill in this case, but
  List<Integer> list = new ArrayList<Integer>();
  Collections.addAll(list, a*2,b*2,c*2);
  return list.contains(a+b+c);