Problem overview
The absolute difference of two numbers is how far apart they are, written as a positive number. You get it by subtracting one number from the other and then dropping any minus sign. The task is a function absDiff(a: Int, b: Int): Int inside class Solution that returns it. Kotlin's own abs is off limits, so the sign has to be handled with a comparison.
Examples with whole numbers:
absDiff(3, 0)returns3.absDiff(0, 3)also returns3: the order of the arguments doesn't matter.absDiff(10, -1)returns11.absDiff(-5, -7)returns2.absDiff(6, -6)returns12.
Approach
The result of a - b is negative when a is smaller than b, and that is the only case where the sign is wrong. In that case, b - a gives the same distance with a positive sign. So the rule is: subtract the smaller number from the larger one.
That means one comparison, a < b, and two possible subtractions. The result is never negative, which is what "absolute" requires.
Step by step
- Compare the inputs. Write
if (a < b) { ... }. The body comes next. - Handle the smaller
a. Inside theif,return b - a. Subtracting the smaller number from the larger one cannot go negative. - Handle everything else. After the
if,return a - b. This line only runs whena < bwasfalse, which meansais greater than or equal tob, soa - bis not negative. When they are equal it is0, which is the right answer.
The solution in Kotlin
class Solution {
fun absDiff(a: Int, b: Int): Int {
if (a < b) {
return b - a
}
return a - b
}
}
Kotlin details worth knowing:
- No
elseneeded. Areturnends the function immediately. When the condition istrue, the function has already returned before the last line, so that line is reached only for the other case. Anelsearound the final return would work too but add nothing. - Parameters are read-only. The function computes new values and does not change
aorb. - The same logic as one expression.
fun absDiff(a: Int, b: Int): Int = if (a < b) b - a else a - bdoes the same thing, becauseifin Kotlin can produce a value.
Tracing two calls
For absDiff(0, 3), the condition 0 < 3 is true, so the function returns 3 - 0, which is 3. For absDiff(3, 0), the condition 3 < 0 is false, so the if body is skipped and the function returns 3 - 0 as well. Both orders of the arguments end in the same subtraction, with the larger number first.
Edge cases and common mistakes
- Equal numbers.
absDiff(4, 4)skips theifand returns4 - 4, which is0. - Negative inputs.
absDiff(-5, -7)hasa = -5, which is greater thanb = -7. The function returns-5 - (-7), and subtracting a negative adds, so the result is2. Mixed signs work the same way:absDiff(10, -1)is10 - (-1), which is11. - Returning
a - balone. It is right only whenais at leastb. ForabsDiff(0, 3)it gives-3. - Swapping the branches. If the first branch returned
a - bwhena < b, every answer would come out negative or zero. - Overflow. An
Intholds values up to 2147483647, and arithmetic that goes past it wraps silently.absDiff(2147483647, -1)returns-2147483648instead of 2147483648, because the true difference doesn't fit in 32 bits. The lesson's tests use small numbers, so this does not matter here. To cover it, useLongfor the type of the result. - Decimals. The parameters are
Int, so fractional inputs such as1.5are not accepted. The same logic works unchanged withDoubleparameters and aDoubleresult.
Testing
absDiff(3, 0)gives3.absDiff(0, 3)gives3.absDiff(10, -1)gives11.absDiff(-5, -7)gives2.absDiff(6, -6)gives12.absDiff(7, 7)gives0.