Problem overview
The method Abs takes an integer n and returns its absolute value: how far the number is from zero, always as a non-negative number. The absolute value of -5 is 5, of 250 is 250, and of 0 is 0. The point of the exercise is to build this yourself, so the library method Math.Abs is off limits.
Think of the number line. The absolute value is the distance from zero, so -3 and 3 are both three steps away and share the answer 3. A distance can't be negative, which is why the result is never below zero.
Approach
Only negative numbers need to change. A positive number, or zero, is already its own absolute value. So the method has two cases:
- If
nis less than0, return the number with its sign flipped. - Otherwise, return
nas it is.
Flipping the sign is just the unary minus: -n. Negating a negative number gives a positive one, because -(-5) is 5.
The same logic can be written on one line with the conditional operator, return n < 0 ? -n : n;. The lesson uses the longer if/else form, which makes each case visible. The two behave identically.
Take n = -5. The test -5 < 0 is true, so the method returns -(-5), which is 5. Take n = 250. The test is false, so the method returns 250 unchanged.
Step by step
- Test for negative. Write an
ifwith the conditionn < 0. Its body is empty for the moment. - Return the negation. Inside the
if,return -n;turns a negative input into its positive counterpart. - Handle everything else. Add
} else {on one line, and in that branchreturn n;. This branch also catches0, which is correct, since the absolute value of zero is zero.
The solution in C#
class Solution {
public int Abs(int n) {
if (n < 0) {
return -n;
} else {
return n;
}
}
}
A few C# details are worth knowing:
- Every path returns. The method promises an
int, so the compiler requires every route through it to return one. Because both theifand theelsereturn, it is satisfied. - PascalCase names. C# methods conventionally start with a capital letter, so the method is
Abs. C# is case-sensitive, andabswould be a different name. - The library method.
Math.Absdoes this job in practice, and the lesson avoids it so you can practise conditions. - An
inthas a lopsided range. It holds -2,147,483,648 up to 2,147,483,647, so the most negative value has no positive counterpart. In the default unchecked arithmetic,-int.MinValuewraps around and givesint.MinValueagain, soAbs(int.MinValue)returns -2147483648 here. The library'sMath.Absthrows anOverflowExceptionfor that input instead.
Edge cases and common mistakes
- Zero.
Abs(0)returns0through theelsebranch. Usingn <= 0in the condition would also work, because-0is0, but it does extra work for nothing. - Returning
nin the first branch. Writingreturn n;wherereturn -n;belongs leaves negatives negative. - Writing
n = -n;without a return. Assigning changes only the local parameter. The method still has to return the result. - Negating every input. Returning
-nunconditionally flips positive numbers into negative ones. - Checking only positives. The method must handle both signs, so test with a negative, a positive and zero.
Testing
Abs(-5)gives5.Abs(7)gives7.Abs(0)gives0.