Please enter your email address or userHandle.
Given a number N. The task is to check whether it is sparse or not. A number is said to be a sparse number if no two or more consecutive bits are set in the binary representation.
Input: N = 2
Explanation: Binary Representation of 2 is 10,
which is not having consecutive set bits.
So, it is sparse number.
Input: N = 3
Explanation: Binary Representation of 3 is 11,
which is having consecutive set bits in it.
So, it is not a sparse number.
Your Task: The task is to complete the function checkSparse() that takes n as a parameter and returns 1 if the number is sparse else returns 0.
Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(1).
1 <= N <= 106
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
want to view the editorial?
Login to access your submissions.