Given an non-negative integer n. You are only allowed to make set bit unset. You have to find the maximum possible value of query so that after performing the given operations, no three consecutive bits of the integer query are set-bits. 
Example 1:
Input:
n = 2
Output: 
2
Explanation: 
2's binary form is 10, no 3 consecutive set bits are here. 
So, 2 itself would be answer.
Example 2:
Input:
n = 7
Output: 
6
Explanation: 
7's binary form is .....00111.We can observe that 3
consecutive bits are set bits. This is not allowed.
So, we can perfrom the operation of changing set 
bit to unset bit. Now, the number 
becomes 6 that is .....00110. It satifies the 
given condition. Hence, the maximum possible 
value is 6.
Your Task:   You don't need to read input or print anything. Your task is to complete the function noConseBits(), which takes integer n as input parameter and returns the maximum value possible so that it satifies the given condition.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ n ≤ 10^{9}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Bit manipulation

Data structures

Starter code: class Solution:

    def noConseBits(self, n : int) -> int:

        # code here

        



Solutions:
class Solution:

	def noConseBits(self, n: int) -> int:
		n = bin(n)[2:]
		i = 0
		b = n
		while i <= len(n):
			if n[i:i + 3] == '111':
				b = n[:i + 2] + '0' + n[i + 3:]
				n = b
			i += 1
		return int(n, 2)


class Solution:

	def noConseBits(self, n):
		ans = 0
		i = 31
		count = 0
		while i >= 0:
			if count <= 1 and n & 1 << i:
				ans |= 1 << i
				count += 1
			else:
				count = 0
			i -= 1
		return ans


class Solution:

	def noConseBits(self, n: int) -> int:
		i = 31
		c = 0
		while i >= 0:
			if n & 1 << i:
				c = c + 1
			else:
				c = 0
			if c == 3:
				n = n & ~(1 << i)
				c = 0
			i = i - 1
		return n


class Solution:

	def noConseBits(self, n: int) -> int:
		t = bin(n)[2:]
		if '111' not in t:
			return n
		x = ''
		x += t[0]
		x += t[1]
		for i in t[2:]:
			if i == '1' and x[-1] == '1' and (x[-2] == '1'):
				x += '0'
			else:
				x += i
		return int(x, 2)


import math

class Solution:

	def noConseBits(self, n: int) -> int:
		if n == 0:
			return 0
		nobits = int(math.log(n, 2))
		for i in range(nobits, 1, -1):
			if n & 1 << i and n & 1 << i - 1 and n & 1 << i - 2:
				n = n & ~(1 << i - 2)
		return n


class Solution:

	def noConseBits(self, n: int) -> int:
		numB = bin(n)[2:]
		count = 0
		for i in range(len(numB)):
			if int(numB[i]):
				count += 1
				if count == 3:
					numB = numB[:i] + '0' + numB[i + 1:]
					count = 0
			else:
				count = 0
		return int(numB, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		(bit_count, ans) = (0, 0)
		i = 31
		while i >= 0:
			if bit_count <= 1 and n & 1 << i:
				ans = ans | 1 << i
				bit_count += 1
			else:
				bit_count = 0
			i -= 1
		return ans


class Solution:

	def noConseBits(self, n: int) -> int:
		set = [0] * 35
		for j in range(30, -1, -1):
			if n & 1 << j:
				set[j] = 1
		fin_ans = 0
		for j in range(30, 1, -1):
			if set[j] == 1:
				fin_ans |= 1 << j
				if set[j - 1] == 1:
					set[j - 2] = 0
		if set[1] == 1:
			fin_ans |= 2
		if set[0] == 1:
			fin_ans |= 1
		return fin_ans


import math

class Solution:

	def noConseBits(self, n: int) -> int:
		if n < 7:
			return n
		count = 0
		for i in range(math.ceil(math.log2(n)), -1, -1):
			if n & 1 << i:
				count += 1
			else:
				count = 0
			if count == 3:
				n &= ~(1 << i)
				count = 0
		return n


class Solution:

	def noConseBits(self, n: int) -> int:
		if n < 7:
			return n
		binary = bin(n)[2:]
		i = 2
		while i < len(binary):
			if binary[i - 2:i + 1] == '111':
				n -= 2 ** (len(binary) - i - 1)
				i += 2
			i += 1
		return n


class Solution:

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


class Solution:

	def noConseBits(self, n: int) -> int:
		binary = bin(n).replace('0b', '').replace('111', '110')
		return int(binary, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		s = bin(n)[2:]
		s = [i for i in s]
		ans = 0
		ind = 0
		while ind < len(s):
			c = s[ind]
			if c == '0':
				ind += 1
				continue
			cnt = 0
			while ind < len(s) and s[ind] == '1':
				cnt += 1
				if cnt == 3:
					s[ind] = '0'
					cnt = 0
				ind += 1
		return int(''.join(s), 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		for i in range(27, -1, -1):
			mask = 7 << i
			if n & mask == mask:
				bit = 1 << i
				n ^= bit
		return n


class Solution:

	def noConseBits(self, n: int) -> int:
		n = bin(n)[2:]
		n = list(n)
		i = 0
		count = 0
		while i <= len(n) - 1:
			if n[i] == '1' and count < 3:
				count += 1
			elif count > 0 and n[i] == '0':
				count = 0
			if count == 3:
				count = 0
				n[i] = '0'
			i += 1
		n = ''.join(n)
		return int(n, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		cnt = 0
		for i in range(31, -1, -1):
			if n & 1 << i:
				cnt += 1
				if cnt == 3:
					n = n ^ 1 << i
					cnt = 0
			else:
				cnt = 0
		return n


class Solution:

	def noConseBits(self, n: int) -> int:
		s = bin(n)[2:]
		count = 0
		res = []
		if len(s) < 3:
			return n
		else:
			i = 0
			while i < len(s):
				if i < len(s) - 2 and s[i] == s[i + 1] == s[i + 2] == '1':
					res.append('110')
					i += 3
				elif s[i] == '1':
					res.append('1')
					i += 1
				else:
					res.append('0')
					i += 1
		ans = ''.join(res)
		return int(ans, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		b = bin(n)[2:]
		b = b.replace('111', '110')
		res = int(b, 2)
		return res


class Solution:

	def noConseBits(self, n: int) -> int:
		bs = bin(n)[2:]
		tl = [i for i in bs]
		i = 0
		while i < len(bs) - 1:
			if bs[i] == '1':
				c = 1
				while c != 3:
					i += 1
					if i < len(bs) and bs[i] == '1':
						c += 1
					else:
						i += 1
						break
				else:
					tl[i] = '0'
					i += 1
			else:
				i += 1
		bs = ''.join(tl)
		return int(bs, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		bina = format(n, 'b')
		size = len(bina)
		fin = ''
		if size <= 2:
			return n
		fin = bina[0] + bina[1]
		for i in range(2, size):
			if bina[i] == '1':
				if fin[i - 1] == '1' and fin[i - 2] == '1':
					fin = fin + '0'
				else:
					fin = fin + '1'
			else:
				fin = fin + '0'
		return int(fin, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		for i in range(31, 1, -1):
			one = 1 << i
			two = 1 << i - 1
			three = 1 << i - 2
			if one & n != 0 and two & n != 0 and (three & n != 0):
				n = n ^ three
		return n


class Solution:

	def noConseBits(self, n: int) -> int:
		b = format(n, 'b')
		a = b
		for i in range(len(b)):
			if b[i:i + 3] == '111':
				a = b[:i + 2] + '0' + b[i + 3:]
				b = a
		y = int(b, 2)
		return y


class Solution:

	def noConseBits(self, n: int) -> int:
		if n < 6:
			return n
		binary = bin(n)[2:]
		for i in range(len(binary) - 2):
			if '111' == binary[i:i + 3]:
				binary = list(binary)
				binary[i + 2] = '0'
				binary = ''.join(binary)
		return int(binary, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		n = str(bin(n))
		n = n[2:]
		for i in range(len(n) - 2):
			if n[i:i + 3] == '111':
				n = n[:i] + '110' + n[i + 3:]
		return int(n, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		num = bin(n)
		num = num[2:]
		consec_1 = 0
		max_num = 0
		for i in range(len(num)):
			if num[i] == '1':
				consec_1 += 1
				if consec_1 == 3:
					consec_1 = 0
				else:
					max_num += 2 ** (len(num) - i - 1)
			else:
				consec_1 = 0
		return max_num


class Solution:

	def noConseBits(self, n: int) -> int:
		if not n:
			return 0

		def binary(n):
			ans = []
			while n:
				mod = n % 2
				ans.append(mod)
				n //= 2
			return ans[::-1]
		setbit = 0
		bi = binary(n)
		res = ''
		for i in bi:
			if i and setbit == 2:
				res += '0'
				setbit = 0
				continue
			if i:
				res += '1'
				setbit += 1
			else:
				res += '0'
				setbit = 0
		return int(res, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		l = [1]
		for i in range(0, 31):
			l.append(l[i] * 2)
		m = 32
		c = 0
		ans = 0
		for i in range(m - 1, -1, -1):
			if l[i] & n:
				c += 1
			elif c < 3:
				c = 0
			if c == 3:
				c = 0
			elif l[i] & n:
				ans += l[i]
		return ans


class Solution:

	def noConseBits(self, n: int) -> int:

		def fun(n):
			s = bin(n).replace('0b', '')
			for i in range(len(s) - 2):
				if s[i] == s[i + 1] == s[i + 2] == '1':
					s = s[:i + 2] + '0' + s[i + 3:]
			if '111' not in s:
				return int(s, 2)
			else:
				return fun(k)
		return fun(n)


import math

class Solution:

	def noConseBits(self, n: int) -> int:
		count = 0
		pos = 0
		ans = 0
		while n:
			if n & 1:
				ans += math.pow(2, pos)
				count += 1
			else:
				temp = pos
				while count >= 3:
					ans -= math.pow(2, temp - 3)
					count -= 3
					temp -= 3
				count = 0
			n >>= 1
			pos += 1
		while count >= 3:
			ans -= math.pow(2, pos - 3)
			count -= 3
			pos -= 3
		return int(ans)


import math as mt

class Solution:
	mx_bits = int(mt.log2(int(1000000000.0)))

	def noConseBits(self, n: int) -> int:
		curr = 0
		for i in range(self.mx_bits - 1, -1, -1):
			if n & 1 << i:
				curr += 1
			else:
				curr = 0
			if curr == 3:
				curr = 0
				n ^= 1 << i
		return n


class Solution:

	def noConseBits(self, n: int) -> int:
		b = bin(n)[2:]
		c = b.count('111')
		while c > 0:
			b = b.replace('111', '110')
			c -= 1
		ans = 0
		j = 0
		for i in range(len(b) - 1, -1, -1):
			ans += int(b[i]) * 2 ** j
			j += 1
		return ans


class Solution:

	def noConseBits(self, n: int) -> int:
		bit = '{:b}'.format(n)
		consbit = 0
		nbit = ''
		for i in bit:
			if i == '1' and consbit == 2:
				nbit += '0'
				consbit = 0
			elif i == '1':
				consbit += 1
				nbit += '1'
			else:
				nbit += '0'
				consbit = 0
		return int(nbit, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		nii = 40
		result = [0] * nii
		for ni in range(0, nii + 1):
			if n >= 2 ** (nii - ni):
				result[ni - 1] = 1
				n = n - 2 ** (nii - ni)
		ones_counter = 0
		for ni in range(0, nii):
			if result[ni] == 1:
				ones_counter = ones_counter + 1
			elif result[ni] == 0:
				ones_counter = 0
			if ones_counter == 3:
				result[ni] = 0
				ones_counter = 0
		final = 0
		for ni in range(0, nii):
			if result[ni] == 1:
				final = final + 2 ** (nii - ni - 1)
		return final


class Solution:

	def noConseBits(self, n: int) -> int:
		a = bin(n)
		b = list(a)
		if len(b) < 5:
			g = n
		for i in range(0, len(b)):
			if i == 0 and i == 1:
				continue
			if i == len(b) - 2:
				break
			if b[i] == '1' and b[i + 1] == '1' and (b[i + 2] == '1'):
				b[i + 2] = '0'
		c = ''.join(b)
		g = int(c, 2)
		return g


class Solution:

	def noConseBits(self, n: int) -> int:
		num = bin(n)[2:]
		output = ''
		i = 0
		while i < len(num):
			if num[i:i + 3] == '111':
				output += '110'
				i += 3
			else:
				output += num[i]
				i += 1
		return int(output, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		a = bin(n)[2:]
		b = ''
		c = 0
		for i in a:
			if i == '1':
				c += 1
				if c == 3:
					c = 0
			else:
				c = 0
			b += '1' if c else '0'
		return int(b, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		bits = []
		while n:
			bits.append(n % 2)
			n //= 2
		bits.reverse()
		bits.append(0)
		i = 0
		for (j, bit) in enumerate(bits):
			if bit:
				continue
			if j - i >= 3:
				for k in range(i + 2, j + 1, 3):
					bits[k] = 0
			i = j + 1
		bits.pop()
		ans = 0
		for x in bits:
			ans = ans * 2 + x
		return ans


class Solution:

	def noConseBits(self, n: int) -> int:
		i = 32
		while i >= 2:
			if n & 1 << i > 0 and n & 1 << i - 1 > 0 and (n & 1 << i - 2 > 0):
				n ^= 1 << i - 2
			i -= 1
		return n


class Solution:

	def noConseBits(self, n: int) -> int:
		num = bin(n)[2:]
		i = 0
		c = 1
		pos = len(num) - 1
		while pos >= 0:
			if num[pos] == '1':
				i += 1
			if num[pos] == '0':
				while i >= 3:
					num = num[:pos + 3] + '0' + num[pos + 4:]
					pos += 3
					i -= 3
				else:
					i = 0
			pos -= 1
		if i >= 3:
			while i >= 3:
				num = num[:pos + 3] + '0' + num[pos + 4:]
				pos += 3
				i -= 3
		return int(num, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		binary = bin(n)[2:].zfill(32)
		j = 0
		store = set()
		for (index, i) in enumerate(binary):
			if i == '0':
				j = 0
			else:
				j += 1
			if j % 3 == 0:
				store.add(index)
		newNum = []
		for (index, i) in enumerate(binary):
			if index in store:
				newNum.append('0')
			else:
				newNum.append(i)
		return int(''.join(newNum), 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		for i in range(30, 1, -1):
			mask1 = 1 << i
			mask2 = 1 << i - 1
			mask3 = 1 << i - 2
			if mask1 & n and mask2 & n and mask3 & n:
				n = n ^ mask3
		return n


class Solution:

	def noConseBits(self, n: int) -> int:
		x = bin(n).replace('0b', '')
		ans = ''
		cnt = 0
		for i in x:
			if i == '1' and cnt == 2:
				ans += '0'
				cnt = 0
				continue
			elif i == '0':
				ans += i
				cnt = 0
				continue
			ans += i
			cnt += 1
		return int(ans, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		if n < 7:
			return n
		d = list(bin(n))
		d1 = d[2:]
		for i in range(len(d1) - 2):
			if d1[i] == '1' and d1[i + 1] == '1' and (d1[i + 2] == '1'):
				d1[i + 2] = '0'
		d = ''.join(d1)
		return int(d, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		ans = bin(n)[2:].replace('111', '110')
		return int(ans, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		if n > 6:
			x = bin(n)
			k = list(x)
			k = k[2:]
			c = 0
			for i in range(len(k)):
				if k[i] == '1':
					c += 1
				else:
					c = 0
				if c == 3:
					k[i] = '0'
					c = 0
			s = ''.join(k)
			return int(s, 2)
		else:
			return n


class Solution:

	def noConseBits(self, n: int) -> int:
		(i, j, k) = (0, 1, 2)
		n = list(bin(n)[2:])
		while k < len(n):
			if n[i] == '1' and n[j] == '1' and (n[k] == '1'):
				n[k] = '0'
			i += 1
			j += 1
			k += 1
		return eval('0b' + ''.join(n))


class Solution:

	def noConseBits(self, n: int) -> int:
		arr = [0] * 32
		for i in range(31, -1, -1):
			if n & 1 << 31 - i != 0:
				arr[i] = 1
		ans = 0
		for i in range(30):
			if arr[i] == 1:
				ans += 1 << 31 - i
				if arr[i + 1] == 1:
					arr[i + 2] = 0
		if arr[30] == 1:
			ans += 2
		if arr[31] == 1:
			ans += 1
		return ans


class Solution:

	def noConseBits(self, n: int) -> int:
		j = bin(n)[2:]
		stri = str(j)
		if '111' in stri:
			stri = stri.replace('111', '110')
			for i in range(n, 0, -1):
				a = bin(i)[2:]
				if str(a) == stri:
					k = i
					break
		else:
			k = n
		return k


class Solution:

	def noConseBits(self, n: int) -> int:
		s = ''
		if n == 2:
			return 2
		z = bin(n)[2:]
		arr = list(z)
		for i in range(len(arr) - 2):
			if arr[i] == arr[i + 1] == arr[i + 2] == '1':
				arr[i + 2] = '0'
		for i in arr:
			s = s + i
		return int(s, 2)


class Solution:

	def noConseBits(self, n: int) -> int:
		if n < 7:
			return n
		a = list(bin(n))
		a = a[2:]
		for i in range(len(a) - 2):
			if a[i:i + 3].count('1') == 3:
				a[i + 2] = '0'
		a = ''.join(a)
		ans = int(a, 2)
		return ans