Even or Odd
Description
Given an integer n, return "Even" if n is even and "Odd" if it is odd.
A number is even when it is a multiple of 2: it splits into pairs with nothing left over. Zero and negative numbers count too, so 0 is even and -3 is odd.
Examples
Example 1
Input: n = 4
Output: "Even"
Explanation: 4 is 2 × 2, so it splits into pairs with nothing left over.
Example 2
Input: n = 7
Output: "Odd"
Explanation: 7 is 2 × 3 + 1: one is left over.
Example 3
Input: n = 0
Output: "Even"
Explanation: 0 is 2 × 0, a multiple of 2, so it's even.
Example 4
Input: n = -3
Output: "Odd"
Explanation: -3 is 2 × (-2) + 1, so it's odd; negative numbers are even or odd just like positive ones.
Constraints
- -10^9 ≤ n ≤ 10^9
Editorial
Approach - Check the Remainder
Intuition
Even means n splits into pairs with nothing left over, so the remainder after dividing by 2 says it all: n % 2 is 0 when n is even.
Test for 0, not for 1. In Java, C++ and JavaScript the remainder takes the sign of the number, so -3 % 2 is -1 there, while Python gives 1 (run on our judge). A remainder of 0 means even in every language, negatives included.
Step-by-Step Explanation
Let's trace Example 2, n = 7:
- Take
n = 7: 7 items in a row. - Pair them up two at a time: 3 pairs, 1 with 2, 3 with 4, 5 with 6.
- One item is left over: that's the remainder,
7 % 2 = 1. - The remainder isn't 0, so 7 is odd: return
"Odd".
Pair Them Up — The items paired two at a time; the one without a partner is the remainder.
Algorithm
- Work out
n % 2. - If it is 0, return
"Even"; otherwise return"Odd".
Code
class Solution {
public:
string evenOrOdd(int n) {
return n % 2 == 0 ? "Even" : "Odd";
}
};
class Solution:
def evenOrOdd(self, n: int) -> str:
return "Even" if n % 2 == 0 else "Odd"
class Solution {
public String evenOrOdd(int n) {
return n % 2 == 0 ? "Even" : "Odd";
}
}
var evenOrOdd = function(n) {
return n % 2 === 0 ? "Even" : "Odd";
};
Complexity Analysis
Time Complexity: O(1)
One remainder, however large n is.
Space Complexity: O(1)
Nothing is stored.
Common Mistakes
Each of these looks right on a quick read, and the tests still reject it. The outputs below come from running the code.
Testing for a remainder of 1. n % 2 == 1 ? "Odd" : "Even"
On n = -3 it returns "Even" in Java; the answer is "Odd". In Java, C++ and JavaScript the remainder takes the sign of the number, so a negative odd number leaves -1, not 1, and the test for 1 calls it even. Test for a remainder of 0 instead, which is the same in every language.
Dividing with /. n / 2 * 2 == n
On n = 7 it returns "Even"; the answer is "Odd". In Python, / always gives a float: 7 / 2 is 3.5, and 3.5 * 2 is 7 again, so every number passes the check and comes out even. Halving and doubling only loses the remainder with //, and n % 2 says it directly.
Reading a remainder as even. return "Even" if n % 2 else "Odd"
On n = 4 it returns "Odd"; the answer is "Even". n % 2 is 1 (true) when n is odd and 0 (false) when it's even, so if n % 2 is the test for odd. Swapping the two answers gets every number wrong.
Where This Pattern Is Taught
This problem practises the remainder operator %, and the sign it gives a negative number in each language. It's taught in Python for DSA, Java for DSA, C++ for DSA and JavaScript for DSA.