Odd or Even
Odd or Even is a easy bit manipulation problem solved with the basics pattern.
The best approach, lowest bit, runs in O(1) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from modulo up.
Problem
Decide whether n is odd or even using a bit operation.
Examples
Example 1
- Input
n = 15- Output
odd
Example 2
- Input
n = 44- Output
even
Constraints
0 <= n <= 10^4(the bit trick also works for negative numbers in two's complement).
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 8 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Modulo | O(1) | O(1) |
| Lowest bit | O(1) | O(1) |
1Modulo
O(1)O(1)Check the remainder when dividing by 2. Compare with 0 rather than 1 so negative numbers work.
- return n % 2 == 0 ? even : odd.
class Solution {
static String oddEven(int n) {
return n % 2 == 0 ? "even" : "odd";
}
}class Solution:
def oddEven(self, n: int) -> str:
return "even" if n % 2 == 0 else "odd"class Solution {
public:
string oddEven(int n) {
return n % 2 == 0 ? "even" : "odd";
}
};function oddEven(n) {
return n % 2 === 0 ? "even" : "odd";
}2Lowest bit
O(1)O(1)n & 1 isolates the lowest bit: 1 means odd, 0 means even. It works for negative numbers too.
- return (n & 1) == 0 ? even : odd.
class Solution {
static String oddEven(int n) {
return (n & 1) == 0 ? "even" : "odd";
}
}class Solution:
def oddEven(self, n: int) -> str:
return "even" if (n & 1) == 0 else "odd"class Solution {
public:
string oddEven(int n) {
return (n & 1) == 0 ? "even" : "odd";
}
};function oddEven(n) {
return (n & 1) === 0 ? "even" : "odd";
}Edge cases to test
- n = 0 (even)
- Negative n: n % 2 gives -1 in Java, so n % 2 == 1 is wrong for negatives
Hints
Hint 1
The lowest bit of a number is 1 exactly when it is odd.
FAQ
What is the best time complexity for Odd or Even?
Lowest bit runs in O(1) time and O(1) extra space.
Which pattern does Odd or Even use?
It is a bit manipulation problem that uses the basics pattern. Other problems with the same pattern: Decimal to binary, Convert Binary Number in a Linked List to Integer, K-th Bit is Set or Not.
Is there a brute force solution for Odd or Even?
Yes. Modulo takes O(1) time and O(1) space. Check the remainder when dividing by 2.
Which edge cases should I test for Odd or Even?
n = 0 (even); Negative n: n % 2 gives -1 in Java, so n % 2 == 1 is wrong for negatives.