Search

Tuesday 6 June 2017

diff21

Problem: 
Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21.

Test Cases: 
diff21(19) → 2
diff21(10) → 11
diff21(21) → 0


Solution: 

public int diff21(int n) {
  if(n>21)
  {
    return 2*(Math.abs(n-21));
  }
  return Math.abs(n-21);
}
 

(NOTE: Math.abc(int n) returns a non-negative integer value.) 

No comments:

Post a Comment