Parity of unsigned integer
Basic Accuracy: 67.02% Submissions: 1634 Points: 1

Given an integer N, find it's parity. 
Parity of a number refers to the number of 1 bits it contains. The number has “odd parity”, if it contains odd number of 1-bits and is “even parity” if it contains even number of 1-bits.

Example 1:

N = 13
Output: odd
(13)10 = (1101)2  The binary representation
has three 1-bits. So, it's parity is odd.

Example 2:

N = 9
Output: even
(9)10 = (1001)2  The binary representation
has two 1-bits. So, it's parity is even.


Your Task:
You don't need to read input or print anything. Your task is to complete the function computeParity() which takes an Integer N as input parameter and returns string "odd" or "even".


Expected Time Complexity: O(log(N))
Expected Auxiliary Space: O(1)


1 <= N <= 105

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Parity of unsigned integer

Output Window