description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N. He is a little weak in maths. Help him find the number of integers. Note : Since N takes large values, brute force won't work. Example 1: Input: N = 8 Output: 3 Explanation: Binary representation of 8 : 1000 So the integers less than 8 with same number of set bits are : 4, 2, 1 Hence, the result is 3. Example 2: Input: N = 4 Output: 2 Explanation: Binary representation of 4 : 100 So the integers less than 4 with same number of set bits are : 2, 1 So, The result is 2. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer. Expected Time Complexity: O(log(n)) Expected Auxiliary Space: O(log(n)*log(n)) Constraints : 1 <= N <= 10^12
class Solution: def count(self, Num): def ncr(n, r): if r > n: return 0 N = 1 D = 1 while r > 0: N *= n D *= r n -= 1 r -= 1 return N / D if Num == 1: return 0 i = 0 ans = 0 cnt = 0 while Num != 0: if Num & 1 == 1: cnt += 1 ans += ncr(i, cnt) Num >>= 1 i += 1 return int(ans)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N. He is a little weak in maths. Help him find the number of integers. Note : Since N takes large values, brute force won't work. Example 1: Input: N = 8 Output: 3 Explanation: Binary representation of 8 : 1000 So the integers less than 8 with same number of set bits are : 4, 2, 1 Hence, the result is 3. Example 2: Input: N = 4 Output: 2 Explanation: Binary representation of 4 : 100 So the integers less than 4 with same number of set bits are : 2, 1 So, The result is 2. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer. Expected Time Complexity: O(log(n)) Expected Auxiliary Space: O(log(n)*log(n)) Constraints : 1 <= N <= 10^12
class Solution: def count(self, N): def fac(n): if n == 1: return 1 t = n * fac(n - 1) factorial[n] = t return factorial[n] bin_n = bin(N).split("0b")[1] n = len(bin_n) factorial = [1] * (n + 1) n = len(bin_n) fac(n) ans = 0 once = bin_n.count("1") for i in range(n - 1): if bin_n[i] == "1": nn = n - (i + 1) ans += factorial[nn] / (factorial[once] * factorial[nn - once]) once -= 1 if once <= 0: break return int(ans)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N. He is a little weak in maths. Help him find the number of integers. Note : Since N takes large values, brute force won't work. Example 1: Input: N = 8 Output: 3 Explanation: Binary representation of 8 : 1000 So the integers less than 8 with same number of set bits are : 4, 2, 1 Hence, the result is 3. Example 2: Input: N = 4 Output: 2 Explanation: Binary representation of 4 : 100 So the integers less than 4 with same number of set bits are : 2, 1 So, The result is 2. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer. Expected Time Complexity: O(log(n)) Expected Auxiliary Space: O(log(n)*log(n)) Constraints : 1 <= N <= 10^12
class Solution: def count(self, n): dp = [[(-1) for i in range(64)] for j in range(64)] def nCr(n, r): if r > n: return 0 if r == 0 or r == n: return 1 if dp[n][r] != -1: return dp[n][r] dp[n][r] = nCr(n - 1, r - 1) + nCr(n - 1, r) return dp[n][r] ones = 0 positions = 0 ans = 0 while n > 0: if n & 1 == 1: ones += 1 ans += nCr(positions, ones) n = n >> 1 positions += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N. He is a little weak in maths. Help him find the number of integers. Note : Since N takes large values, brute force won't work. Example 1: Input: N = 8 Output: 3 Explanation: Binary representation of 8 : 1000 So the integers less than 8 with same number of set bits are : 4, 2, 1 Hence, the result is 3. Example 2: Input: N = 4 Output: 2 Explanation: Binary representation of 4 : 100 So the integers less than 4 with same number of set bits are : 2, 1 So, The result is 2. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer. Expected Time Complexity: O(log(n)) Expected Auxiliary Space: O(log(n)*log(n)) Constraints : 1 <= N <= 10^12
class Solution: def comb(self, n, r): return self.fact(n) // (self.fact(n - r) * self.fact(r)) def fact(self, n): if n == 0: return 1 else: return self.fact(n - 1) * n def solve(self, x, one, start, n): if start == n: return 0 elif x[start] == "0": start += 1 return self.solve(x, one, start, n) elif x[start] == "1" and n - start - 1 >= one: return self.comb(n - start - 1, one) + self.solve(x, one - 1, start + 1, n) else: return 0 def reverse1(self, s): out = "" n = len(s) - 1 for i in range(n, -1, -1): out += s[i] return out def count(self, x): s = "" one = 0 while x >= 0: y = x % 2 if y == 1: one += 1 s = s + str(y) if x == 1 or x == 0: break x //= 2 s = self.reverse1(s) n = len(s) out = self.solve(s, one, 0, n) return out
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR STRING VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER FUNC_DEF ASSIGN VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N. He is a little weak in maths. Help him find the number of integers. Note : Since N takes large values, brute force won't work. Example 1: Input: N = 8 Output: 3 Explanation: Binary representation of 8 : 1000 So the integers less than 8 with same number of set bits are : 4, 2, 1 Hence, the result is 3. Example 2: Input: N = 4 Output: 2 Explanation: Binary representation of 4 : 100 So the integers less than 4 with same number of set bits are : 2, 1 So, The result is 2. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer. Expected Time Complexity: O(log(n)) Expected Auxiliary Space: O(log(n)*log(n)) Constraints : 1 <= N <= 10^12
class Solution: def fact(self, n): if n == 0 or n == 1: return 1 d = 1 for i in range(1, n + 1): d *= i return d def ncr(self, n, r): if r > n: return 0 ff = self.fact(n) // (self.fact(r) * self.fact(n - r)) return ff def count(self, N): i = 1 ans = 0 one = 0 while N > 0: if N & 1 == 1: one += 1 k = self.ncr(i - 1, one) ans += k i += 1 N = N >> 1 return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N. He is a little weak in maths. Help him find the number of integers. Note : Since N takes large values, brute force won't work. Example 1: Input: N = 8 Output: 3 Explanation: Binary representation of 8 : 1000 So the integers less than 8 with same number of set bits are : 4, 2, 1 Hence, the result is 3. Example 2: Input: N = 4 Output: 2 Explanation: Binary representation of 4 : 100 So the integers less than 4 with same number of set bits are : 2, 1 So, The result is 2. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer. Expected Time Complexity: O(log(n)) Expected Auxiliary Space: O(log(n)*log(n)) Constraints : 1 <= N <= 10^12
class Solution: def count(self, n): def nck(n, r): if r > n: return 0 res = 1 for i in range(r): res *= n - i res //= i + 1 return res if N == 1: return 0 num = N cnt = 0 i = 0 ans = 0 while num != 0: if num & 1 == 1: cnt += 1 ans += nck(i, cnt) i += 1 num >>= 1 return ans des = [] limit = int(format(n, "0b")) def permutation(l, c, m): if l == 0 and c == 0: if m < limit: des.append(m) else: if l > 0 and m * 10 + 0 < limit: permutation(l - 1, c, m * 10 + 0) if c > 0 and m * 10 + 1 < limit: permutation(l, c - 1, m * 10 + 1) count = 0 lens = 0 k = n while n: count += n & 1 n >>= 1 lens += 1 permutation(lens - count, count, 0) return len(des)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_DEF IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N. He is a little weak in maths. Help him find the number of integers. Note : Since N takes large values, brute force won't work. Example 1: Input: N = 8 Output: 3 Explanation: Binary representation of 8 : 1000 So the integers less than 8 with same number of set bits are : 4, 2, 1 Hence, the result is 3. Example 2: Input: N = 4 Output: 2 Explanation: Binary representation of 4 : 100 So the integers less than 4 with same number of set bits are : 2, 1 So, The result is 2. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer. Expected Time Complexity: O(log(n)) Expected Auxiliary Space: O(log(n)*log(n)) Constraints : 1 <= N <= 10^12
class Solution: def factorial(self, n): if n == 0: return 1 return n * self.factorial(n - 1) def fact(self, n, r): n1 = self.factorial(n) r1 = self.factorial(r) n_r = self.factorial(abs(n - r)) return n1 // (n_r * r1) def count(self, N): ans = 0 ori = bin(N)[2:] for i in range(len(ori)): if ori[i] == "1" and i != len(ori) - 1: set_bits = 1 rest = 0 for j in range(i + 1, len(ori)): if ori[j] == "1": set_bits += 1 rest += 1 ans += self.fact(rest, set_bits) return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR
Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N. He is a little weak in maths. Help him find the number of integers. Note : Since N takes large values, brute force won't work. Example 1: Input: N = 8 Output: 3 Explanation: Binary representation of 8 : 1000 So the integers less than 8 with same number of set bits are : 4, 2, 1 Hence, the result is 3. Example 2: Input: N = 4 Output: 2 Explanation: Binary representation of 4 : 100 So the integers less than 4 with same number of set bits are : 2, 1 So, The result is 2. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer. Expected Time Complexity: O(log(n)) Expected Auxiliary Space: O(log(n)*log(n)) Constraints : 1 <= N <= 10^12
class Solution: def f(self, n, m): if n == 0: return 0 if n < m: return 0 x = 1 r2 = 1 if n - m > m: for i in range(n, n - m, -1): x = x * i for j in range(1, m + 1): r2 = r2 * j return x / r2 else: for i in range(n, m, -1): x = x * i for j in range(1, n - m + 1): r2 = r2 * j return x / r2 def count(self, N): n = bin(N).replace("0b", "") n0 = 0 n1 = 0 c = 0 for i in range(len(n) - 1, -1, -1): if n[i] == "1": n1 = n1 + 1 c = c + self.f(len(n) - i - 1, n1) return int(c)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given an undirected graph G = (V, E). We define a function f(s) for s ⊆ V to be the number of edges in the induced subgraph of s. The problem asks you to calculate the sum of f(s)^{k} over all s in 2^{|V|} subsets of V. As the answer could be very large, output it modulo (10^{9}+7). ------ Input ------ The first line of input contains an integer T denoting the number of test cases. For each test case, the first line contains three space-separated integers n = |V|, m = |E| and k. Then m lines follow, each line contains two space-separated integers u, v denoting an edge (u, v) is in E. ------ Output ------ For each test case, output one line containing one integer, the answer modulo (10^{9}+7). ------ Constraints ------ 1 ≤ T ≤ 100 2 ≤ n ≤ 10^{5} 0 ≤ m ≤ 10^{5} Sum of each of n, m over all test cases ≤ 3 * 10^{5} 1 ≤ u, v ≤ n. 1 ≤ k ≤ 3. The graph is simple, i.e., doesn't contain self loops and multiple edges. ------ Subtasks ------ Subtask #1 (8 points): T, n ≤ 15 Subtask #2 (7 points): k = 1 Subtask #3 (9 points): k = 2 Subtask #4 (15 points): k = 3. Sum of n over all test cases ≤ 300 Sum of m over all test cases ≤ 300 Subtask #5 (24 points): k = 3. Sum of n over all test cases ≤ 3000 Sum of m over all test cases ≤ 3000 Subtask #6 (37 points): Original Constraints ----- Sample Input 1 ------ 3 3 3 1 1 2 2 3 3 1 4 5 2 1 2 2 3 3 4 4 1 2 4 5 4 3 1 2 1 3 1 4 2 5 ----- Sample Output 1 ------ 6 56 194 ----- explanation 1 ------ Example case 1. f(emptyset) = f({1}) = f({2}) = f({3}) = 0; f({1, 2}) = f({2, 3}) = f({3, 1}) = 1 f({1, 2, 3}) = 3. So the answer is 1 + 1 + 1 + 3 = 6. Example case 2. The nonzero f's are as follows f({1, 2}) = f({2, 3}) = f({3, 4}) = f({4, 1}) = f({2, 4}) = 1 f({1, 2, 3}) = f({1, 3, 4}) = 2 f({1, 2, 4}) = f({2, 3, 4}) = 3 f({1, 2, 3, 4}) = 5 So the answer is 5 * 12 + 2 * 22 + 2 * 32 + 52 = 56.
import sys MOD = 10**9 + 7 def trivial(edge, N, K): total = 2**N situation = [] for i in range(1, total): s = bin(i)[2:] if len(s) < N: s = "0" * (N - len(s)) + s s = "0" + s[::-1] situation += [s] ans = 0 for i in range(len(situation)): s = situation[i] temp = [] for j in range(len(s)): if s[j] != "0": temp += [j] value = 0 for ii in range(len(temp)): for jj in range(ii + 1, len(temp)): aa = temp[ii] bb = temp[jj] if (aa, bb) in edge or (bb, aa) in edge: value += 1 ans += value**K ans %= MOD return ans def ONE(edge, N): if N == 1: return 0 return pow(2, N - 2, MOD) * len(edge) % MOD def TWO(graph, edge, N, M): MOD = 10**9 + 7 degree = {} for i in range(1, N + 1): degree[i] = len(graph[i]) temp = 0 for X in edge: a, b = X temp += degree[a] + degree[b] temp %= MOD sum1 = M sum2 = (temp - 2 * M) * pow(2, MOD - 2, MOD) % MOD sum3 = (M * (M + 1) - temp) % MOD * pow(2, MOD - 2, MOD) % MOD ans = ( pow(2, N - 2, MOD) * (sum2 + sum1) % MOD + pow(2, N - 3, MOD) * sum3 % MOD ) % MOD return ans def THREE(graph, edge, N, M): MOD = 10**9 + 7 degree = {} GRAPH = {} for i in range(1, N + 1): degree[i] = len(graph[i]) GRAPH[i] = set(graph[i]) temp = 0 for X in edge: a, b = X temp += degree[a] + degree[b] temp %= MOD sum1 = M sum2 = (temp - 2 * M) * pow(2, MOD - 2, MOD) % MOD sum3 = (M * (M + 1) - temp) % MOD * pow(2, MOD - 2, MOD) % MOD total = 0 tri = 0 for X in edge: a, b = X if degree[a] >= 2 and degree[b] >= 2: total += (degree[a] - 1) * (degree[b] - 1) tri += len(GRAPH[a] & GRAPH[b]) tri %= MOD total %= MOD tri *= pow(3, MOD - 2, MOD) tri %= MOD need = (tri + total) % MOD number1 = sum2 * (M - 2) % MOD number2 = M * (M - 1) * (M - 2) // 6 % MOD ans11 = 3 * (number1 + number2 + need) % MOD % MOD ans11 += M * 8 + (12 * sum2 + 6 * sum3) * 2 ans11 %= MOD return ans11 * pow(2, N - 5, MOD) % MOD ANS = [] for _ in range(int(input())): N, M, K = list(map(int, sys.stdin.readline().strip().split(" "))) edge = set() graph = {} for i in range(1, N + 1): graph[i] = [] for ii in range(M): x, y = list(map(int, sys.stdin.readline().strip().split(" "))) graph[x] += [y] graph[y] += [x] edge.add((x, y)) if N < 16: ANS += [trivial(edge, N, K)] elif K == 1: ANS += [ONE(edge, N)] elif K == 2: ANS += [TWO(graph, edge, N, M)] else: ANS += [THREE(graph, edge, N, M)] print("\n".join(list(map(str, ANS))))
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR NUMBER VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING VAR VAR LIST VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR LIST FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR LIST FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR LIST FUNC_CALL VAR VAR VAR VAR VAR VAR LIST FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. You are given an undirected graph G = (V, E). We define a function f(s) for s ⊆ V to be the number of edges in the induced subgraph of s. The problem asks you to calculate the sum of f(s)^{k} over all s in 2^{|V|} subsets of V. As the answer could be very large, output it modulo (10^{9}+7). ------ Input ------ The first line of input contains an integer T denoting the number of test cases. For each test case, the first line contains three space-separated integers n = |V|, m = |E| and k. Then m lines follow, each line contains two space-separated integers u, v denoting an edge (u, v) is in E. ------ Output ------ For each test case, output one line containing one integer, the answer modulo (10^{9}+7). ------ Constraints ------ 1 ≤ T ≤ 100 2 ≤ n ≤ 10^{5} 0 ≤ m ≤ 10^{5} Sum of each of n, m over all test cases ≤ 3 * 10^{5} 1 ≤ u, v ≤ n. 1 ≤ k ≤ 3. The graph is simple, i.e., doesn't contain self loops and multiple edges. ------ Subtasks ------ Subtask #1 (8 points): T, n ≤ 15 Subtask #2 (7 points): k = 1 Subtask #3 (9 points): k = 2 Subtask #4 (15 points): k = 3. Sum of n over all test cases ≤ 300 Sum of m over all test cases ≤ 300 Subtask #5 (24 points): k = 3. Sum of n over all test cases ≤ 3000 Sum of m over all test cases ≤ 3000 Subtask #6 (37 points): Original Constraints ----- Sample Input 1 ------ 3 3 3 1 1 2 2 3 3 1 4 5 2 1 2 2 3 3 4 4 1 2 4 5 4 3 1 2 1 3 1 4 2 5 ----- Sample Output 1 ------ 6 56 194 ----- explanation 1 ------ Example case 1. f(emptyset) = f({1}) = f({2}) = f({3}) = 0; f({1, 2}) = f({2, 3}) = f({3, 1}) = 1 f({1, 2, 3}) = 3. So the answer is 1 + 1 + 1 + 3 = 6. Example case 2. The nonzero f's are as follows f({1, 2}) = f({2, 3}) = f({3, 4}) = f({4, 1}) = f({2, 4}) = 1 f({1, 2, 3}) = f({1, 3, 4}) = 2 f({1, 2, 4}) = f({2, 3, 4}) = 3 f({1, 2, 3, 4}) = 5 So the answer is 5 * 12 + 2 * 22 + 2 * 32 + 52 = 56.
MOD = 10**9 + 7 def pow2(n): return 0 if n < 0 else pow(2, n, MOD) def setB(): global B gs = [set(g_u) for g_u in g] es = set(e) B = [0] * (n + 1) lim = m**0.5 for u in range(1, n + 1): if deg[u] < lim: for v in g[u]: for w in g[u]: if (v, w) in es: B[u] += 1 else: for v, w in e: if v in gs[u] and w in gs[u]: B[u] += 1 def solve3(): a23, a24 = solve2(get_a23_a24=True) setB() tri = sum(B) claw = sum(deg_u * (deg_u - 1) * (deg_u - 2) for deg_u in deg) P4 = 3 * sum((deg[u] - 1) * (deg[v] - 1) for u, v in e) - 3 * tri a32 = m a33 = 3 * a23 + tri a34 = 3 * a24 + claw + P4 a35 = 3 * sum( deg[u] * (deg[u] - 1) * (m - deg[u] + 2) + B[u] - 2 * (deg[u] - 1) * sum(deg[v] for v in g[u]) for u in range(1, n + 1) ) a36 = m * m * m - a32 - a33 - a34 - a35 return ( a32 * pow2(n - 2) + a33 * pow2(n - 3) + a34 * pow2(n - 4) + a35 * pow2(n - 5) + a36 * pow2(n - 6) ) % MOD def solve2(get_a23_a24=False): a22 = m a23 = sum(deg_u * (deg_u - 1) for deg_u in deg) a24 = m * m - a22 - a23 if get_a23_a24: return a23, a24 return (a22 * pow2(n - 2) + a23 * pow2(n - 3) + a24 * pow2(n - 4)) % MOD def solve1(): return m * pow2(n - 2) % MOD T = int(input()) for t in range(T): n, m, k = map(int, input().split()) e = [] g = [[] for i in range(n + 1)] for i in range(m): u, v = map(int, input().split()) e.append((u, v)) e.append((v, u)) g[u].append(v) g[v].append(u) deg = [len(g_u) for g_u in g] ans = solve1() if k == 1 else solve2() if k == 2 else solve3() print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER NUMBER FUNC_CALL VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR RETURN VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def validTicTacToe(self, board): O, X = 0, 0 for r in board: for c in r: if c == "O": O += 1 elif c == "X": X += 1 if O - X > 0: return False if X - O > 1: return False for c in range(2): if board[0][c] == "X" and board[1][c] == "X" and board[2][c] == "X": return X - O == 1 if board[c][0] == "X" and board[c][1] == "X" and board[c][2] == "X": return X - O == 1 if board[0][0] == "X" and board[1][1] == "X" and board[2][2] == "X": return X - O == 1 if board[2][0] == "X" and board[1][1] == "X" and board[0][2] == "X": return X - O == 1 for c in range(2): if board[0][c] == "O" and board[1][c] == "O" and board[2][c] == "O": return X - O == 0 if board[c][0] == "O" and board[c][1] == "O" and board[c][2] == "O": return X - O == 0 if board[0][0] == "O" and board[1][1] == "O" and board[2][2] == "O": return X - O == 0 if board[2][0] == "O" and board[1][1] == "O" and board[0][2] == "O": return X - O == 0 return True
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING RETURN BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING VAR VAR NUMBER STRING RETURN BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING RETURN BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING RETURN BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING RETURN BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING VAR VAR NUMBER STRING RETURN BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING RETURN BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING RETURN BIN_OP VAR VAR NUMBER RETURN NUMBER
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def validTicTacToe(self, board): o_cnt = 0 x_cnt = 0 bingo_x = 0 bingo_o = 0 for i in range(3): if board[i] == "O" * 3: bingo_o += 1 if board[i] == "X" * 3: bingo_x += 1 if board[0][i] == board[1][i] == board[2][i]: if board[0][i] == "O": bingo_o += 1 if board[0][i] == "X": bingo_x += 1 for j in range(3): if board[i][j] == "O": o_cnt += 1 elif board[i][j] == "X": x_cnt += 1 if x_cnt < o_cnt: return False elif x_cnt - o_cnt > 1: return False if board[1][1] == board[0][2] == board[2][0]: if board[1][1] == "O": bingo_o += 1 elif board[1][1] == "X": bingo_x += 1 if board[0][0] == board[1][1] == board[2][2]: if board[0][0] == "O": bingo_o += 1 elif board[0][0] == "X": bingo_x += 1 if bingo_x >= 1 and bingo_o >= 1: return False if bingo_x >= 1: return x_cnt == o_cnt + 1 elif bingo_o >= 1: return x_cnt == o_cnt return True
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP STRING NUMBER VAR NUMBER IF VAR VAR BIN_OP STRING NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR VAR RETURN NUMBER
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def validTicTacToe(self, board): num_X = 0 num_O = 0 for i in range(3): for j in range(3): if board[i][j] == "X": num_X += 1 elif board[i][j] == "O": num_O += 1 if num_X < num_O or num_X > num_O + 1: return False def win(board, total_X, total_O): for i in range(3): num_X = 0 num_O = 0 for j in range(3): if board[i][j] == "X": num_X += 1 elif board[i][j] == "O": num_O += 1 if num_X == 3: return total_X > total_O if num_O == 3: return total_X == total_O for j in range(3): num_X = 0 num_O = 0 for i in range(3): if board[i][j] == "X": num_X += 1 elif board[i][j] == "O": num_O += 1 if num_X == 3: return total_X > total_O if num_O == 3: return total_X == total_O num_X = 0 num_O = 0 for i in range(3): if board[i][i] == "X": num_X += 1 elif board[i][i] == "O": num_O += 1 if num_X == 3: return total_X > total_O if num_O == 3: return total_X == total_O num_X = 0 num_O = 0 for i in range(3): if board[i][2 - i] == "X": num_X += 1 elif board[i][2 - i] == "O": num_O += 1 if num_X == 3: return total_X > total_O if num_O == 3: return total_X == total_O return True return win(board, num_X, num_O)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR NUMBER RETURN VAR VAR IF VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR NUMBER RETURN VAR VAR IF VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR NUMBER RETURN VAR VAR IF VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR STRING VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR STRING VAR NUMBER IF VAR NUMBER RETURN VAR VAR IF VAR NUMBER RETURN VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def validTicTacToe(self, board): x_count, o_count = 0, 0 for string in board: for c in string: if c == "X": x_count += 1 if c == "O": o_count += 1 if x_count - o_count >= 2 or x_count - o_count < 0: return False x_win = 0 o_win = 0 if board[0] == "XXX" or board[1] == "XXX" or board[2] == "XXX": x_win += 1 if board[0] == "OOO" or board[1] == "OOO" or board[2] == "OOO": o_win += 1 if ( board[0][0] + board[1][0] + board[2][0] == "XXX" or board[0][1] + board[1][1] + board[2][1] == "XXX" or board[0][2] + board[1][2] + board[2][2] == "XXX" ): x_win += 1 if ( board[0][0] + board[1][0] + board[2][0] == "OOO" or board[0][1] + board[1][1] + board[2][1] == "OOO" or board[0][2] + board[1][2] + board[2][2] == "OOO" ): o_win += 1 if ( board[0][0] + board[1][1] + board[2][2] == "XXX" or board[2][0] + board[1][1] + board[0][2] == "XXX" ): x_win += 1 if ( board[0][0] + board[1][1] + board[2][2] == "OOO" or board[2][0] + board[1][1] + board[0][2] == "OOO" ): o_win += 1 if x_win == 1 and o_win == 1: return False if x_win == 1 and x_count - o_count != 1 or o_win == 1 and o_count != x_count: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def validTicTacToe(self, board): targetState = tuple(board) startingState = " ", " ", " " stateStack = [startingState] visited = set() while len(stateStack) > 0: curState = stateStack.pop() if curState == targetState: return True if curState in visited: continue visited.add(curState) if ( any(r == "XXX" for r in curState) or any(r == "OOO" for r in curState) or any( "".join([curState[i][j] for i in range(3)]) == "XXX" for j in range(3) ) or any( "".join([curState[i][j] for i in range(3)]) == "OOO" for j in range(3) ) or curState[0][0] == curState[1][1] == curState[2][2] == "X" or curState[0][0] == curState[1][1] == curState[2][2] == "O" or curState[0][2] == curState[1][1] == curState[2][0] == "X" or curState[0][2] == curState[1][1] == curState[2][0] == "O" ): continue moves = set() for i, row in enumerate(curState): for j, cell in enumerate(row): if cell == " " and cell != targetState[i][j]: moves.add((i, j, targetState[i][j])) if len(moves) == 0: continue numX = sum(r.count("X") for r in curState) numO = sum(r.count("O") for r in curState) if numX == numO: curTurn = "X" else: curTurn = "O" for i, j, symbol in moves: if symbol != curTurn: continue newState = [curState[t] for t in range(3)] newState[i] = curState[i][:j] + curTurn + curState[i][j + 1 :] stateStack.append(tuple(newState)) return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING STRING STRING ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR STRING VAR VAR FUNC_CALL VAR VAR STRING VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR FUNC_CALL VAR NUMBER STRING VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR FUNC_CALL VAR NUMBER STRING VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING VAR VAR IF VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN NUMBER
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def isWin(self, board, c): for i in range(3): if board[0][i] == c and board[1][i] == c and board[2][i] == c: return True for i in range(3): if board[i][0] == c and board[i][1] == c and board[i][2] == c: return True if ( board[0][0] == c and board[1][1] == c and board[2][2] == c or board[0][2] == c and board[1][1] == c and board[2][0] == c ): return True return False def validTicTacToe(self, board): o_cnt = 0 x_cnt = 0 for i in range(3): for j in range(3): x_cnt += 1 if board[i][j] == "X" else 0 o_cnt += 1 if board[i][j] == "O" else 0 if o_cnt > x_cnt or x_cnt > o_cnt + 1: return False if ( o_cnt == x_cnt and self.isWin(board, "X") or x_cnt == o_cnt + 1 and self.isWin(board, "O") ): return False return True
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR STRING NUMBER NUMBER VAR VAR VAR VAR STRING NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR FUNC_CALL VAR VAR STRING VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR STRING RETURN NUMBER RETURN NUMBER
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def validTicTacToe(self, board): def win(board, w): for i in range(3): if board[i] == w * 3: return True for i in range(3): if board[0][i] == w and board[1][i] == w and board[2][i] == w: return True sign = True for i in range(3): if board[i][i] != w: sign = False if sign: return True sign = True for i in range(3): if board[i][2 - i] != w: sign = False if sign: return True Xnum = 0 Onum = 0 for ss in board: for s in ss: if s == "X": Xnum += 1 if s == "O": Onum += 1 if win(board, "X"): if Xnum == Onum + 1: return True else: return False if win(board, "O"): if Xnum == Onum: return True else: return False if Xnum == Onum or Xnum == Onum + 1: return True else: return False
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR STRING IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR STRING IF VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def validTicTacToe(self, board): O, X = 0, 0 for r in board: for c in r: if c == "O": O += 1 elif c == "X": X += 1 if O - X > 0: return False if X - O > 1: return False if X - O == 0: for r in board: if r == "XXX": return False for c in range(2): if board[0][c] == board[1][c] == board[2][c] == "X": return False if board[0][0] == board[1][1] == board[2][2] == "X": return False if board[2][0] == board[1][1] == board[0][2] == "X": return False else: for r in board: if r == "OOO": return False for c in range(2): if board[0][c] == board[1][c] == board[2][c] == "O": return False if board[0][0] == board[1][1] == board[2][2] == "O": return False if board[2][0] == board[1][1] == board[0][2] == "O": return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR VAR IF VAR STRING RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR STRING RETURN NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING RETURN NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING RETURN NUMBER FOR VAR VAR IF VAR STRING RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR STRING RETURN NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING RETURN NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER STRING RETURN NUMBER RETURN NUMBER
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def validTicTacToe(self, board): x = 0 o = 0 win_o = 0 win_x = 0 for i in range(3): for j in range(3): if board[i][j] == "X": x += 1 elif board[i][j] == "O": o += 1 if board[i] == "XXX": win_x += 1 elif board[i] == "OOO": win_o += 1 if board[0][i] == board[1][i] == board[2][i]: if board[0][i] == "X": win_x += 1 elif board[0][i] == "O": win_o += 1 if board[0][0] == board[1][1] == board[2][2]: if board[0][0] == "O": win_o += 1 elif board[0][0] == "X": win_x += 1 if board[2][0] == board[1][1] == board[0][2]: if board[2][0] == "O": win_o += 1 elif board[2][0] == "X": win_x += 1 if x > o + 1 or x < o or win_o > 0 and win_x > 0: return False if win_x > 0 and x == o: return False if win_o > 0 and x > o: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def validTicTacToe(self, board): valid_X, valid_O = 0, 0 cnt_X, cnt_O = 0, 0 for row in board: if row == "XXX": valid_X += 1 elif row == "OOO": valid_O += 1 cnt_X += row.count("X") cnt_O += row.count("O") a, b, c = board for col in zip(a, b, c): if col == "XXX": valid_X += 1 elif col == "OOO": valid_O += 1 if board[1][1] != " ": if board[0][0] == board[2][2] == board[1][1]: if board[1][1] == "X": valid_X += 1 else: valid_O += 1 if board[2][0] == board[0][2] == board[1][1]: if board[1][1] == "X": valid_X += 1 else: valid_O += 1 if valid_X + valid_O > 1: return False if valid_X: if cnt_X - cnt_O != 1: return False elif valid_O: if cnt_X != cnt_O: return False elif not 0 <= cnt_X - cnt_O <= 1: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR NUMBER NUMBER STRING IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR IF VAR VAR RETURN NUMBER IF NUMBER BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def playerWon(self, board, player): for i in range(3): if board[0][i] == board[1][i] == board[2][i] == player: return True for i in range(3): if board[i][0] == board[i][1] == board[i][2] == player: return True if ( board[0][0] == board[1][1] == board[2][2] == player or board[0][2] == board[1][1] == board[2][0] == player ): return True return False def validTicTacToe(self, board): count_x = sum(b.count("X") for b in board) count_o = sum(b.count("O") for b in board) if count_x - count_o > 1 or count_x - count_o < 0: return False if self.playerWon(board, "X") and count_x - count_o != 1: return False if self.playerWon(board, "O") and count_x - count_o != 0: return False return True
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR STRING BIN_OP VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR STRING BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def validTicTacToe(self, board): cross = 0 circle = 0 for i in range(3): for j in range(3): if board[i][j] == "X": cross += 1 if board[i][j] == "O": circle += 1 if cross < circle or cross - circle > 1: return False cross_winning = 0 circle_winning = 0 for i in range(3): if board[i][0] == board[i][1] == board[i][2]: if board[i][0] == "X": cross_winning += 1 elif board[i][0] == "O": circle_winning += 1 if board[0][i] == board[1][i] == board[2][i]: if board[0][i] == "X": cross_winning += 1 elif board[0][i] == "O": circle_winning += 1 if board[0][0] == board[1][1] == board[2][2]: if board[0][0] == "X": cross_winning += 1 elif board[0][0] == "O": circle_winning += 1 if board[0][2] == board[1][1] == board[2][0]: if board[0][2] == "X": cross_winning += 1 elif board[0][2] == "O": circle_winning += 1 if cross_winning > 0 and circle_winning > 0: return False if cross_winning > 0 and cross <= circle: return False if circle_winning > 0 and circle < cross: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER STRING VAR NUMBER IF VAR VAR NUMBER STRING VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER
We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.  If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses.  (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.) Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins. Return True if and only if Alice wins the game, assuming both players play optimally. Example: Input: nums = [1, 1, 2] Output: false Explanation: Alice has two choices: erase 1 or erase 2. If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose. Notes: 1 <= N <= 1000.  0 <= nums[i] <= 2^16.
class Solution: def validTicTacToe(self, board): xoDict = {"X": [], "O": [], " ": []} for i in range(0, 3): for j in range(0, 3): if board[i][j] == "X": xoDict["X"] += ([i, j],) elif board[i][j] == "O": xoDict["O"] += ([i, j],) elif board[i][j] == " ": xoDict[" "] += ([i, j],) if len(xoDict["O"]) > len(xoDict["X"]): return False if len(xoDict["X"]) - len(xoDict["O"]) > 1: return False xwin = self.check(board, "X") owin = self.check(board, "O") if xwin and owin: return False if xwin and len(xoDict["X"]) == len(xoDict["O"]): return False if owin and len(xoDict["X"]) > len(xoDict["O"]): return False return True def check(self, board, char): for i in range(0, 3): if board[i][0] == char and board[i][1] == char and board[i][2] == char: return True for i in range(0, 3): if board[0][i] == char and board[1][i] == char and board[2][i] == char: return True if board[0][0] == char and board[1][1] == char and board[2][2] == char: return True if board[0][2] == char and board[1][1] == char and board[2][0] == char: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING LIST LIST LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR STRING VAR STRING LIST VAR VAR IF VAR VAR VAR STRING VAR STRING LIST VAR VAR IF VAR VAR VAR STRING VAR STRING LIST VAR VAR IF FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING IF VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING RETURN NUMBER IF VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR RETURN NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR RETURN NUMBER RETURN NUMBER
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): n += 1 powerOf2 = 2 cnt = n // 2 while powerOf2 <= n: totalPairs = n // powerOf2 cnt += totalPairs // 2 * powerOf2 if totalPairs & 1: cnt += n % powerOf2 else: cnt += 0 powerOf2 <<= 1 return cnt
CLASS_DEF FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): start = 1 result = 0 for i in range(len(str(bin(n)))): count = n - start + 1 groups = count // start if groups % 2 == 0: result += groups // 2 * start + count % start else: result += (groups // 2 + 1) * start start *= 2 return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): res, i, ln = 0, 1, n while ln != 0: q = (n + 1) // 2**i a = (n + 1) % 2**i b = 2 ** (i - 1) res = res + q * b if a > b: res = res - b + a ln = ln // 2 i += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def powOfTwo(self, n): x = 0 while 1 << x <= n: x += 1 return x - 1 def countSetBits(self, n): if n == 0: return 0 pow = self.powOfTwo(n) val = 2 ** (pow - 1) count = val * pow + (n - 2**pow + 1) + self.countSetBits(n - 2**pow) return int(count)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): def max2(n): r = 0 while 1 << r <= n: r += 1 return r - 1 def sol(n): if n <= 1: return n x = max2(n) return x * pow(2, x - 1) + (n - pow(2, x) + 1) + sol(n - pow(2, x)) return sol(n)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR RETURN FUNC_CALL VAR VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): count = 0 i = 0 while 2**i <= n: total_pairs = n // 2 ** (i + 1) total_ones = total_pairs * 2**i remaining_ones = max(0, n % 2 ** (i + 1) - 2**i + 1) count += total_ones + remaining_ones i += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): S = 0 t = n + 1 for i in range(n.bit_length()): x2 = 1 << i x = x2 << 1 l = x2 * (t // x) s = t % x - x2 if s < 0: s = 0 S += l + s return S
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): po = 1 suma = 0 while n * 2 >= po: suma += (n + 1) // (po * 2) * po a = (n + 1) % (po * 2) if a - po > 0: suma += a - po po = po * 2 return suma
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def highestpow2(self, n): x = 0 while 1 << x <= n: x += 1 return x - 1 def countSetBits(self, n): if n == 0: return 0 x = self.highestpow2(n) pow2 = x * 2 ** (x - 1) rem = n - 2**x + 1 rest = n - 2**x ans = pow2 + rem + self.countSetBits(rest) return int(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): count = 0 i = 1 while n // i > 0: count += n // (i * 2) * i if n % (i * 2) > i: count += n % (i * 2) - i + 1 i <<= 1 return count + 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): c = 0 while True: l = len(bin(n)[2:]) c += (l - 1) * 2 ** (l - 2) n -= 2 ** (l - 1) c += n + 1 if n < 3: c += n break return c
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
def largestPowerOfTwo(num): if num == 0: return -1 k = 1 pos = 0 while k <= num: k = k * 2 pos += 1 return pos - 1 class Solution: def countSetBits(self, n): if n == 0: return 0 pos = largestPowerOfTwo(n) k = 2**pos ans = 2 ** (pos - 1) * pos + 1 + (n - k) + self.countSetBits(n - k) return int(ans)
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): count = 0 i = 1 while i <= n: p = 2 * i groups = (n + 1) // p count += groups * i count += max((n + 1) % p - i, 0) i = p return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): n += 1 count = 0 x = 2 while x // 2 < n: quotient = n // x count += quotient * x // 2 reminder = n % x if reminder > x // 2: count += reminder - x // 2 x = x * 2 return count
CLASS_DEF FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): cnt = 0 k = 1 temp = n while temp > 0: cnt += n // 2**k * 2 ** (k - 1) + max(n % 2**k + 1 - 2 ** (k - 1), 0) k += 1 temp = temp // 2 return cnt
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n: int) -> int: if n == 0: return 0 p = 0 while 2**p <= n: p += 1 p -= 1 ans = p * 2**p // 2 + (n - 2**p + 1) + self.countSetBits(n - 2**p) return ans
CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): def findPrevPower(n): pos = -1 while n != 0: n = n >> 1 pos += 1 return pos def f(n): if n < 1: return 0 if n == 1: return 1 x = findPrevPower(n) return 2**x * x // 2 + (n - 2**x + 1) + f(n - 2**x) return f(n)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): count = 0 factor = 1 while n >= factor: pairs = (n + 1) // (factor * 2) * factor ones = max(0, (n + 1) % (factor * 2) - factor) count += pairs + ones factor *= 2 return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): gul = 0 g = 1 while g <= n: g = g * 2 quotient = (n + 1) // g remainder = (n + 1) % g total = quotient * (g // 2) if remainder > g // 2: total += remainder - g // 2 gul += total return gul
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, N): set_bits = 0 while N > 0: mx_pwr_2_lt_n, mx_pwr_2_lt_n_val = 0, 1 while mx_pwr_2_lt_n_val << 1 <= N: mx_pwr_2_lt_n_val <<= 1 mx_pwr_2_lt_n += 1 set_bits += (mx_pwr_2_lt_n_val >> 1) * mx_pwr_2_lt_n + ( N - mx_pwr_2_lt_n_val + 1 ) N -= mx_pwr_2_lt_n_val return set_bits
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, d): def aux(n): if n <= 1: return n x = 0 while 1 << x <= n: x += 1 x = x - 1 return (1 << x - 1) * x + (n + 1 - (1 << x)) + aux(n - (1 << x)) return aux(d)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, N): if N <= 2: return N if N <= 4: return N + 1 powerOf2 = 2 setBits = 0 n = N while n != 0: setBits += N // powerOf2 * (powerOf2 >> 1) if N & powerOf2 - 1 > (powerOf2 >> 1) - 1: setBits += (N & powerOf2 - 1) - (powerOf2 >> 1) + 1 powerOf2 <<= 1 n >>= 1 return setBits
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): intervals = 0 if n == 1: return 1 ans = 0 power_2 = 1 i = 0 while power_2 <= n: intervals = (n + 1) // power_2 intervals_with_1 = intervals // 2 ones_in_intervals_with_1 = intervals_with_1 * power_2 extra_ones = (n + 1) % power_2 if intervals & 1 == 0: ans += ones_in_intervals_with_1 else: ans += ones_in_intervals_with_1 ans += extra_ones power_2 *= 2 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): res = 0 i = 2 while i // 2 <= n: groups = n // i one_group_size = i // 2 res += groups * one_group_size if n % i >= one_group_size: res += n % i + 1 - one_group_size i <<= 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): n = n + 1 power_of_two = 2 count = n // 2 while n >= power_of_two: pairs = n // power_of_two count = count + pairs // 2 * power_of_two if pairs & 1 != 0: count = count + n % power_of_two power_of_two = power_of_two << 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): def count(num): ans = "" while num: rem = num % 2 num = num // 2 ans = str(rem) + ans return ans bits = len(count(n)) bitpow = [1] for i in range(bits): bitpow.append(bitpow[-1] * 2) totnum = n + 1 ans = 0 for i in range(bits): zo = totnum // bitpow[i] if zo % 2 == 0: ans += zo // 2 * bitpow[i] else: ans += zo // 2 * bitpow[i] ans += totnum % bitpow[i] return ans
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR STRING WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): n = n + 1 p = 1 sum = n // 2 while pow(2, p) < n: d = n // pow(2, p) if d % 2 == 0: sum = sum + d // 2 * pow(2, p) else: sum = sum + d // 2 * pow(2, p) + n % pow(2, p) p = p + 1 return sum
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): n = n + 1 count = n // 2 powerOf2 = 2 while powerOf2 < n: pairOfOnes = n // powerOf2 count += pairOfOnes // 2 * powerOf2 if pairOfOnes & 1: extraOnes = n % powerOf2 count += extraOnes powerOf2 <<= 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). Example 1: Input: N = 4 Output: 5 Explanation: For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Example 2: Input: N = 17 Output: 35 Explanation: From numbers 1 to 17(both inclusive), the total number of set bits is 35. Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. Expected Time Complexity: O(log N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{8}
class Solution: def countSetBits(self, n): two = 2 ans = 0 N = n while n != 0: ans += int(N / two) * (two >> 1) if N & two - 1 > (two >> 1) - 1: ans += (N & two - 1) - (two >> 1) + 1 two <<= 1 n >>= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def checkBit(self, pattern, arr, n): count = 0 for i in range(0, n): if pattern & arr[i] == pattern: count += 1 return count def maxAND(self, arr, n): res = 0 for bit in range(16, -1, -1): count = self.checkBit(res | 1 << bit, arr, n) if count >= 2: res |= 1 << bit return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def maxAND(self, arr, n): res = 0 for i in range(31, -1, -1): x = res | 1 << i c = 0 for e in arr: if e & x == x: c += 1 if c > 1: res = x return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
def count(arr, pat): c = 0 for i in arr: if i & pat == pat: c += 1 return c class Solution: def maxAND(self, arr, n): ma = 0 for i in range(31, -1, -1): if count(arr, ma | 1 << i) >= 2: ma |= 1 << i return ma
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def maxAND(self, arr, n): mx_and = 0 max_bit = max(arr) a = max_bit.bit_length() for i in range(a, -1, -1): count = 0 for j in arr: pattern = mx_and | 1 << i if pattern & j == pattern: count += 1 if count >= 2: break if count >= 2: mx_and |= 1 << i return mx_and
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def maxAND(self, arr, n): ans = 0 bits = 31 while bits >= 0: past = ans | pow(2, bits) count = 0 for i in arr: if i & past == past: count += 1 if count >= 2: ans = past bits = bits - 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def maxAND(self, arr, n): el = [] i = 33 while len(el) == 0 and i >= 0: for j in arr: if 1 << i & j != 0: el.append(j) if len(el) == 1: el = [] i -= 1 if len(el) == 0: return 0 bits = [i + 1] while i >= 0: x = [] for j in el: if 1 << i & j != 0: x.append(j) if len(x) > 1: el = x bits.append(i) i -= 1 ans = 0 for i in bits: ans |= 1 << i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def countBits(self, pattern, arr): c = 0 for i in arr: if pattern & i == pattern: c += 1 return c def maxAND(self, arr, n): if n <= 1: return 0 res = 0 c = 0 for i in range(31, -1, -1): c = self.countBits(res | 1 << i, arr) if c >= 2: res |= 1 << i return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def maxAND(self, arr, n): ans = 0 for i in range(18, -1, -1): temp = [] for a in arr: if a & 1 << i: temp.append(a) if len(temp) == 2: return temp[0] & temp[1] elif len(temp) > 2: arr = temp ans += 1 << i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def maxAND(self, arr, n): bits = max(arr).bit_length() curr = 0 for i in range(bits, -1, -1): seti = 1 << i count = 0 for j in range(n): if arr[j] & curr + seti == curr + seti: count += 1 if count >= 2: curr += seti return curr
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def maxAND(self, arr, n): ans = 0 cur = 16 while cur >= 0: new = list() for i in arr: if i & 1 << cur > 0: new.append(i) if len(new) > 1: arr = new ans = ans | 1 << cur cur -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def maxAND(self, arr, n): if n == 1: return 0 def checkbit(pat): count = 0 for i in range(n): if pat & arr[i] == pat: count += 1 return count res = 0 for i in range(16, -1, -1): count = checkbit(res | 1 << i) if count >= 2: res |= 1 << i return res
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def maxAND(self, arr, n): mask = 0 def count(mask): c = 0 for num in arr: if num & mask == mask: c += 1 return c for i in range(31, -1, -1): if count(mask | 1 << i) >= 2: mask = mask | 1 << i return mask
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def maxAND(self, arr, n): x = 0 ans = 0 for i in range(17, -1, -1): c = 0 for j in range(n): if x & arr[j] == x and arr[j] & 1 << i: c += 1 if c >= 2: break if c >= 2: ans += 1 << i x += 1 << i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
def check(res, arr): count = 0 for i in range(len(arr)): if arr[i] & res == res: count += 1 return count class Solution: def maxAND(self, arr, N): ans = 0 for i in range(16, -1, -1): res = ans | 1 << i x = check(res, arr) if x > 1: ans = res return ans
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR RETURN VAR
Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 4 and it is between one-time champions Rajasthan Royals and the team lead by the Youngest Captian - Delhi Capitals. As this match is very crucial, Rahul Tewatia, Rajasthan Royals all-rounder wants to hit six sixes in an over. But he knows that he can only hit six sixes if he finds the maximum AND value that can be generated by any pair of elements from the given array. Help Tewatia to find the answer. Note: AND is a bitwise '&' operator. Example 1: Input: N = 4 arr[] = {4, 8, 12, 16} Output: 8 Explanation: Pair (8, 12) has the Maximum AND Value 8. Example 2: Input: N = 4 arr[] = {4, 8, 16, 2} Output: 0 Explanation: Maximum AND Value is 0. Your Task: You don't need to read input or print anything. Your task is to complete the function maxAND() which takes the array elements and N (size of the array) as input parameters and returns the maximum AND value generated by any pair in the array. Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ arr[i] ≤ 10^{5}
class Solution: def maxAND(self, arr, n): ans = 0 cur = 17 count = 0 while cur >= 0: count = 0 for i in arr: if i & 1 << cur > 0 and (ans == 0 or ans & i == ans): count += 1 if count > 1: ans = ans | 1 << cur cur -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): b_a = list(bin(a)) b_b = list(bin(b)) c_a = b_a.count("1") c_b = b_b.count("1") if c_b > c_a: c = c_a c_r = c_b - c_a else: c = c_b c_r = 0 s = "" b_a1 = b_a b_a1.remove("0") b_a1.remove("b") for i in b_a: if i == "1" and c > 0: s += "1" c -= 1 else: s += "0" if c_r > 0: l = list(s) c_l = l.count("0") if c_r <= c_l: c_1 = c_r c_2 = 0 else: c_1 = c_l c_2 = c_r - c_l for i in reversed(range(len(l))): if l[i] == "0" and c_1 > 0: l[i] = 1 c_1 -= 1 while c_2 > 0: l = ["1"] + l s = "" for i in range(len(l)): s += str(l[i]) return int(s, 2)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR IF VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def find(self, a): c = 0 while a > 0: if a % 2 != 0: c += 1 a //= 2 return c def minVal(self, a, b): v1 = [0] * 32 v2 = [0] * 32 bit_a = 0 bit_b = 0 ans = 0 for i in range(0, 32): if a & 1 << i > 0: bit_a += 1 if b & 1 << i > 0: bit_b += 1 if bit_a == bit_b: return a elif bit_a > bit_b: while bit_a > bit_b: a = a & a - 1 bit_a -= 1 else: while bit_b > bit_a: a = a | a + 1 bit_b -= 1 return a
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): ob = 0 while b: if b % 2 != 0: ob += 1 b >>= 1 ba, oa = 0, 0 va = a while a: if a % 2 != 0: oa += 1 ba += 1 a >>= 1 if ba <= ob: return 2**ob - 1 n = oa - ob ans = 0 ba -= 1 c = ba while va: if n == 0 and va % 2 != 0: ans += 2 ** (ba - c) elif n < 0: if va % 2 == 0: n += 1 ans += 2 ** (ba - c) elif n > 0 and va % 2 != 0: n -= 1 c -= 1 va >>= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): def ct(dig): count = 0 for i in range(33): num = 2**i if num & dig != 0: count += 1 return count bct = ct(b) act = ct(a) if act == bct: return a for i in range(33): num = 2**i if act < bct: if num & a == 0: a = a | num act += 1 elif num & a != 0: a -= num act -= 1 if act == bct: break return a
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): setBits = bin(b).count("1") ans = 0 for i in range(30, -1, -1): mask = 1 << i s = a & mask if s and setBits > 0: ans |= 1 << i setBits -= 1 i = 0 while setBits > 0: mask = 1 << i if ans & mask == 0: ans |= 1 << i setBits -= 1 i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): cnt = bin(b)[2:] cnt = cnt.count("1") a_cnt = bin(a)[2:] ans = str() for i in a_cnt: if i == "1" and cnt > 0: ans += "1" cnt -= 1 else: ans += "0" if cnt > 0: ans = [char for char in ans] ans = ans[::-1] for i in range(len(ans)): if cnt == 0: break if ans[i] == "0": cnt -= 1 ans[i] = "1" ans = ans[::-1] ans = "".join(ans) ans = int(ans, 2) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): x = bin(a).count("1") y = bin(b).count("1") if x == y: return a while x > y: a = a & a - 1 x -= 1 while y > x: a = a | a + 1 y -= 1 return a
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING IF VAR VAR RETURN VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): setBits = str(bin(b)).count("1") setBits1 = str(bin(a)).count("1") ans = 0 for i in range(32): mask = 1 << i set = a & mask if set == 0 and setBits > setBits1: ans |= mask setBits -= 1 elif set != 0 and setBits1 > setBits: setBits1 -= 1 else: ans |= set return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): a1 = bin(a)[2:] c = a1.count("1") b1 = bin(b)[2:] c1 = b1.count("1") if c1 == c: return a elif c < c1: k = c1 - c s = "" a11 = a1[::-1] for i in a11: if i == "0" and k > 0: s += "1" k -= 1 elif i == "1": s += "1" else: s += "0" while k: s += "1" k -= 1 ss = s[::-1] return int(ss, 2) else: k = 0 s = "" for i in a1: if k < c1 and i == "1": s += "1" k += 1 else: s += "0" return int(s, 2) return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR STRING VAR NUMBER IF VAR STRING VAR STRING VAR STRING WHILE VAR VAR STRING VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR STRING VAR STRING VAR NUMBER VAR STRING RETURN FUNC_CALL VAR VAR NUMBER RETURN NUMBER
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, A, B): ans, setB = 0, 0 b = B while B > 0: if B & 1: setB += 1 B >>= 1 B = b for i in range(31, -1, -1): mask = 1 << i set = A & mask if set and setB > 0: setB -= 1 ans |= mask i = 0 while setB > 0: if not ans & 1 << i: setB -= 1 ans |= 1 << i i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): c = 0 z = a while z > 0: if z & 1: c += 1 z = z >> 1 y = b co = 0 while y > 0: if y & 1: co += 1 y = y >> 1 if co == c: return a ans = 0 if co < c: r = co for i in range(31, -1, -1): if a & 1 << i: ans |= 1 << i r -= 1 if r == 0: return ans ans = a if co > c: r = co - c for i in range(32): if not a & 1 << i: ans |= 1 << i r -= 1 if r == 0: return ans return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): def countSetBits(N): count = 0 for i in range(4 * 8): if N & 1 << i: count += 1 return count x = countSetBits(a) y = countSetBits(b) diff = abs(x - y) if x == y: return a elif x > y: while diff: a = a & a - 1 diff -= 1 else: while diff: a = a | a + 1 diff -= 1 return a
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR IF VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): k = bin(b).count("1") x = 0 for i in range(29, -1, -1): if k and a & 1 << i: x |= 1 << i k -= 1 a = ~a for i in range(30): if k and a & 1 << i: x |= 1 << i k -= 1 return x
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def countBits(self, n): count = 0 while n: count += n & 1 n >>= 1 return count def minVal(self, a, b): x, B = 0, self.countBits(b) for i in range(31, -1, -1): if a & 1 << i and B: x |= 1 << i B -= 1 for i in range(32): if not x & 1 << i and B: x |= 1 << i B -= 1 return x
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): counta, countb = 0, 0 for i in bin(b): if i == "1": countb += 1 for i in bin(a): if i == "1": counta += 1 if counta == countb: return a elif counta > countb: diff = counta - countb while diff: a &= a - 1 diff -= 1 return a else: diff = countb - counta while diff: a |= a + 1 diff -= 1 return a
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): set_bits = 0 while b: set_bits += b & 1 b = b >> 1 a_copy = a a_bits = [] while a: a_bits.append(a & 1) a = a >> 1 a_bits.reverse() x = 0 n = len(a_bits) for i in range(n): if set_bits == 0: break if set_bits == n - i: x += 2 ** (n - i - 1) set_bits += -1 elif a_bits[i] == 1: x += 2 ** (n - i - 1) set_bits += -1 while set_bits > 0: x += 2**n set_bits += -1 n += 1 return x
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): def bit(n): ans = [] while n > 0: ans.append(n % 2) n = n // 2 return ans arr1 = bit(a) arr2 = bit(b) arr1 = arr1[::-1] arr2 = arr2[::-1] sm1 = sum(arr1) sm2 = sum(arr2) if sm2 == sm1: return a elif sm2 < sm1: ans = [] for i in range(len(arr1)): if sm2 == 0: ans.append(0) else: ans.append(arr1[i]) if arr1[i] == 1: sm2 = sm2 - 1 else: arr1 = arr1[::-1] sm2 = sm2 - sm1 ans = [] for i in range(len(arr1)): if arr1[i] == 0 and sm2 > 0: arr1[i] = 1 sm2 = sm2 - 1 while sm2 > 0: arr1.append(1) ans = arr1[::-1] ans1 = 0 ans = ans[::-1] for i in range(len(ans)): if ans[i] == 1: ans1 = ans1 + 2**i return ans1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): count_b = 0 while b: if b & 1: count_b += 1 b = b // 2 array = [0] * 32 for i in range(31, -1, -1): if a & 1 << i and count_b > 0: array[i] = 1 count_b -= 1 j = 0 while j < 32 and count_b > 0: if array[j] == 0: array[j] = 1 count_b -= 1 j += 1 ans = 0 for i in range(32): if array[i]: ans |= 1 << i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): count = 0 A, B = a, b count0 = 0 while a > 0: count += a & 1 a >>= 1 while b > 0: if b & 1: count0 += 1 b >>= 1 if count > count0: while count != count0: A = A & A - 1 count -= 1 elif count < count0: while count < count0: A = A | A + 1 count += 1 return A
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): cnt = 0 for i in range(32): if b & 1 << i: cnt += 1 i = 31 ans = 0 while i >= 0 and cnt > 0: if a & 1 << i: ans = ans | 1 << i cnt -= 1 i -= 1 i = 0 while i < 32 and cnt > 0: if not ans & 1 << i: ans = ans | 1 << i cnt -= 1 i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): def count(n): ans = 0 while n: if n & 1: ans += 1 n >>= 1 return ans A = count(a) B = count(b) if A == B: return a d = abs(A - B) if A > B: while d: a = a & a - 1 d -= 1 else: while d: a = a | a + 1 d -= 1 return a
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): set_bits = 0 for i in range(31): if b & 1 << i: set_bits += 1 ans = 0 for i in range(30, -1, -1): if a & 1 << i and set_bits > 0: ans |= 1 << i set_bits -= 1 i = 0 while set_bits > 0: while a & 1 << i: i += 1 ans |= 1 << i set_bits -= 1 i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def tobin(self, a): x = [] while a > 0: x.append(a % 2) a //= 2 return x def minVal(self, a, b): abin = self.tobin(a) bbin = self.tobin(b) if sum(abin) == sum(bbin): return a if sum(abin) > sum(bbin): ans = 0 cnt = 0 i = 0 while cnt < sum(abin) - sum(bbin): if abin[i]: ans += pow(2, i) cnt += 1 i += 1 return a - ans ans = 0 cnt = 0 i = 0 while cnt < sum(bbin) - sum(abin): if i >= len(abin) or abin[i] == 0: ans += pow(2, i) cnt += 1 i += 1 return ans + a
CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): if bin(a)[2:].count("1") == bin(b)[2:].count("1"): return a elif bin(a)[2:].count("1") > bin(b)[2:].count("1"): while bin(a)[2:].count("1") != bin(b)[2:].count("1"): a = a - 1 return a else: while bin(a)[2:].count("1") != bin(b)[2:].count("1"): a += 1 return a
CLASS_DEF FUNC_DEF IF FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING RETURN VAR IF FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING WHILE FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR WHILE FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): def bitLength(a): cnt = 0 i = 1 while a >= i: i *= 2 cnt += 1 return cnt def bitCount(a): cnt = 0 while a > 0: if a % 2: cnt += 1 a = int(a / 2) return cnt a = int(a) b = int(b) i = bitLength(a) j = bitCount(b) x = 0 while i >= 0 and j > 0: if a & 2**i: x = x | 2**i j -= 1 i -= 1 i = 0 while j > 0: if x & 2**i == 0: x = x | 2**i j -= 1 i += 1 return x
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): def count_setbit(n): count = 0 while n: n = n & n - 1 count = count + 1 return count def UnsetRightmostsetBit(n): return n & n - 1 def setRightmostUnsetBit(n): if n & n + 1 == 0: return n return n | n + 1 setbitB = count_setbit(b) setbitA = count_setbit(a) X = a if setbitA == setbitB: return X else: setbitdiff = setbitB - setbitA if setbitdiff > 0: while count_setbit(X) != setbitB: X = setRightmostUnsetBit(X) if setbitdiff < 0: setbitdiff = -setbitdiff while count_setbit(X) != setbitB: X = UnsetRightmostsetBit(X) return X
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a: int, b: int) -> int: numB = self.count_num_of_set_bits(b) numA = self.count_num_of_set_bits(a) diff = numA - numB while diff > 0: diff -= 1 a &= a - 1 i = 0 while diff < 0: if a & 1 << i == 0: diff += 1 a |= 1 << i i += 1 return a def count_num_of_set_bits(self, n: int) -> int: cnt = 0 while n: cnt += 1 n &= n - 1 return cnt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): ca = 0 cb = 0 k = a while k: ca += 1 k = k & k - 1 k = b while k: cb += 1 k = k & k - 1 if ca == cb: return a if ca > cb: ans = 0 for i in range(30, -1, -1): if cb and a >> i & 1: cb -= 1 ans += 1 << i return ans else: ans = a for i in range(30): if a >> i & 1 == 0: if cb > ca: cb -= 1 ans += 1 << i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, A, B): setB, setA = 0, 0 a, b = A, B while B > 0: if B & 1: setB += 1 B >>= 1 while A > 0: if A & 1: setA += 1 A >>= 1 A, B = a, b if setA == setB: return A ans = 0 for i in range(0, 32): mask = 1 << i set = A & mask if set == 0 and setB > setA: ans |= mask setB -= 1 elif set and setA > setB: setA -= 1 else: ans |= set return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): abit = bin(a)[2:] bbit = bin(b)[2:] temp = [] alen = len(abit) aco, bco = 0, 0 for i in range(alen): if abit[i] == "1": aco += 1 temp.append(i) for ele in bbit: if ele == "1": bco += 1 if bco > alen: return int("".join("1" for i in range(bco)), 2) else: ans = [0] * alen for ele in temp: if bco > 0: ans[ele] = 1 else: break bco -= 1 j = alen - 1 while bco > 0: if ans[j] != 1: ans[j] = 1 bco -= 1 j -= 1 return int("".join(str(ele) for ele in ans), 2)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL STRING STRING VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): x = bin(a)[2:] y = bin(b)[2:] if x.count("1") == y.count("1"): return a elif x.count("1") > y.count("1"): ans = "" c = y.count("1") for i in x: if i == "1" and c > 0: ans += "1" c -= 1 else: ans += "0" return int(ans, 2) else: x = "0" * (32 - len(x)) + x arr = [0] * 32 for i in range(len(x)): arr[i] = x[i] c = y.count("1") - x.count("1") for i in range(31, -1, -1): if arr[i] != "1" and c > 0: arr[i] = "1" c -= 1 return int("".join(arr), 2)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING RETURN VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): if a == b: return 0 x = bin(a)[2:] y = bin(b)[2:] c1 = x.count("1") c2 = y.count("1") r = 0 if c1 == c2: return a k = len(x) - 1 for i in range(len(x)): if c2 == 0: break if x[i] == "1": r += 1 << k c2 -= 1 k -= 1 if c2 == 0: return r else: k = 0 for i in range(len(x) - 1, -1, -1): if c2 == 0: break if x[i] == "0": r += 1 << k c2 -= 1 k += 1 n = len(x) while c2 != 0: r += 1 << n c2 -= 1 n += 1 return r
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log MAX(A,B)) Expected Auxiliary Space : O(1) Constraints : 1 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): def countBits(num): num_bits = 0 while num: num_bits += num & 1 num >>= 1 return num_bits b_bits = countBits(b) a_bits = countBits(a) diff = abs(a_bits - b_bits) if a_bits == b_bits: return a if a_bits > b_bits: while diff: a &= a - 1 diff -= 1 else: while diff: a |= a + 1 diff -= 1 return a
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR IF VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR