Given two numbers N and K, change the Kth (1-based indexing) bit from the left of the binary representation of the number N to '0' if it is  '1', else return the number N itself.
Example 1:
Input:
N = 13, K = 2
Output: 9
Explanation: Binary representation of 13 is
1101. The 2nd bit from left is 1, we make
it 0 and result is 1001 = 9 (decimal).
Example 2:
Input: 
N = 13, K = 6
Output: 13
Explanation: Binary representation of 13 is
1101. There's no 6th bit from left,
hence we return the number itself.
Your Task:
You don't need to read input or print anything. Your task is to complete the function replaceBit() which takes the integers N and K as inputs and returns the resultant number after the stated modifications.
Expected Time Complexity: O(LogN).
Expected Auxiliary Space: O(1).
Constraints:
1<=N<=10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:

    def replaceBit(self, N, K):

        # code here

Solutions:
class Solution:

	def replaceBit(self, N, K):
		n = list(bin(N)[2:])
		if len(n) >= K:
			ind = K - 1
			if n[ind] == '1':
				n[ind] = '0'
				return int(''.join(n), 2)
			else:
				return N
		else:
			return N


class Solution:

	def replaceBit(self, N, K):
		a = bin(N)
		a = a.replace('0b', '')
		if k > len(a):
			return N
		else:
			a = a[:k - 1] + '0' + a[k:]
			return int(a, 2)


class Solution:

	def replaceBit(self, N, K):
		b = str(bin(N).replace('0b', ''))
		b = [*b]
		if K > len(b):
			return N
		if b[K - 1] == '1':
			b[K - 1] = '0'
		d = ''
		for i in b:
			d += i
		return int(d, 2)


import math

class Solution:

	def replaceBit(self, N, K):
		pos = int(math.log(N, 2)) + 1
		if K > pos:
			return N
		return N & ~(1 << pos - k)


class Solution:

	def replaceBit(self, N, K):
		n = bin(N).replace('0b', '')
		N = list((i for i in n))
		if len(N) >= K and K != 0:
			N[K - 1] = '0'
		p_ans = ''.join(N)
		return int(p_ans, 2)


class Solution:

	def replaceBit(self, N, K):
		ans = list(bin(N)[2:])
		if K - 1 < len(ans):
			ans[K - 1] = '0'
		return int(''.join(ans), 2)


class Solution:

	def replaceBit(self, N, k, sum=0):
		m = str(bin(N)[2:])
		if len(m) >= k and m[k - 1] == '1':
			st = m[:k - 1] + '0' + m[k:]
			for i in st:
				sum = sum * 2 + pow(int(i) % 2, 2)
			return sum
			return sum
		return N


class Solution:

	def replaceBit(self, N, K):
		k = -1
		for i in range(16, -1, -1):
			if N & 1 << i:
				k = i
				break
		if k - K + 1 >= 0:
			N &= ~(1 << k - K + 1)
		return N


class Solution:

	def replaceBit(self, N, K):
		b = bin(N)[2:]
		if len(b) < K or b[K - 1] == 0:
			return N
		return int(b[:K - 1] + '0' + b[k:], 2)


class Solution:

	def replaceBit(self, N, K):
		binary_num = list(bin(N).replace('0b', ''))
		if len(binary_num) >= K:
			n = K - 1
			if binary_num[n] == '1':
				binary_num[n] = '0'
				return int(''.join(binary_num), 2)
			else:
				return N
		else:
			return N


class Solution:

	def replaceBit(self, N, K):
		num = bin(N)[2:]
		if K > len(num):
			return N
		output = num[:K - 1]
		output += '0'
		output += num[K:]
		return int(output, 2)


import math

class Solution:

	def replaceBit(self, N, K):
		index = int(math.log2(n)) + 1
		if K > index:
			return N
		return N & ~(1 << index - K)


