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.
Example 1:
Input: N = 2
Output: 1
Explanation: Binary Representation of 2 is 10, 
which is not having consecutive set bits. 
So, it is sparse number.
Example 2:
Input: N = 3
Output: 0
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(1).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:

    

    #Function to check if the number is sparse or not.

    def isSparse(self,n):

        #Your code here 



Solutions:
class Solution:

	def isSparse(self, n):
		k = bin(n)[2:]
		a = k.split('0')
		for i in a:
			if len(i) > 1:
				return 0
		return 1


class Solution:

	def isSparse(self, n):
		binary = []
		while n > 0:
			x = n % 2
			binary.append(x)
			n = n // 2
		for i in range(0, len(binary) - 1):
			if binary[i] == binary[i + 1] and binary[i] == 1:
				return 0
		return 1


class Solution:

	def isSparse(self, n):
		while n > 0:
			x = n
			n = n >> 1
			if x & 1 == 1 and n & 1 == 1:
				return 0
		return 1


class Solution:

	def bina(n):
		b = ''
		while n != 1:
			b = str(n % 2) + b
			n = n // 2
		return '1' + b

	def isSparse(self, n):
		n = Solution.bina(n)
		n = str(n)
		if '11' in n:
			return 0
		return 1


class Solution:

	def isSparse(self, n):
		numero = bin(n)[2:]
		if len(numero) == 1:
			return True
		ultimo = numero[0]
		for l in numero[1:]:
			if l == '1' and ultimo == '1':
				return False
			else:
				ultimo = l
		return True


class Solution:

	def isSparse(self, n):
		count = 0
		while n != 0:
			if n & 1 == 1:
				count += 1
				if count == 2:
					return 0
			else:
				count = 0
			n = n // 2
		return 1


class Solution:

	def isSparse(self, n):
		a = bin(n)[2:]
		b = a.split('0')
		for i in b:
			if len(i) >= 2:
				return False
		return True


class Solution:

	def isSparse(self, n):
		if n >> 1 & n == 0:
			return True
		return False


class Solution:

	def isSparse(self, n):
		binn = bin(n)[2:]
		if '11' in binn:
			return 0
		return 1


class Solution:

	def isSparse(self, n: int) -> bool:
		binary_str = bin(n)[2:]
		for i in range(len(binary_str) - 1):
			if binary_str[i] == '1' and binary_str[i + 1] == '1':
				return False
		return True


class Solution:

	def isSparse(self, n):
		c = 0
		flag = -1
		while n != 0:
			if n & 1 == 1:
				if flag == 1:
					return 0
				flag = 1
			else:
				flag = 0
			n = n >> 1
		return 1


class Solution:

	def isSparse(self, n):
		while n:
			if n & 3 == 3:
				return False
			n >>= 1
		return True


class Solution:

	def isSparse(self, n):
		binary = bin(n)[2:]
		for i in range(len(binary) - 1):
			if binary[i] == '1' and binary[i + 1] == '1':
				return 0
		return 1


class Solution:

	def isSparse(self, n):
		binary = bin(n)[2:]
		if '11' in binary:
			return False
		else:
			return True


class Solution:

	def isSparse(self, n):
		binary = format(n, 'b')
		for i in range(0, len(binary) - 1):
			if binary[i] == binary[i + 1] and binary[i] == '1':
				return 0
		return 1


class Solution:

	def isSparse(self, n):
		elem = str(bin(n))[2:]
		for i in range(len(elem) - 1):
			if elem[i] == '1':
				if elem[i] == elem[i + 1]:
					return False
		return True


class Solution:

	def isSparse(self, n):
		last = False
		for (i, ch) in enumerate(bin(n)):
			if i > 0 and last and (ch == '1'):
				return 0
			last = True if ch == '1' else False
		return 1


class Solution:

	def isSparse(self, n):
		prev = 0
		while n > 0:
			if n % 2 and prev:
				return 0
			prev = n % 2
			n = n // 2
		return 1


class Solution:

	def isSparse(self, n):
		a = bin(n)[2:]
		string = str(a)
		for i in range(0, len(string) - 1):
			if string[i] == '1' and string[i + 1] == '1':
				return 0
		return 1


