Problem overview
The method negative(int n) must return the negative of n. A number that is already negative is returned unchanged. Zero stays zero.
negative(4) gives -4
negative(15) gives -15
negative(-4) gives -4
negative(0) gives 0
So the result is never positive: negative(4) flips the sign, and negative(-4) keeps it. This is different from simply writing -n, which would turn -4 into 4.
Approach: decide, then flip or keep
The task has two cases, and the sign of n tells you which one applies. A conditional statement chooses between them:
- If
n < 0, it is already negative, so return it as it is. - Otherwise
nis zero or positive, and the unary minus-ngives the negative value. For zero,-0is just0.
Zero needs no case of its own. The test n < 0 is false for 0, and -n returns 0, which is what the examples ask for.
Put an input through both paths. For n = 15, the test 15 < 0 is false, so the code takes the else and returns -15. For n = -4, the test -4 < 0 is true, so the code returns -4 and never reaches the else.
Step by step
- Write an
ifthat checksn < 0. A true condition means the input is already negative. - Return
ninside theif. Nothing has to be changed. Returning leaves the method immediately. - Write an
elsethat returns-n. It runs for zero and every positive number. The unary minus negates the value.
The solution in C++
class Solution {
public:
int negative(int n) {
if (n < 0) {
return n;
} else {
return -n;
}
}
};
Both branches return, so the method always produces a value. Compilers can see this and do not warn about reaching the end without a return. Because each branch ends in return, the else is not strictly needed: writing if (n < 0) { return n; } followed by return -n; behaves the same. Keeping the else makes the two cases easy to see.
The same logic fits in one line with the conditional operator: return n < 0 ? n : -n;. Another form is return -abs(n); with <cstdlib>, but the lesson practises if and else.
Complexity
Skipped for such a short lesson: the method does one comparison and at most one negation, so time and space are both constant.
Edge cases and common mistakes
- Zero.
negative(0)returns0, through theelsebranch. Writingn <= 0in the condition would also work, since both branches then return 0. - Returning
-nfor everything. That turns-4into4. The check for negative input is what stops it. - Writing
n = -n;and forgetting to return it. Assigning to the parameter changes only the local copy. The method mustreturnthe value. - Using
=in the condition.if (n = 0)assigns 0 tonand tests the result. Use<,<=or==to compare. - The smallest
int.INT_MINis already negative, so it returns through the first branch. For positive inputs,-nnever overflows, because the positive range is smaller than the negative one by a single value. The one value to avoid is negatingINT_MINitself, which is undefined behaviour in C++, and this method never does.
Testing
negative(4)gives-4.negative(15)gives-15.negative(-4)gives-4.negative(0)gives0.negative(2147483647)gives-2147483647.
