Problem overview
You write a method Negative that takes an integer n and returns its negative version. A positive number becomes negative, a number that is already negative stays as it is, and zero stays zero.
| n | Returned |
|---|---|
| 4 | -4 |
| 15 | -15 |
| -4 | -4 |
| 0 | 0 |
The point is that the result is never positive. A common first guess is to always flip the sign, but that would turn -4 into 4, which breaks the rule that negatives come back unchanged.
Approach
Split the input into two groups. If n is already below zero, return it as it is. Otherwise, n is zero or positive, and the unary minus -n gives the value you want. For zero, -0 is just 0, so zero needs no special case.
Unlike a method that prints, this one hands a value back to the caller, so each branch uses return.
A quick way to check the idea is to trace three inputs. For -4 the test passes and the method returns -4. For 4 the test fails, the else branch runs, and the method returns -4. For 0 the test fails too, and -0 comes out as 0. All three match the table.
Step by step
- Test for negative input. Write
if (n < 0)with an empty block first. That condition means the input is already in its final form. - Return it unchanged. Inside the block,
return n;. The method ends right there. - Return the flipped value otherwise. The
elseblock returns-n. The minus sign in front of a variable is the unary minus operator: it negates the value. For 4 it gives -4, and for 0 it gives 0.
The solution in C#
class Solution {
public int Negative(int n) {
if (n < 0) {
return n;
} else {
return -n;
}
}
}
C# details
- The return type is
int, so every path through the method has to return anint. C# checks this at compile time. Here both branches return, so the compiler knows no path is left without a value. If you removed theelseblock and itsreturn, the method would fail to compile with an error saying that not all code paths return a value. - The method is named
Negative, with a capital letter, and it is an instance method ofSolution. The examples in the problem write it in lowercase, but the code you write has to use the name from the editor. - Zero is handled by the
elsebranch.-0on an integer is plain0, because integers have no separate negative zero.
The next lesson shows that the else here can be removed without changing the result. Keep it for now, because it makes the two cases explicit.
Edge cases and common mistakes
- Negating negatives. Writing
return -n;unconditionally turns-4into4. Theifexists to prevent that. - Zero. Both
n < 0andn <= 0give the right answer for zero here, since-0and0are the same integer. The lesson usesn < 0. - Extreme values. The smallest
int,-2147483648, is negative, so it is returned unchanged. The risky direction would be negating it, which cannot be represented as anint. C# wraps silently in that case rather than throwing, but this solution never negates it. - Using
Math.Abswithout thinking.-Math.Abs(n)also solves the problem in one line, but the lesson is practising theif-elseform. Note thatMath.Abs(int.MinValue)throws anOverflowException, so the explicit branches are safer.
Testing
Call the method with these values:
Negative(4)returns-4.Negative(15)returns-15.Negative(-4)returns-4.Negative(0)returns0.