Check if a given number is sparse or not
Last Updated :
10 Nov, 2022
Write a function to check if a given number is Sparse or not.
A number is said to be a sparse number if in the binary representation of the number no two or more consecutive bits are set.
Example:
Input: x = 72
Output: true
Explanation: Binary representation of 72 is 01001000.
There are no two consecutive 1’s in binary representation
Input: x = 12
Output: false
Explanation: Binary representation of 12 is 1100.
Third and fourth bits (from end) are set.
Naive Approach: The idea is to check the consecutive bits of the number until the number becomes 0.
C++
#include <iostream>
using namespace std;
bool isSparse( int n)
{
int prev;
if (n == 1)
return true ;
while (n > 0) {
prev = n & 1;
n = n >> 1;
int curr = n & 1;
if (prev == curr && prev == 1)
return false ;
prev = curr;
}
return true ;
}
int main()
{
int n = 100;
if (isSparse(n)) {
cout << "Sparse" ;
}
else {
cout << "Not Sparse" ;
}
return 0;
}
|
Java
public class GFG {
static boolean isSparse( int n)
{
int prev;
if (n == 1 )
return true ;
while (n > 0 ) {
prev = n & 1 ;
n = n >> 1 ;
int curr = n & 1 ;
if (prev == curr && prev == 1 )
return false ;
prev = curr;
}
return true ;
}
public static void main(String[] args)
{
int n = 100 ;
if (isSparse(n)) {
System.out.println( "Sparse" );
}
else {
System.out.println( "Not Sparse" );
}
}
}
|
Python3
def isSparse(n):
if (n = = 1 ):
return true
global prev
while (n > 0 ):
prev = n & 1
n = n >> 1
curr = n & 1
if (prev = = curr and prev = = 1 ):
return False
prev = curr
return True
n = 100
if (isSparse(n)):
print ( "Sparse" )
else :
print ( "Not Sparse" )
|
C#
using System;
public class GFG {
static bool isSparse( int n)
{
int prev;
if (n == 1)
return true ;
while (n > 0) {
prev = n & 1;
n = n >> 1;
int curr = n & 1;
if (prev == curr && prev == 1)
return false ;
prev = curr;
}
return true ;
}
public static void Main( string [] args)
{
int n = 100;
if (isSparse(n)) {
Console.WriteLine( "Sparse" );
}
else {
Console.WriteLine( "Not Sparse" );
}
}
}
|
Javascript
function isSparse(n)
{
let prev;
if (n == 1)
return true ;
while (n > 0) {
prev = n & 1;
n = n >> 1;
let curr = n & 1;
if (prev == curr && prev == 1)
return false ;
prev = curr;
}
return true ;
}
let n = 100;
if (isSparse(n)) {
console.log( "Sparse" );
}
else {
console.log( "Not Sparse" );
}
|
Time Complexity: O(Log2n)
Auxiliary Space: O(1)
Efficient Approach: To solve the problem follow the below idea:
If we observe carefully, then we can notice that if we can use bitwise AND of the binary representation of the “given number, then it’s “right-shifted number”(i.e., half the given number) to figure out whether the number is sparse or not. The result of AND operator would be 0 if the number is sparse and non-zero if not sparse.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkSparse( int n)
{
if (n & (n >> 1))
return false ;
return true ;
}
int main()
{
cout << checkSparse(72) << endl;
cout << checkSparse(12) << endl;
cout << checkSparse(2) << endl;
cout << checkSparse(3) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int checkSparse( int n)
{
if ((n & (n >> 1 )) >= 1 )
return 0 ;
return 1 ;
}
public static void main(String[] args)
{
System.out.println(checkSparse( 72 ));
System.out.println(checkSparse( 12 ));
System.out.println(checkSparse( 2 ));
System.out.println(checkSparse( 3 ));
}
}
|
Python3
def checkSparse(n):
if (n & (n >> 1 )):
return 0
return 1
if __name__ = = "__main__" :
print (checkSparse( 72 ))
print (checkSparse( 12 ))
print (checkSparse( 2 ))
print (checkSparse( 30 ))
|
C#
using System;
class GFG {
static int checkSparse( int n)
{
if ((n & (n >> 1)) >= 1)
return 0;
return 1;
}
public static void Main()
{
Console.WriteLine(checkSparse(72));
Console.WriteLine(checkSparse(12));
Console.WriteLine(checkSparse(2));
Console.WriteLine(checkSparse(3));
}
}
|
PHP
<?php
function checkSparse( $n )
{
if ( $n & ( $n >> 1))
return 0;
return 1;
}
echo checkSparse(72), "\n" ;
echo checkSparse(12), "\n" ;
echo checkSparse(2), "\n" ;
echo checkSparse(3), "\n" ;
?>
|
Javascript
<script>
function checkSparse(n)
{
if ((n & (n>>1)) > 0)
return 0;
return 1;
}
document.write(checkSparse(72) + "</br>" );
document.write(checkSparse(12) + "</br>" );
document.write(checkSparse(2) + "</br>" );
document.write(checkSparse(3) + "</br>" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Note: Instead of the right shift, we could have used the left shift also, but the left shift might lead to an overflow in some cases.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...