Check if a given number is sparse or not - GeeksforGeeks
A number is said to be a sparse number if in binary representation of the number no two or more consecutive bits are set. Write a function to check if a given number is Sparse or not.
Read full article from Check if a given number is sparse or not - GeeksforGeeks
A number is said to be a sparse number if in binary representation of the number no two or more consecutive bits are set. Write a function to check if a given number is Sparse or not.
// Return true if n is sparse, else false
bool
checkSparse(
int
n)
{
// n is not sparse if there is set
// in AND of n and n/2
if
(n & (n>>1))
return
false
;
return
true
;
}