Solution Review: Check If a Given Number is Even/Odd
This is a solution to another Bitwise question. Think of the rightmost bit of a number and think of some logic that could solve this. We solve using AND operator with O(1) time Complexity.
Table of Contents
Here, we see the solution for checking if a number is even/odd.
Solution review
We must review all the elements to check if any are even/Odd.
It would be best to see the pattern of bits present in odd numbers. If you take a closer look at each of them, you can see the right-most significant bit is set to 1 (for 20 place).
So, we do an AND bitwise operation with 1
decimal number to see if the resultant output is 1
. If not, it is an even number else odd.
Algorithm
- Iterate over all the elements to check
- if
(element & 1) == 1
- if
true
, print "Odd" - else, print "Even"
- if
Explanation
Table illustrating values at various levels of each operation.
n | n & 1 | ((n & 1) == 1) | Output |
---|---|---|---|
1 | 1 | True | Odd |
2 | 0 | False | Even |
3 | 1 | True | Odd |
4 | 0 | False | Even |
5 | 1 | True | Odd |
6 | 0 | False | Even |
7 | 1 | True | Odd |
8 | 0 | False | Even |
9 | 1 | True | Odd |
Solution
Here is the logic behind this problem and its solution.
Java
class IsEven {
private static String helper(int n) {
return (n & 1) == 0 ? "Even" : "Odd";
}
public static void main(String[] args) {
System.out.println("Number '" + 1 + "' is : " + helper(1));
System.out.println("Number '" + 2 + "' is : " + helper(2));
System.out.println("Number '" + 3 + "' is : " + helper(3));
System.out.println("Number '" + 4 + "' is : " + helper(4));
System.out.println("Number '" + 5 + "' is : " + helper(5));
}
}
Python
def CheckEvenOdd(n):
return "even" if (n & 1)==0 else "odd"
print("Number ",1," is : " ,CheckEvenOdd(1))
print("Number ",2," is : " ,CheckEvenOdd(2))
print("Number ",3," is : " ,CheckEvenOdd(3))
print("Number ",4," is : " ,CheckEvenOdd(4))
print("Number ",5," is : " ,CheckEvenOdd(5))
JavaScript
const IsEven = n => {
return (n & 1) === 0 ? 'Even' : 'Odd';
}
console.log (`Number '1' is : ${IsEven (1)}`);
console.log (`Number '2' is : ${IsEven (2)}`);
console.log (`Number '3' is : ${IsEven (3)}`);
console.log (`Number '4' is : ${IsEven (4)}`);
console.log (`Number '5' is : ${IsEven (5)}`);
C++
#include <iostream>
#include <string>
using namespace std;
string helper(int n) {
return (n & 1) == 0 ? "even" : "odd";
}
int main() {
int firstNumber = 125;
int secondNumber = 8;
cout << "Number '" << 1 << "' is : " << helper(1) << endl;
cout << "Number '" << 2 << "' is : " << helper(2) << endl;
cout << "Number '" << 3 << "' is : " << helper(3) << endl;
cout << "Number '" << 4 << "' is : " << helper(4) << endl;
cout << "Number '" << 5 << "' is : " << helper(5) << endl;
return 0;
}
TypeScript
export const IsEven = (n: number): string => {
return (n & 1) === 0 ? 'Even' : 'Odd';
}
console.log(`Number '1' is : ${IsEven(1)}`);
console.log(`Number '2' is : ${IsEven(2)}`);
console.log(`Number '3' is : ${IsEven(3)}`);
console.log(`Number '4' is : ${IsEven(4)}`);
console.log(`Number '5' is : ${IsEven(5)}`);
Array of outputs
Let’s return an array of string outputs based on the input values. A code snippet of each language follows.
Java
import java.util.Arrays;
class CheckEvenOdd {
private static String[] checkEvenOdd(int[] nums) {
String[] ans = new String[nums.length];
int k = 0;
for (int n : nums) {
ans[k++] = ((n & 1) == 1) ? "Odd" : "Even";
}
return ans;
}
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println(Arrays.toString(checkEvenOdd(nums)));
}
}
Python
def IsEven(array):
result = []
def helper(array):
k = 0
for n in array:
result.append("Odd" if (n & 1) == 1 else "Even")
k += 1
return result
return helper(array)
print(IsEven([1, 2, 3, 4, 5, 6, 7, 8, 9]))
JavaScript
const IsEven = array => {
const result = [];
function helper (array) {
let k = 0;
for(let n of array) {
result[k++] = ((n & 1) === 1) ? "Odd" : "Even";
}
return result;
}
return helper (array);
}
console.log (IsEven ([1, 2, 3, 4, 5, 6, 7, 8, 9]));
TypeScript
export const IsEven = (array: number[]): string[] => {
const result: string[] = [];
function helper(array: number[]): string[] {
let k: number = 0;
for (let n of array) {
result[k++] = ((n & 1) === 1) ? "Odd" : "Even";
}
return result;
}
return helper(array);
}
console.log(IsEven([1, 2, 3, 4, 5, 6, 7, 8, 9]));
Complexity analysis
Time complexity: O(n)
We need to review the entire array to check each and see if they are even/odd. So, the time complexity is directly proportional to the number of elements present in the nums
array.
Space complexity: O(n)
We do not need to create an array here. We can print the Odd
and Even
directly on the console.
To make things easier to represent, we used an extra space array of Strings
to store the output.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.