class Solution:

	def replaceBit(self, N, K):
		a = bin(n).replace('0b', '')
		b = list(a)
		if len(b) < K:
			return N
		if b[K - 1] == '1':
			b[K - 1] = '0'
		sum = 0
		p = 2 ** (len(b) - 1)
		for i in b:
			sum += int(i) * p
			p /= 2
		return int(sum)


class Solution:

	def replaceBit(self, N, K):
		x = bin(N)[2:]
		if K > len(x):
			return int(x, 2)
		new = ''
		for i in range(len(x)):
			if i + 1 == K:
				new += '0'
			else:
				new += x[i]
		return int(new, 2)


class Solution:

	def replaceBit(self, N, K):
		bit_len = N.bit_length()
		if bit_len < K:
			return N
		if N >> bit_len - K & 1 > 0:
			return N & ~(1 << bit_len - K)
		return N


class Solution:

	def replaceBit(self, N, K):
		bits = []
		n = N
		while n > 0:
			bits.append(n % 2)
			n = n // 2
		if K > len(bits):
			return N
		if bits[-1 * K] == 1:
			diff = 2 ** (len(bits) - K)
			return N - diff
		return N


class Solution:

	def replaceBit(self, N, K):
		count = 0
		n = N
		while n:
			count += 1
			n = n >> 1
		if K > count:
			return N
		return N & ~(1 << count - K)


class Solution:

	def replaceBit(self, N, K):
		bn = list(bin(N)[2:])
		if K > len(bn):
			return N
		bn[K - 1] = '0'
		st = ''
		for e in bn:
			st = st + e
		N = int(st, 2)
		return N


class Solution:

	def replaceBit(self, N, K):
		binary = list(format(int(N), 'b'))
		if len(binary) >= int(K) and binary[int(K) - 1] == '1':
			binary[int(K) - 1] = '0'
		return int(''.join(binary), 2)


class Solution:

	def replaceBit(self, N, K):
		n = bin(N)[2:]
		n = list(n)
		if k > len(n):
			return N
		if n[k - 1] == '1':
			n[k - 1] = '0'
		num = 0
		n = n[::-1]
		for i in range(len(n)):
			if n[i] == '1':
				num += 1 << i
		return num


class Solution:

	def replaceBit(self, N, K):
		a = bin(N).replace('0b', '')
		b = list(str(a))
		if len(b) >= K:
			b[K - 1] = '0'
		c = ''.join(b)
		d = int(c, 2)
		return d


class Solution:

	def replaceBit(self, N, K):
		if K == 0 or N == 0:
			return N
		b = bin(N)[2:]
		b = list(b)
		index = -1
		if K > len(b):
			return N
		for i in range(len(b)):
			if i == K - 1:
				index = i
		if b[index] == '1':
			b[index] = '0'
		s = ''.join(map(str, b))
		return int(s, 2)


class Solution:

	def replaceBit(self, N, K):
		binV = str(bin(N)[2:])
		ans = ''
		for i in range(0, len(binV)):
			if k - 1 == i:
				if binV[i] == '1':
					ans += '0'
				else:
					ans += binV[i]
			else:
				ans += binV[i]
		return int(ans, 2)


class Solution:

	def replaceBit(self, N, K):
		s = ''
		t = bin(N)
		s1 = t[2:]
		l = list(s1)
		if len(s1) < K:
			return N
		if l[K - 1] == '1':
			l[K - 1] = '0'
		for i in l:
			s += i
		return int(s, 2)


class Solution:

	def replaceBit(self, N, K):
		p = bin(N).replace('0b', '')
		l = list(p)
		if K > len(l):
			return N
		elif l[K - 1] == '1':
			l[K - 1] = '0'
		else:
			return N
		res = ''
		for i in l:
			res = res + i


class Solution:

	def replaceBit(self, N, k):
		m = ''
		t = bin(N)[2:]
		if len(t) < k:
			return N
		for i in range(len(t)):
			if i == k - 1:
				m += '0'
			else:
				m += t[i]
		q = int(m, 2)
		return q