class Solution:

	def isSparse(self, n):
		prev_bit = '0'
		bits = bin(n)[2:]
		for bit in bits:
			if bit == '1' and prev_bit == '1':
				return 0
			prev_bit = bit
		return 1


class Solution:

	def isSparse(self, n):
		return n & n >> 1 == 0


class Solution:

	def isSparse(self, num):
		n = bin(num)
		n = list(n)
		n = n[2:]
		for i in range(len(n) - 1):
			if n[i] == n[i + 1] == '1':
				return 0
		return 1


class Solution:

	def isSparse(self, n):
		len = 0
		if n == 0:
			return True
		while n > 0:
			if n & 1:
				len += 1
				if len > 1:
					return False
			else:
				len = 0
			n >>= 1
		return True


class Solution:

	def isSparse(self, n):
		b = bin(n)
		count0 = 0
		count1 = 1
		for i in range(len(b) - 1):
			if b[i] == '1':
				if b[i + 1] == '1':
					return False
		return True


class Solution:

	def isSparse(self, 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


class Solution:

	def isSparse(self, n):
		t = bin(n)[2:]
		k = t.count('11')
		if k == 0:
			return 1
		else:
			return 0


class Solution:

	def isSparse(self, n):
		p = 0
		while n > 0:
			if n & 1 == 1 and p == 1:
				return 0
			else:
				p = n & 1
			n = n >> 1
		return 1


class Solution:

	def isSparse(self, n):
		flag = False
		while n:
			if n & 1:
				if flag:
					return False
				flag = True
			else:
				flag = False
			n >>= 1
		return True


class Solution:

	def isSparse(self, n):
		if n & n >> 1 != 0:
			return 0
		else:
			return 1


class Solution:

	def isSparse(self, n):
		count = 0
		while n > 0 and count < 2:
			if n & 1 == 1:
				count = count + 1
			else:
				count = 0
			n = n >> 1
		if count >= 2:
			return False
		return True


class Solution:

	def isSparse(self, n):
		r1 = n % 2
		n = n // 2
		while n > 0:
			r2 = n % 2
			if r1 and r2:
				return 0
			r1 = r2
			n = n // 2
		return 1


class Solution:

	def isSparse(self, n):
		bi = bin(n)[2:]
		p = bi.split('0')
		for i in p:
			if len(i) >= 2:
				return False
		return True


class Solution:

	def isSparse(self, n):
		b = str(bin(n))[2:]
		for i in range(len(b)):
			if i == len(b) - 1:
				return 1
			if b[i] == '1':
				if b[i + 1] == '1':
					return 0
		return 1


class Solution:

	def isSparse(self, N):
		Temp = bin(N)[2:]
		ctr = 0
		Ans = 0
		for i in Temp:
			if i == '1':
				ctr += 1
			else:
				Ans = max(Ans, ctr)
				ctr = 0
		Ans = max(ctr, Ans)
		if Ans <= 1:
			return 1
		else:
			return 0


class Solution:

	def isSparse(self, n):
		s = str(bin(n))
		if '11' in s:
			return 0
		else:
			return 1


class Solution:

	def isSparse(self, n):
		x = bin(n)
		if '11' in x:
			return 0
		else:
			return 1


class Solution:

	def isSparse(self, n):
		bits = bin(n)
		if n & n << 1:
			return 0
		else:
			return 1


class Solution:

	def isSparse(self, n):
		binary = bin(n)[2:]
		for i in range(len(binary) - 1):
			if binary[i:i + 2] == '11':
				return False
		return True


class Solution:

	def isSparse(self, n):
		c = str(bin(n)[2:])
		if '11' in c:
			return 0
		return 1


class Solution:

	def isSparse(self, n):
		n = bin(n)[2:]
		arr = list(n)
		if len(arr) < 2:
			return 1
		elif len(arr) == 2:
			if arr[0] != arr[1]:
				return 1
			else:
				return 0
		for i in range(1, len(arr)):
			if arr[i] == '1' and arr[i - 1] == '1':
				return 0
		return 1


class Solution:

	def isSparse(self, n):
		p = bin(n).replace('0b', '')
		for i in range(len(p) - 1):
			if p[i] == p[i + 1] and p[i] == '1':
				return 0
		return 1