Problem overview
The method AbsDiff receives two integers, a and b, and returns the absolute difference between them: the gap between the two numbers, always expressed as a non-negative value. In other words, it is the absolute value of a - b.
The examples show the pattern. AbsDiff(3, 0) and AbsDiff(0, 3) both give 3, so the order of the arguments doesn't matter. AbsDiff(10, -1) gives 11, AbsDiff(-5, -7) gives 2, and AbsDiff(6, -6) gives 12. The lesson also says not to use the library's absolute-value method, so the work is done with a comparison.
Approach
If you subtract the smaller number from the larger one, the result is never negative. So the task reduces to finding which argument is larger:
- If
ais smaller thanb, the answer isb - a. - Otherwise
ais greater than or equal tob, and the answer isa - b.
When the two numbers are equal, both formulas give 0, so ties need no special case.
Here is a worked case. For a = 10 and b = -1, the test 10 < -1 is false, so the method returns 10 - (-1), which is 11. For a = -5 and b = -7, the test -5 < -7 is false as well, and the result is -5 - (-7), which is 2.
Step by step
- Compare. Write an
ifwith the conditiona < b, and close its braces. The body comes next. - Handle the small-
acase. Inside theif,return b - a;. Subtracting the smaller value from the larger one gives a result that is not negative. - Handle the rest. After the
if,return a - b;. Noelseis needed, because thereturninside theifalready ended the method for the casea < b. This line is only reached whenais greater than or equal tob, and thena - bis not negative.
The solution in C#
class Solution {
public int AbsDiff(int a, int b) {
if (a < b) {
return b - a;
}
return a - b;
}
}
Some details about this code:
- Early return instead of
else. When anifbody always returns, the code after it behaves as the "otherwise" branch. Adding an explicitelsewould give the same result, and both styles are common in C#. - The method name. The method is
AbsDiff, in PascalCase as C# methods usually are. C# is case-sensitive, soabsDiffwould not match. - Integer parameters. The signature takes and returns
int. This method works on whole numbers. To compare decimals, the same logic would usedoubleparameters and adoublereturn type. - Overflow. If the two values are far apart, the true difference may not fit in an
int. For example,AbsDiff(int.MaxValue, int.MinValue)should be 4,294,967,295, which is too large. C# arithmetic is unchecked by default, so the result silently wraps around. For the ordinary numbers in this lesson, this never happens.
Edge cases and common mistakes
- Equal inputs.
AbsDiff(4, 4)falls through toa - band returns0. - Negative inputs.
AbsDiff(-5, -7): herea(-5) is larger thanb(-7), so the method returns-5 - (-7), which is2. Subtracting a negative number adds its size. - Swapped arguments.
AbsDiff(0, 3)takes thea < bbranch and returns3 - 0. A correct solution gives the same answer in both argument orders. - Always returning
a - b. This is negative wheneverais smaller thanb. - Using
>with the wrong subtraction. If you testa > b, the matching result isa - b. Mixing the comparison with the wrong subtraction brings back negative answers.
Testing
AbsDiff(3, 0)gives3.AbsDiff(0, 3)gives3.AbsDiff(10, -1)gives11.AbsDiff(-5, -7)gives2.AbsDiff(6, -6)gives12.