class Solution:

	def replaceBit(self, N, K):
		li = []
		t = N
		while N:
			bit = N & 1
			li.append(bit)
			N >>= 1
		if len(li) < k:
			return t
		li = li[::-1]
		if li[K - 1] == 0:
			return t
		li[K - 1] = 0
		N = 0
		base = 1
		for i in range(len(li) - 1, -1, -1):
			N += li[i] * base
			base *= 2
		return N


class Solution:

	def replaceBit(self, N, K):
		n = bin(N)
		bin_str = '0b'
		for i in range(2, len(n)):
			if i - 1 == K:
				if n[i] == '1':
					bin_str += '0'
					continue
				else:
					return N
			bin_str += n[i]
		return int(bin_str, 2)


import math

class Solution:

	def replaceBit(self, N, K):
		pos = math.floor(math.log2(N)) + 1
		if pos < K:
			return N
		mask = 1 << pos - K
		N = ~mask & N
		return N


class Solution:

	def replaceBit(self, N, K):
		temp = N
		z = ''
		while N > 0:
			z += str(N % 2)
			N = int(N / 2)
		z = list(z[::-1])
		if K - 1 > len(z) - 1 or K == 0:
			return temp
		elif z[K - 1] == '1':
			z[k - 1] = '0'
			z = ''.join(z)
			z = int(z, 2)
			return z
		else:
			return temp


