Problem overview
The absolute difference of two numbers is the gap between them, written as a non-negative number. It does not matter which number comes first: absDiff(3, 0) and absDiff(0, 3) are both 3. The method absDiff receives two integers, a and b, and returns that gap without using the library's abs function.
More examples: absDiff(10, -1) is 11, absDiff(-5, -7) is 2, and absDiff(6, -6) is 12. The statement also lists absDiff(1.5, 1.5), but the method in the editor takes Int values, so the code works on whole numbers.
Approach: subtract the smaller from the larger
Computing a - b is negative whenever a is smaller than b. The fix is to subtract the smaller number from the larger one, so the result is never negative:
- if
a < b, the answer isb - a; - otherwise (
ais greater than or equal tob), the answer isa - b.
When a equals b, either subtraction gives 0, so that case needs no special treatment.
Two examples traced by hand. For absDiff(0, 3), the test 0 < 3 is true, so the method returns 3 - 0, which is 3. For absDiff(-5, -7), the test -5 < -7 is false, because -5 sits to the right of -7 on the number line. The method skips the if and returns -5 - (-7), which is 2.
Another way to see the same thing is the definition itself: the absolute difference is the absolute value of a - b. Negating a negative value fixes its sign, as when recreating abs. Here the comparison tells you in advance which order of subtraction gives a non-negative result, so you never need to negate.
Step by step
- Compare the numbers. Write an
ifstatement with its closing brace whose condition isa < b. - Return
b - ainside it. Withathe smaller number, subtracting it frombgives a result that is not negative. - Return
a - bbelow theif. Noelseis needed. Whena < bis true, the method has already returned. So the last line only runs whenais greater than or equal tob, and thena - bis not negative.
The solution in Scala
class Solution {
def absDiff(a: Int, b: Int): Int = {
if (a < b) {
return b - a
}
return a - b
}
}
The Scala details worth knowing:
- Early return.
returnleaves the method immediately, so code after theifonly runs when the condition was false. This replaces theelseand keeps the code flat. - Result type. A method that uses
returnneeds its result type written out, here: Int. - The expression style. Because
if/elseis an expression and the last expression is the result, the same method is often written asif (a < b) b - a else a - bwith noreturnat all.
Edge cases and common mistakes
- Equal numbers.
absDiff(4, 4)skips theifand returns4 - 4, which is0. - Negative inputs. Subtracting a negative number adds:
absDiff(10, -1)computes10 - (-1), which is11.absDiff(-5, -7)computes-5 - (-7), which is2. - Subtracting in the wrong order. Returning
a - bfor every input gives-3forabsDiff(0, 3). - Overflow.
Intwraps silently at 32 bits. Fora = Int.MaxValueandb = -1, the true gap is 2147483648, which does not fit, and the method returns -2147483648. UseLongif inputs can be that far apart.
Testing
absDiff(3, 0)gives3.absDiff(0, 3)gives3.absDiff(10, -1)gives11.absDiff(-5, -7)gives2.absDiff(6, -6)gives12.