class Solution:

	def replaceBit(self, N, K):
		global k1, s
		k1 = k
		s = ''
		self.helper(n)
		return int(s, 2)

	def helper(self, n):
		global k1, s
		if n > 1:
			self.helper(n // 2)
		k1 -= 1
		if k1 == 0:
			s += '0'
		else:
			s += str(n % 2)


class Solution:

	def replaceBit(self, N, K):
		bini = format(N, 'b')
		st = str(bini)
		num = 0
		if K > len(st):
			return N
		if st[K - 1] == '1':
			li = list(st)
			li[K - 1] = '0'
			li.reverse()
			st = ''.join(li)
			i = 0
			while i < len(st):
				if st[i] != '0':
					num += int(pow(2, i))
				i += 1
			return num
		else:
			return N


class Solution:

	def replaceBit(self, N, K):
		res = ''
		N1 = bin(N).replace('0b', '')
		for i in range(len(N1)):
			if i == K - 1:
				if N1[i] == '1':
					res += '0'
				else:
					return N
			else:
				res += N1[i]
		return int(res, 2)


class Solution:

	def replaceBit(self, N, K):
		b1 = bin(N)[2:]
		if len(b1) < K:
			return N
		else:
			l1 = list(b1)
			if l1[K - 1] == '1':
				l1[K - 1] = '0'
			b1 = ''.join(l1)
			return int(b1, 2)


class Solution:

	def replaceBit(self, N, K):
		x = list(bin(N)[2:])
		if len(x) < K:
			return N
		else:
			if x[K - 1] == '1':
				x[K - 1] = '0'
			s = ''
			for i in x:
				s += i
			p = int(s, 2)
			return p


class Solution:

	def replaceBit(self, N, K):
		new = list(bin(N)[2:])
		try:
			new[K - 1] = '0'
			new = ''.join(new)
			return int(new, 2)
		except:
			return N


class Solution:

	def replaceBit(self, N, K):
		s = bin(N).replace('0b', '')
		s = list(s)
		if len(s) >= K:
			s[K - 1] == '1'
			s[K - 1] = '0'
			p = ''.join(s)
			return int(p, 2)
		else:
			return N


class Solution:

	def replaceBit(self, N, K):
		n = bin(N)[2:]
		if len(n) < K:
			return N
		if n[K - 1] == '1':
			n = list(n)
			n[K - 1] = '0'
			return eval('0b' + ''.join(n))
		return N


class Solution:

	def replaceBit(self, N, K):
		b = bin(N)[2:]
		b = [x for x in b]
		if K > len(b):
			return N
		b[K - 1] = '0'
		res = ''.join(b)
		return int(res, 2)


import math

class Solution:

	def replaceBit(self, N, K):
		bits = 0
		temp = N
		while temp:
			bits += 1
			temp = temp >> 1
		if K > bits:
			return N
		return N & ~(1 << bits - K)


class Solution:

	def replaceBit(self, N, K):
		binum = bin(N).replace('0b', '')
		for i in range(1, len(binum) + 1):
			if i == K and binum[i - 1] == '1':
				ans = binum[:i - 1] + '0' + binum[i:]
				return int(ans, 2)
		else:
			return N
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(n, k) = input().strip().split(' ')
		(n, k) = (int(n), int(k))
		ob = Solution()
		ans = ob.replaceBit(n, k)
		print(ans)


class Solution:

	def replaceBit(self, N, K):
		a = N
		l = []
		b = ''
		c = ''
		while a != 0:
			r = a % 2
			l.append(r)
			a = a // 2
		l.reverse()
		for x in l:
			b = b + str(x)
		for y in range(len(b)):
			if y + 1 == K and ord(b[y]) == 49:
				c = c + '0'
			else:
				c = c + b[y]
		sum = 0
		i = 0
		num = int(c)
		while num != 0:
			rem = int(num) % 10
			sum = int(sum) + rem * pow(2, i)
			num = int(num) / 10
			i = i + 1
		return sum
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(n, k) = input().strip().split(' ')
		(n, k) = (int(n), int(k))
		ob = Solution()
		ans = ob.replaceBit(n, k)
		print(ans)


class Solution:

	def replaceBit(self, N, K):
		n = bin(N)
		n = n[2:]
		if K > len(n):
			return N
		r = n[:K - 1]
		t = n[K:]
		z = n[K - 1]
		z = z.replace('1', '0')
		N = r + z + t
		return int(N, 2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(n, k) = input().strip().split(' ')
		(n, k) = (int(n), int(k))
		ob = Solution()
		ans = ob.replaceBit(n, k)
		print(ans)


class Solution:

	def getBinary(self, decimal):
		binary = ''
		while decimal != 0:
			rem = decimal % 2
			binary = str(rem) + binary
			decimal //= 2
		return binary

	def getDecimal(self, binary):
		decimal = 0
		i = 0
		while binary != 0:
			rem = binary % 10
			decimal += rem * 2 ** i
			i += 1
			binary //= 10
		return decimal

	def replaceBit(self, N, K):
		binary = self.getBinary(N)
		lst = list(map(str, binary))
		if k - 1 < len(lst) and lst[k - 1] == '1':
			lst[k - 1] = '0'
			dec = ''.join(lst)
			decimal = self.getDecimal(int(dec))
			return decimal
		return N
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(n, k) = input().strip().split(' ')
		(n, k) = (int(n), int(k))
		ob = Solution()
		ans = ob.replaceBit(n, k)
		print(ans)


class Solution:

	def replaceBit(self, N, K):
		binary = list(str(bin(N)))
		binary = binary[2:]
		if K > len(binary):
			return N
		elif binary[K - 1] == '0':
			return N
		else:
			binary[K - 1] = '0'
			num = ''.join(binary)
			return int(num, 2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(n, k) = input().strip().split(' ')
		(n, k) = (int(n), int(k))
		ob = Solution()
		ans = ob.replaceBit(n, k)
		print(ans)


class Solution:

	def replaceBit(self, N, K):
		s = bin(N)
		s = s[2:]
		d = len(s) - 1
		c = 1
		t = ''
		for i in s:
			if K == c:
				t += '0'
			else:
				t += i
			c += 1
		s = 0
		for i in t:
			if i == '1':
				s += 2 ** d
			d -= 1
		return s
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(n, k) = input().strip().split(' ')
		(n, k) = (int(n), int(k))
		ob = Solution()
		ans = ob.replaceBit(n, k)
		print(ans)


class Solution:

	def replaceBit(self, N, K):
		if K == 0 or N == 0:
			return N
		b = bin(N)[2:]
		b = list(b)
		index = -1
		if K > len(b):
			return N
		for i in range(len(b)):
			if i == K - 1:
				index = i
		if b[index] == '1':
			b[index] = '0'
		s = ''.join(map(str, b))
		return int(s, 2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(n, k) = input().strip().split(' ')
		(n, k) = (int(n), int(k))
		ob = Solution()
		ans = ob.replaceBit(n, k)
		print(ans)


class Solution:

	def replaceBit(self, N, K):
		string1 = ''
		while N > 0:
			rem = N % 2
			string1 += str(rem)
			N = int(N / 2)
		binary_string = string1[::-1]
		binary_string2 = ''
		for i in range(len(binary_string)):
			if i == K - 1:
				binary_string2 += '0'
			else:
				binary_string2 += binary_string[i]
		sum = 0
		string2 = binary_string2[::-1]
		for i in range(len(string2)):
			sum += int(string2[i]) * 2 ** i
		return sum


class Solution:

	def replaceBit(self, n, k):
		t = bin(n)[2:]
		try:
			if t[k - 1] == '0':
				return n
		except:
			return n
		z = ''
		for i in range(len(t)):
			if i == k - 1:
				z += '0'
			else:
				z += '1'
		return n & int(z, 2)


class Solution:

	def replaceBit(self, N, K):
		bit = bin(N).replace('0b', '')
		s = ''
		for i in range(len(bit)):
			if k - 1 < len(bit) and i == K - 1:
				s += '0'
			else:
				s += bit[i]
		return int(s, 2)


class Solution:

	def replaceBit(self, N, K):
		x = 1
		p = 0
		nn = N
		while nn:
			p += 1
			nn = nn // 2
		K = p - K
		ans = 0
		cnt = 0
		num = N
		while N:
			a = N % 2
			if cnt == K and a == 1:
				ans += 0 * x
			else:
				ans += a * x
			x = x * 2
			cnt += 1
			N = N // 2
		if K >= cnt:
			return num
		return ans


class Solution:

	def replaceBit(self, N, K):
		X = N
		c = 0
		while X != 0:
			X = X >> 1
			c += 1
		if K > c:
			return N
		else:
			return N & ~(1 << c - K)


class Solution:

	def replaceBit(self, N, K):

		def binary(no):
			if no == 1:
				return '1'
			else:
				r = no % 2
				r = str(r)
				return r + binary(no // 2)
		n = binary(N)
		n = n[::-1]
		if len(n) >= K:
			n = list(n)
			if n[K - 1] == '1':
				n[K - 1] = '0'
				c = 0
				s = 0
				for i in range(len(n) - 1, -1, -1):
					if n[i] == '1':
						s = s + 1 * 2 ** c
					c = c + 1
				return s
			else:
				return N
		return N


class Solution:

	def replaceBit(self, N, K):
		s = N
		n = N
		n = bin(n).replace('0b', '')
		if len(n) >= K:
			n = list(n)
			if n[K - 1] == '1':
				n[K - 1] = '0'
				c = 0
				s = 0
				for i in range(len(n) - 1, -1, -1):
					if n[i] == '1':
						s = s + 1 * 2 ** c
					c = c + 1
				return s
			else:
				return N
		return N


class Solution:

	def replaceBit(self, n, k):
		k = k - 1
		binary = bin(n)[2:]
		if k < len(binary) and binary[k] == '1':
			l1 = list(binary)
			l1[k] = '0'
			ans = ''.join(l1)
			finalans = int(ans, 2)
			return finalans
		else:
			return n


class Solution:

	def replaceBit(self, N, K):
		bit = bin(N).replace('0b', '')
		try:
			arr = list(bit)
			if arr[K - 1] == '1':
				arr[K - 1] = '0'
			return int(''.join(arr), 2)
		except:
			return N


import math

class Solution:

	def replaceBit(self, N, K):
		length = int(math.log(n, 2)) + 1
		if K > length:
			return N
		return ~(~N | 1 << length - K)


import math

class Solution:

	def replaceBit(self, N, K):
		n = int(math.log(N) / math.log(2) + 1)
		if n < K:
			return N
		p = n - K
		mask = ~(1 << p)
		return N & mask


class Solution:

	def replaceBit(self, N, K):
		if K > N:
			return N
		temp = list(bin(N)[2:])
		if len(temp) >= K:
			temp[K - 1] = '0'
			temp = ''.join(temp)
			result = int(temp, 2)
		else:
			return N
		return result


class Solution:

	def replaceBit(self, N, K):
		N = bin(N)
		N = N[2:]
		s = ''
		for i in range(len(N)):
			if i == K - 1 and N[i] == '1':
				s += '0'
			else:
				s += N[i]
		m = int(s, 2)
		return m


import math

class Solution:

	def replaceBit(self, N, K):
		n = int(math.log2(N)) + 1
		if n >= k:
			c = 1 << n - K
			if N & c != 0:
				return N ^ c
			return N
		return N


class Solution:

	def replaceBit(self, N, K):
		new_num = bin(n).replace('0b', '')
		ls = []
		for x in new_num:
			ls.append(x)
		if k <= len(ls):
			if ls[k - 1] == str(1):
				ls[k - 1] = '0'
		st = ''
		for y in ls:
			st += y
		return int(st, 2)


import math

class Solution:

	def replaceBit(self, N, K):
		l = self.getLength(N)
		if K > l:
			return N
		mask = 1 << l - K
		return N & ~mask

	def getLength(self, N):
		return int(math.log2(N)) + 1


import math

class Solution:

	def replaceBit(self, N, K):
		bits = math.floor(math.log(N, 2)) + 1
		if K > bits:
			return N
		K = bits - K
		return (N | 1 << K) ^ 1 << K


class Solution:

	def replaceBit(self, n, k):
		p = n
		s = ''
		while n:
			m = n & 1
			s = str(m) + s
			n = n >> 1
		l = len(s)
		if k > l:
			return p
		elif s[k - 1] == '0':
			return p
		return p - 2 ** (l - k)


class Solution:

	def replaceBit(self, N, K):
		flag = 0
		count = 0
		temp = N
		while temp != 0:
			count += 1
			temp = temp >> 1
		if count < k:
			return N
		else:
			pos = count - k
			temp = N
			while temp != 0:
				if pos == 0:
					if temp & 1 == 0:
						flag = 1
						break
				pos -= 1
				temp = temp >> 1
			if flag == 1:
				return N
			else:
				mask = 1 << count - K
				return N ^ mask


class Solution:

	def replaceBit(self, N, K):
		K = K - 1
		s = bin(N)[2:]
		l = list(s)
		if len(l) <= K or l[K] == '0':
			return N
		else:
			l[K] = '0'
			n = int(''.join(l), 2)
			return n


class Solution:

	def replaceBit(self, N, K):
		bi = bin(N)[2:]
		m = len(bi)
		if K > m:
			return N
		num = N >> m - K & 1
		if num != 0:
			return N ^ 1 << m - K
		else:
			return N


class Solution:

	def replaceBit(self, N, K):
		x = ''
		b = '{0:b}'.format(N)
		for i in range(len(b)):
			if K <= len(b):
				if b[K - 1] == '1':
					x = b[:K - 1] + '0' + b[K:]
				else:
					x = b
			else:
				x = b
		return int(x, 2)


class Solution:

	def replaceBit(self, N, K):
		li = []
		num = N
		while N > 0:
			li.append(N % 2)
			N = N // 2
		li = li[::-1]
		if K > len(li):
			return num
		elif li[K - 1] == 1:
			li[K - 1] = 0
		else:
			return num
		l = len(li)
		for i in range(l):
			li[i] = li[i] * 2 ** (l - i - 1)
		return sum(li)