description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def grayCode(self, n: int) -> List[int]: if n == 0: return [0] prv_code = self.grayCode(n - 1) return prv_code + [(2 ** (n - 1) + x) for x in reversed(prv_code)] def circularPermutation(self, n: int, start: int) -> List[int]: ind = self.grayCode(n).index(start) return self.grayCode(n)[ind:] + self.grayCode(n)[:ind]
CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: return self.solve(start, n) def solve(self, num, length): if length == 1: if num == 1: return [1, 0] return [0, 1] firstnum = 1 if num < 2 ** (length - 1): firstnum = 0 res = self.solve(num - firstnum * 2 ** (length - 1), length - 1) ans = [] for i in res: ans.append(firstnum * 2 ** (length - 1) + i) res.reverse() for i in res: ans.append((1 - firstnum) * 2 ** (length - 1) + i) return ans
CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN LIST NUMBER NUMBER RETURN LIST NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR RETURN VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: array = list(map(self.binaryToGray, range(0, 2**n))) index = array.index(start) return array[index:] + array[:index] def binaryToGray(self, n: int) -> int: return n ^ n >> 1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR FUNC_DEF VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: r = [0, 1] for i in range(1, n): r += [(2**i + v) for v in r[::-1]] idx = r.index(start) return r[idx:] + r[:idx]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: k = 1 ans = [0, 1] while k <= n - 1: l = len(ans) for i in range(l - 1, -1, -1): ans.append(2**k + ans[i]) k = k + 1 while 1: if ans[0] == start: break else: ans.append(ans.pop(0)) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n, start): start = bin(start)[2:] while len(start) != n: start = "0" + start nums = [start] for i in range(n): tmp = [] for j in nums[::-1]: if j[i] == "1": tmp.append(j[:i] + "0" + j[i + 1 :]) else: tmp.append(j[:i] + "1" + j[i + 1 :]) nums += tmp return [int(i, 2) for i in nums]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: gray = [] for i in range(2**n): gray.append(i ^ i >> 1) if start == i ^ i >> 1: index = i return gray[index:] + gray[0:index]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR VAR VAR NUMBER VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: res, s = [], None for i in range(1 << n): tmp = i ^ i >> 1 res.append(i ^ i >> 1) if tmp == start: s = i res += res return res[s : s + len(res) // 2]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR LIST NONE FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: res = [(i ^ i >> 1) for i in range(1 << n)] idx = res.index(start) return res[idx:] + res[:idx]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: gc, bit = [0, 1], 1 while bit < n: bit += 1 next_vals = [] for val in reversed(gc): next_vals.append(val + 2 ** (bit - 1)) gc.extend(next_vals) ind = gc.index(start) return gc[ind:] + gc[:ind]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def helper(self, n): if n == 1: return ["0", "1"] else: res = self.helper(n - 1) return [("0" + x) for x in res] + [("1" + x) for x in res[::-1]] def circularPermutation(self, n: int, start: int) -> List[int]: res = [int(n, 2) for n in self.helper(n)] return res[res.index(start) :] + res[: res.index(start)]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP STRING VAR VAR VAR BIN_OP STRING VAR VAR VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: perms = [(2**i) for i in range(n - 1, -1, -1)] end = start ^ 1 visited = set() path = [] def dfs(node): if node == end: path.append(node) return True if node in visited: return False visited.add(node) for perm in perms: if dfs(node ^ perm): path.append(node) return True return False dfs(start) return reversed(path)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: a = ["0", "1"] for i in range(n - 1): a0 = [("0" + b) for b in a] a1 = [("1" + b) for b in a] a = a0 + a1[::-1] intperm = [int(b, 2) for b in a] idx = intperm.index(start) return intperm[idx:] + intperm[:idx]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: gray = [] for i in range(2**n): gray.append(i ^ i >> 1) idxStart = gray.index(start) result = [] for i in range(len(gray)): result.append(gray[(i + idxStart) % len(gray)]) return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: def encode_as_gray(n): return n ^ n >> 1 arr = [encode_as_gray(i) for i in range(0, 2**n)] i = arr.index(start) return arr[i:] + arr[:i]
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: answer = [start] visited = set(answer) next = start while next != None: previous = next next = None for i in range(n): newNum = previous ^ 1 << i if newNum not in visited: visited.add(newNum) answer.append(newNum) next = newNum break return answer
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: def f(n): if n == 0: return [0] else: return f(n - 1) + [(2 ** (n - 1) + v) for v in f(n - 1)[::-1]] res = f(n) i = res.index(start) return res[i:] + res[:i]
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def helper(self, n): return n ^ n >> 1 def circularPermutation(self, n: int, start: int) -> List[int]: solution = list() for i in range(0, 2**n): solution.append(self.helper(i)) index = -1 for i in range(0, len(solution)): if solution[i] == start: index = i break return solution[index:] + solution[:index]
CLASS_DEF FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that : p[0] = start p[i] and p[i+1] differ by only one bit in their binary representation. p[0] and p[2^n -1] must also differ by only one bit in their binary representation.   Example 1: Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2] Example 2: Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).   Constraints: 1 <= n <= 16 0 <= start < 2 ^ n
class Solution: def backtracking(self, result, visited, n, found): if len(result) == 2**n: diff = result[-1] ^ result[0] i = 0 is_one_bit = 0 while i < n: if 1 << i == diff: is_one_bit = 1 break i = i + 1 if is_one_bit == 1: found[0] = 1 return if found[0] == 1: return start = result[-1] for i in range(n): num = start ^ 1 << i if num in visited or num < 0 or num >= 2**n: continue result.append(num) visited[num] = 1 self.backtracking(result, visited, n, found) if found[0] == 1: return visited.pop(num) result.pop() def circularPermutation(self, n: int, start: int) -> List[int]: if n == 0: return [0] result = [start] found = [0] visited = {} visited[start] = 1 self.backtracking(result, visited, n, found) return result
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER RETURN IF VAR NUMBER NUMBER RETURN ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF VAR VAR IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). Examples: Input: N = 1, K = 1 Output: 0 Input: N = 2, K = 1 Output: 0 Input: N = 2, K = 2 Output: 1 Input: N = 4, K = 5 Output: 1 Explanation: row 1: 0 row 2: 01 row 3: 0110 row 4: 01101001 Note: N will be an integer in the range [1, 30]. K will be an integer in the range [1, 2^(N-1)].
class Solution: def maxChunksToSorted(self, arr): res = 0 temp = sorted(arr) sum1, sum2 = 0, 0 for i in range(0, len(arr)): sum1 += arr[i] sum2 += temp[i] if sum1 == sum2: res += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). Examples: Input: N = 1, K = 1 Output: 0 Input: N = 2, K = 1 Output: 0 Input: N = 2, K = 2 Output: 1 Input: N = 4, K = 5 Output: 1 Explanation: row 1: 0 row 2: 01 row 3: 0110 row 4: 01101001 Note: N will be an integer in the range [1, 30]. K will be an integer in the range [1, 2^(N-1)].
class Solution: def maxChunksToSorted(self, arr): arr_index = sorted(list(range(len(arr))), key=arr.__getitem__) ans = ma = 0 for i, x in enumerate(arr_index): ma = max(ma, x) if ma == i: ans += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). Examples: Input: N = 1, K = 1 Output: 0 Input: N = 2, K = 1 Output: 0 Input: N = 2, K = 2 Output: 1 Input: N = 4, K = 5 Output: 1 Explanation: row 1: 0 row 2: 01 row 3: 0110 row 4: 01101001 Note: N will be an integer in the range [1, 30]. K will be an integer in the range [1, 2^(N-1)].
class Solution: def maxChunksToSorted(self, arr): dict = {} sortedArr = sorted(arr) for i in range(len(sortedArr)): if sortedArr[i] not in dict: dict[sortedArr[i]] = [i] else: dict[sortedArr[i]].append(i) position = [] for i, val in enumerate(arr): position.append(dict[val][0] - i) dict[val].pop(0) maxNum = 0 i = 0 ifPlusMinus = 0 maxIdealPosition = 0 while i < len(position): if position[i] == 0 and ifPlusMinus == 0: maxNum += 1 i += 1 elif position[i] > 0 and ifPlusMinus == 0: ifPlusMinus = 1 maxIdealPosition = i + position[i] i += 1 elif position[i] >= 0 and ifPlusMinus == 1: if i + position[i] > maxIdealPosition: maxIdealPosition = i + position[i] i += 1 elif position[i] < 0 and ifPlusMinus == 1: if i < len(position) - 1: if position[i + 1] < 0: i += 1 elif position[i + 1] >= 0: if i == maxIdealPosition: maxNum += 1 ifPlusMinus = 0 maxIdealPosition = 0 i += 1 else: maxNum += 1 ifPlusMinus = 0 i += 1 return maxNum
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). Examples: Input: N = 1, K = 1 Output: 0 Input: N = 2, K = 1 Output: 0 Input: N = 2, K = 2 Output: 1 Input: N = 4, K = 5 Output: 1 Explanation: row 1: 0 row 2: 01 row 3: 0110 row 4: 01101001 Note: N will be an integer in the range [1, 30]. K will be an integer in the range [1, 2^(N-1)].
class Solution: def maxChunksToSorted(self, arr): stack = [] for num in arr: if len(stack) == 0 or stack[-1] <= num: stack.append(num) else: maxnum = stack.pop() while len(stack) > 0 and stack[-1] > num: stack.pop() stack.append(maxnum) return len(stack)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). Examples: Input: N = 1, K = 1 Output: 0 Input: N = 2, K = 1 Output: 0 Input: N = 2, K = 2 Output: 1 Input: N = 4, K = 5 Output: 1 Explanation: row 1: 0 row 2: 01 row 3: 0110 row 4: 01101001 Note: N will be an integer in the range [1, 30]. K will be an integer in the range [1, 2^(N-1)].
class Solution: def maxChunksToSorted(self, arr): mxvs, mnvs = [-1] * len(arr), [10**8 + 1] * len(arr) for i, v in enumerate(arr): if i == 0: mxvs[i] = arr[i] else: mxvs[i] = max(mxvs[i - 1], v) for i in range(len(arr))[::-1]: if i == len(arr) - 1: mnvs[i] = arr[i] else: mnvs[i] = min(mnvs[i + 1], arr[i]) cnt = 0 for i in range(len(arr)): if mxvs[i] <= (mnvs[i + 1] if i < len(arr) - 1 else 10**8 + 1): cnt += 1 return cnt
CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER RETURN VAR
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). Examples: Input: N = 1, K = 1 Output: 0 Input: N = 2, K = 1 Output: 0 Input: N = 2, K = 2 Output: 1 Input: N = 4, K = 5 Output: 1 Explanation: row 1: 0 row 2: 01 row 3: 0110 row 4: 01101001 Note: N will be an integer in the range [1, 30]. K will be an integer in the range [1, 2^(N-1)].
class Solution: def maxChunksToSorted(self, arr): if arr is None: return 0 if len(arr) < 2: return len(arr) suffix_min = [float("inf")] * len(arr) suffix_min[-1] = arr[-1] suffix_max = [float("-inf")] * len(arr) suffix_max[-1] = arr[-1] for index in range(len(arr) - 2, -1, -1): suffix_min[index] = min(arr[index + 1], suffix_min[index + 1]) suffix_max[index] = max(arr[index + 1], suffix_max[index + 1]) index = 0 chunk_count = 0 print(suffix_min) while index < len(arr): chunk_count += 1 if arr[index] > suffix_min[index]: j = index running_max = arr[j] while j < len(arr) and running_max > suffix_min[j]: running_max = max(running_max, arr[j]) j += 1 index = j + 1 else: index += 1 return chunk_count
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). Examples: Input: N = 1, K = 1 Output: 0 Input: N = 2, K = 1 Output: 0 Input: N = 2, K = 2 Output: 1 Input: N = 4, K = 5 Output: 1 Explanation: row 1: 0 row 2: 01 row 3: 0110 row 4: 01101001 Note: N will be an integer in the range [1, 30]. K will be an integer in the range [1, 2^(N-1)].
class Solution: def maxChunksToSorted(self, arr): t = arr[0] left_max = [] for x in arr: t = max(t, x) left_max.append(t) t = arr[-1] right_min = [] for i in range(len(arr) - 1, -1, -1): t = min(arr[i], t) right_min.append(t) count = 1 for i in range(0, len(arr) - 1): if left_max[i] <= right_min[len(arr) - i - 2]: count += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). Examples: Input: N = 1, K = 1 Output: 0 Input: N = 2, K = 1 Output: 0 Input: N = 2, K = 2 Output: 1 Input: N = 4, K = 5 Output: 1 Explanation: row 1: 0 row 2: 01 row 3: 0110 row 4: 01101001 Note: N will be an integer in the range [1, 30]. K will be an integer in the range [1, 2^(N-1)].
class Solution: def maxChunksToSorted(self, arr): stack = [] for e in reversed(arr): min_so_far = e while True: if len(stack) == 0 or e <= stack[-1][0]: stack.append([min_so_far, e]) break if e > stack[-1][1]: min_so_far = min(min_so_far, stack.pop()[0]) else: stack[-1][0] = min(min_so_far, stack[-1][0]) break return len(stack)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). Examples: Input: N = 1, K = 1 Output: 0 Input: N = 2, K = 1 Output: 0 Input: N = 2, K = 2 Output: 1 Input: N = 4, K = 5 Output: 1 Explanation: row 1: 0 row 2: 01 row 3: 0110 row 4: 01101001 Note: N will be an integer in the range [1, 30]. K will be an integer in the range [1, 2^(N-1)].
class Solution: def maxChunksToSorted(self, arr): sarr = sorted(arr) table = {} for i, n in enumerate(sarr): if n not in table: table[n] = [i, i] else: table[n][1] += 1 c = -1 chunks = 0 for i, n in enumerate(arr): c = max(c, table[n][0]) if table[n][0] < table[n][1]: table[n][0] += 1 if c == i: chunks += 1 return chunks
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER RETURN VAR
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example: * 2 and 4 are similar (binary representations are 10 and 100); * 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101); * 3 and 2 are not similar (binary representations are 11 and 10); * 42 and 13 are similar (binary representations are 101010 and 1101). You are given an array of n integers a_1, a_2, ..., a_n. You may choose a non-negative integer x, and then get another array of n integers b_1, b_2, ..., b_n, where b_i = a_i ⊕ x (⊕ denotes bitwise XOR). Is it possible to obtain an array b where all numbers are similar to each other? Input The first line contains one integer n (2 ≤ n ≤ 100). The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2^{30} - 1). Output If it is impossible to choose x so that all elements in the resulting array are similar to each other, print one integer -1. Otherwise, print any non-negative integer not exceeding 2^{30} - 1 that can be used as x so that all elements in the resulting array are similar. Examples Input 2 7 2 Output 1 Input 4 3 17 6 0 Output 5 Input 3 1 2 3 Output -1 Input 3 43 12 12 Output 1073709057
import sys readline = sys.stdin.readline readlines = sys.stdin.readlines ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep="\n") def solve(): n = ni() a = nl() mask = (1 << 15) - 1 ab = [(x & mask) for x in a] at = [(x >> 15) for x in a] d = dict() for bit in range(mask, -1, -1): b = [bin(bit ^ x).count("1") for x in ab] g = tuple([(x - b[0]) for x in b[1:]]) d[g] = bit for bit in range(mask + 1): b = [bin(bit ^ x).count("1") for x in at] g = tuple([(b[0] - x) for x in b[1:]]) if g in d: print(bit << 15 | d[g]) return print(-1) return solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example: * 2 and 4 are similar (binary representations are 10 and 100); * 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101); * 3 and 2 are not similar (binary representations are 11 and 10); * 42 and 13 are similar (binary representations are 101010 and 1101). You are given an array of n integers a_1, a_2, ..., a_n. You may choose a non-negative integer x, and then get another array of n integers b_1, b_2, ..., b_n, where b_i = a_i ⊕ x (⊕ denotes bitwise XOR). Is it possible to obtain an array b where all numbers are similar to each other? Input The first line contains one integer n (2 ≤ n ≤ 100). The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2^{30} - 1). Output If it is impossible to choose x so that all elements in the resulting array are similar to each other, print one integer -1. Otherwise, print any non-negative integer not exceeding 2^{30} - 1 that can be used as x so that all elements in the resulting array are similar. Examples Input 2 7 2 Output 1 Input 4 3 17 6 0 Output 5 Input 3 1 2 3 Output -1 Input 3 43 12 12 Output 1073709057
def bitcount(x): return 0 if x == 0 else x % 2 + bitcount(x // 2) def core(): _ = int(input()) A = [int(v) for v in input().split()] Hmask = 1073709056 Lmask = 32767 H = [(a >> 15) for a in A] L = [(a & Lmask) for a in A] Hcnt = {} Lcnt = {} for x in range(2**15): hkey = [bitcount(h ^ x) for h in H] shift = min(hkey) hkey = tuple(v - shift for v in hkey) Hcnt.setdefault(hkey, x) Hcnt[hkey] = min(Hcnt[hkey], x) lkey = tuple(bitcount(l ^ x) for l in L) Lcnt.setdefault(lkey, x) Lcnt[lkey] = min(Lcnt[lkey], x) for t in Lcnt: reverse_t = tuple(max(t) - v for v in t) shift = min(reverse_t) shifted_rt = tuple(shift + v for v in reverse_t) if shifted_rt in Hcnt: ans = Hcnt[shifted_rt] << 15 | Lcnt[t] return ans return -1 print(core())
FUNC_DEF RETURN VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example: * 2 and 4 are similar (binary representations are 10 and 100); * 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101); * 3 and 2 are not similar (binary representations are 11 and 10); * 42 and 13 are similar (binary representations are 101010 and 1101). You are given an array of n integers a_1, a_2, ..., a_n. You may choose a non-negative integer x, and then get another array of n integers b_1, b_2, ..., b_n, where b_i = a_i ⊕ x (⊕ denotes bitwise XOR). Is it possible to obtain an array b where all numbers are similar to each other? Input The first line contains one integer n (2 ≤ n ≤ 100). The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2^{30} - 1). Output If it is impossible to choose x so that all elements in the resulting array are similar to each other, print one integer -1. Otherwise, print any non-negative integer not exceeding 2^{30} - 1 that can be used as x so that all elements in the resulting array are similar. Examples Input 2 7 2 Output 1 Input 4 3 17 6 0 Output 5 Input 3 1 2 3 Output -1 Input 3 43 12 12 Output 1073709057
import sys def main(): import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) dic1 = {} dic2 = {} A1 = [0] * N for i, a in enumerate(A): A1[i] = a >> 15 for i in range(2**15): tmp = [0] * N for j, a in enumerate(A1): a ^= i tmp[j] = bin(a).count("1") t0 = tmp[0] for j in range(N): tmp[j] -= t0 dic1[i] = tuple(tmp) A2 = [0] * N for i, a in enumerate(A): A2[i] = a % 2**15 for i in range(2**15): tmp = [0] * N for j, a in enumerate(A2): a ^= i tmp[j] = -bin(a).count("1") t0 = tmp[0] for j in range(N): tmp[j] -= t0 dic2[tuple(tmp)] = i for i in range(2**15): if dic1[i] in dic2: print(i * 2**15 + dic2[dic1[i]]) exit() print(-1) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example: * 2 and 4 are similar (binary representations are 10 and 100); * 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101); * 3 and 2 are not similar (binary representations are 11 and 10); * 42 and 13 are similar (binary representations are 101010 and 1101). You are given an array of n integers a_1, a_2, ..., a_n. You may choose a non-negative integer x, and then get another array of n integers b_1, b_2, ..., b_n, where b_i = a_i ⊕ x (⊕ denotes bitwise XOR). Is it possible to obtain an array b where all numbers are similar to each other? Input The first line contains one integer n (2 ≤ n ≤ 100). The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2^{30} - 1). Output If it is impossible to choose x so that all elements in the resulting array are similar to each other, print one integer -1. Otherwise, print any non-negative integer not exceeding 2^{30} - 1 that can be used as x so that all elements in the resulting array are similar. Examples Input 2 7 2 Output 1 Input 4 3 17 6 0 Output 5 Input 3 1 2 3 Output -1 Input 3 43 12 12 Output 1073709057
n = int(input()) d = [int(i) for i in input().split()] l1 = [([1] * n) for i in range(15)] l2 = [([1] * n) for i in range(15)] vec_const1 = [0] * n vec_const2 = [0] * n for j in range(n): dd = d[j] for i in range(30): if dd & 1 << i: if i < 15: vec_const1[j] += 1 l1[i][j] = -1 else: vec_const2[j] += 1 l2[i - 15][j] = -1 ub = 1 << 15 dic = {} for msk in range(ub): mask = msk vec = vec_const1[:] for i in range(15): if mask & 1: for j in range(n): vec[j] += l1[i][j] mask >>= 1 dic[tuple([(vec[i + 1] - vec[i]) for i in range(n - 1)])] = msk for msk in range(ub): vec = vec_const2[:] mask = msk for i in range(15): if mask & 1: for j in range(n): vec[j] += l2[i][j] mask >>= 1 dif = tuple([(vec[i] - vec[i + 1]) for i in range(n - 1)]) if dif in dic: print((msk << 15) + dic[dif]) exit() print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example: * 2 and 4 are similar (binary representations are 10 and 100); * 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101); * 3 and 2 are not similar (binary representations are 11 and 10); * 42 and 13 are similar (binary representations are 101010 and 1101). You are given an array of n integers a_1, a_2, ..., a_n. You may choose a non-negative integer x, and then get another array of n integers b_1, b_2, ..., b_n, where b_i = a_i ⊕ x (⊕ denotes bitwise XOR). Is it possible to obtain an array b where all numbers are similar to each other? Input The first line contains one integer n (2 ≤ n ≤ 100). The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2^{30} - 1). Output If it is impossible to choose x so that all elements in the resulting array are similar to each other, print one integer -1. Otherwise, print any non-negative integer not exceeding 2^{30} - 1 that can be used as x so that all elements in the resulting array are similar. Examples Input 2 7 2 Output 1 Input 4 3 17 6 0 Output 5 Input 3 1 2 3 Output -1 Input 3 43 12 12 Output 1073709057
n = int(input()) arr = list(map(int, input().split())) msbs_achieved_by = { tuple(bin(num >> 15 ^ x).count("1") for num in arr): x for x in range(1 << 15) } def main(): for x in range(1 << 15): counts = [bin(x ^ num & (1 << 15) - 1).count("1") for num in arr] for common_count in range(max(counts), 31): other_counts = tuple(common_count - s for s in counts) found = msbs_achieved_by.get(other_counts) if found: result = found << 15 | x print(result) arr2 = [(result ^ num) for num in arr] return print(-1) main()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER STRING VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example: * 2 and 4 are similar (binary representations are 10 and 100); * 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101); * 3 and 2 are not similar (binary representations are 11 and 10); * 42 and 13 are similar (binary representations are 101010 and 1101). You are given an array of n integers a_1, a_2, ..., a_n. You may choose a non-negative integer x, and then get another array of n integers b_1, b_2, ..., b_n, where b_i = a_i ⊕ x (⊕ denotes bitwise XOR). Is it possible to obtain an array b where all numbers are similar to each other? Input The first line contains one integer n (2 ≤ n ≤ 100). The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2^{30} - 1). Output If it is impossible to choose x so that all elements in the resulting array are similar to each other, print one integer -1. Otherwise, print any non-negative integer not exceeding 2^{30} - 1 that can be used as x so that all elements in the resulting array are similar. Examples Input 2 7 2 Output 1 Input 4 3 17 6 0 Output 5 Input 3 1 2 3 Output -1 Input 3 43 12 12 Output 1073709057
H = 15 n = int(input()) a = list(map(int, input().split())) def pc(v): v = v - (v >> 1 & 1431655765) v = (v & 858993459) + (v >> 2 & 858993459) return ((v + (v >> 4) & 252645135) * 16843009 & 4294967295) >> 24 mask = (1 << H) - 1 rec = {} for x in range(1 << H): r = [pc(ai & mask ^ x) for ai in a] d = tuple(r[-1] - ri for ri in r[:-1]) rec.setdefault(d, x) ans = None mask = (1 << H) - 1 << H for x in range(1 << H): x <<= H r = [pc(ai & mask ^ x) for ai in a] y = rec.get(tuple(ri - r[-1] for ri in r[:-1]), None) if y is not None: ans = x | y break print(-1 if ans is None else ans)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER NONE IF VAR NONE ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NONE NUMBER VAR
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example: * 2 and 4 are similar (binary representations are 10 and 100); * 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101); * 3 and 2 are not similar (binary representations are 11 and 10); * 42 and 13 are similar (binary representations are 101010 and 1101). You are given an array of n integers a_1, a_2, ..., a_n. You may choose a non-negative integer x, and then get another array of n integers b_1, b_2, ..., b_n, where b_i = a_i ⊕ x (⊕ denotes bitwise XOR). Is it possible to obtain an array b where all numbers are similar to each other? Input The first line contains one integer n (2 ≤ n ≤ 100). The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2^{30} - 1). Output If it is impossible to choose x so that all elements in the resulting array are similar to each other, print one integer -1. Otherwise, print any non-negative integer not exceeding 2^{30} - 1 that can be used as x so that all elements in the resulting array are similar. Examples Input 2 7 2 Output 1 Input 4 3 17 6 0 Output 5 Input 3 1 2 3 Output -1 Input 3 43 12 12 Output 1073709057
H = 15 MASK = (1 << H) - 1 n = int(input()) a = list(map(int, input().split())) rec = {} for x in range(1 << H): r = [bin(ai & MASK ^ x).count("1") for ai in a] d = tuple(r[-1] - ri for ri in r[:-1]) rec.setdefault(d, x) ans = None for x in range(1 << H): r = [bin(ai >> H ^ x).count("1") for ai in a] y = rec.get(tuple(ri - r[-1] for ri in r[:-1]), None) if y is not None: ans = x << H | y break print(-1 if ans is None else ans)
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER NONE IF VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NONE NUMBER VAR
Chef's new friend hErd gave him two functions f and g. The function f is defined over x (x≥ 1) as: f(x) = \begin{cases} 0, & \text{if } x = 1 \\ f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} The function g is defined over x (x≥ 1) as: g(x) = \begin{cases} 1, & \text{if } x = 1 \\ 2\cdot g( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ 2\cdot g( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} where \lfloor z \rfloor, denotes the greatest integer less than or equal to z. He also gave Chef two integers L and R. Chef has to find the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The only line of each test case contains two space-separated integers L and R, as mentioned in the statement. ------ Output Format ------ For each test case, output on a new line the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ L ≤ R ≤ 10^{9}$ ----- Sample Input 1 ------ 3 1 1 1 2 1 20 ----- Sample Output 1 ------ 1 4 35 ----- explanation 1 ------ Test case $1$: $f(1)=0$ and $g(1)=1$. Hence, $f(x) + g(x) = 1$. Test case $2$: There are $2$ possible values of $x$. - $x = 1$: $f(1)+g(1)=1$ - $x = 2$: $f(2)+g(2)=(f(1) + 1) + (2\cdot g(1) + 1) = 1 + 3 = 4$. Hence the maximum value of $f(x) + g(x) = 4$.
def FF(x): return format(x, "b").count("0") def GG(x): pwr2 = x.bit_length() return p_of_2[pwr2] + p_of_2[pwr2 - 1] - x - 1 def F_G(x): pwr2 = x.bit_length() return format(x, "b").count("0") + p_of_2[pwr2] + p_of_2[pwr2 - 1] - x - 1 p_of_2 = {i: (2**i) for i in range(31)} for _ in range(int(input())): L, R = map(int, input().split()) if R == L: print(F_G(L)) continue power_of_2 = max(p_of_2[R.bit_length()] - 1, L) if p_of_2[R.bit_length() - 1] > L: print(F_G(p_of_2[R.bit_length() - 1])) continue upp_lim = L + 2 * R.bit_length() if upp_lim > R: upp_lim = R + 1 print(max(F_G(x) for x in range(L, upp_lim)))
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL FUNC_CALL VAR VAR STRING STRING VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
Chef's new friend hErd gave him two functions f and g. The function f is defined over x (x≥ 1) as: f(x) = \begin{cases} 0, & \text{if } x = 1 \\ f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} The function g is defined over x (x≥ 1) as: g(x) = \begin{cases} 1, & \text{if } x = 1 \\ 2\cdot g( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ 2\cdot g( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} where \lfloor z \rfloor, denotes the greatest integer less than or equal to z. He also gave Chef two integers L and R. Chef has to find the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The only line of each test case contains two space-separated integers L and R, as mentioned in the statement. ------ Output Format ------ For each test case, output on a new line the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ L ≤ R ≤ 10^{9}$ ----- Sample Input 1 ------ 3 1 1 1 2 1 20 ----- Sample Output 1 ------ 1 4 35 ----- explanation 1 ------ Test case $1$: $f(1)=0$ and $g(1)=1$. Hence, $f(x) + g(x) = 1$. Test case $2$: There are $2$ possible values of $x$. - $x = 1$: $f(1)+g(1)=1$ - $x = 2$: $f(2)+g(2)=(f(1) + 1) + (2\cdot g(1) + 1) = 1 + 3 = 4$. Hence the maximum value of $f(x) + g(x) = 4$.
f = lambda x: bin(x).count("0") - 1 g = lambda x: (~x & (1 << x.bit_length()) - 1) + (1 << x.bit_length() - 1) def solve(l, r): p = 1 best = None while p <= r: if p >= l: best = p p <<= 1 if best is not None: return f(best) + g(best) return max(f(k) + g(k) for k in range(l, min(l + 100, r + 1))) t = int(input()) for _ in range(t): l, r = map(int, input().split()) print(solve(l, r))
ASSIGN VAR BIN_OP FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Chef's new friend hErd gave him two functions f and g. The function f is defined over x (x≥ 1) as: f(x) = \begin{cases} 0, & \text{if } x = 1 \\ f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} The function g is defined over x (x≥ 1) as: g(x) = \begin{cases} 1, & \text{if } x = 1 \\ 2\cdot g( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ 2\cdot g( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} where \lfloor z \rfloor, denotes the greatest integer less than or equal to z. He also gave Chef two integers L and R. Chef has to find the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The only line of each test case contains two space-separated integers L and R, as mentioned in the statement. ------ Output Format ------ For each test case, output on a new line the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ L ≤ R ≤ 10^{9}$ ----- Sample Input 1 ------ 3 1 1 1 2 1 20 ----- Sample Output 1 ------ 1 4 35 ----- explanation 1 ------ Test case $1$: $f(1)=0$ and $g(1)=1$. Hence, $f(x) + g(x) = 1$. Test case $2$: There are $2$ possible values of $x$. - $x = 1$: $f(1)+g(1)=1$ - $x = 2$: $f(2)+g(2)=(f(1) + 1) + (2\cdot g(1) + 1) = 1 + 3 = 4$. Hence the maximum value of $f(x) + g(x) = 4$.
t = int(input()) while t: a, b = input().split() a, b = int(a), int(b) n = bin(b)[2:] l = len(n) x = 2 ** (l - 1) if x >= a: print(2 * x - 1 + l - 1) elif a > x: k = 2 * x - 1 n2 = bin(x)[2:] mx = 0 for j in range(a, min(a + 20, b) + 1): n1 = bin(j)[2:] c = 0 for i in range(len(n2)): if n1[i] == n2[i] == "0": c += 1 z = k - (j - x) + c if z > mx: mx = z print(mx) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Chef's new friend hErd gave him two functions f and g. The function f is defined over x (x≥ 1) as: f(x) = \begin{cases} 0, & \text{if } x = 1 \\ f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} The function g is defined over x (x≥ 1) as: g(x) = \begin{cases} 1, & \text{if } x = 1 \\ 2\cdot g( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ 2\cdot g( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} where \lfloor z \rfloor, denotes the greatest integer less than or equal to z. He also gave Chef two integers L and R. Chef has to find the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The only line of each test case contains two space-separated integers L and R, as mentioned in the statement. ------ Output Format ------ For each test case, output on a new line the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ L ≤ R ≤ 10^{9}$ ----- Sample Input 1 ------ 3 1 1 1 2 1 20 ----- Sample Output 1 ------ 1 4 35 ----- explanation 1 ------ Test case $1$: $f(1)=0$ and $g(1)=1$. Hence, $f(x) + g(x) = 1$. Test case $2$: There are $2$ possible values of $x$. - $x = 1$: $f(1)+g(1)=1$ - $x = 2$: $f(2)+g(2)=(f(1) + 1) + (2\cdot g(1) + 1) = 1 + 3 = 4$. Hence the maximum value of $f(x) + g(x) = 4$.
a = int(input()) for i in range(a): L, R = map(int, input().split()) ans = 0 x = bin(L) y = bin(R) p = len(x) - 2 q = len(y) - 2 if q > p: ans = 2**q + q - 2 elif q == p: A = [] b = min(R - L, 30) c = 2**p - 1 d = x.count("0") - 1 g_L = c - (L - 2 ** (p - 1)) f_L = d if L == R: ans = g_L + f_L else: for j in range(1, b + 1): t = bin(L + j).count("0") + g_L - j - 1 A.append(t) f = max(A) ans = max(g_L + f_L, f) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Chef's new friend hErd gave him two functions f and g. The function f is defined over x (x≥ 1) as: f(x) = \begin{cases} 0, & \text{if } x = 1 \\ f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} The function g is defined over x (x≥ 1) as: g(x) = \begin{cases} 1, & \text{if } x = 1 \\ 2\cdot g( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ 2\cdot g( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} where \lfloor z \rfloor, denotes the greatest integer less than or equal to z. He also gave Chef two integers L and R. Chef has to find the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The only line of each test case contains two space-separated integers L and R, as mentioned in the statement. ------ Output Format ------ For each test case, output on a new line the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ L ≤ R ≤ 10^{9}$ ----- Sample Input 1 ------ 3 1 1 1 2 1 20 ----- Sample Output 1 ------ 1 4 35 ----- explanation 1 ------ Test case $1$: $f(1)=0$ and $g(1)=1$. Hence, $f(x) + g(x) = 1$. Test case $2$: There are $2$ possible values of $x$. - $x = 1$: $f(1)+g(1)=1$ - $x = 2$: $f(2)+g(2)=(f(1) + 1) + (2\cdot g(1) + 1) = 1 + 3 = 4$. Hence the maximum value of $f(x) + g(x) = 4$.
def f(n): ans = 0 b = bin(n)[2:] for i in b[1:]: if i == "0": ans += 1 return ans def g(n): ans = 1 b = bin(n)[2:] for i in b[1:]: ans *= 2 if i == "0": ans += 1 return ans def solver(l, r): p = 1 << int(len(bin(r)) - 3) x = max(l, p) ans = f(x) + g(x) for i in range(30): if x + i <= r: ans = max(ans, f(x + i) + g(x + i)) return ans for t in range(int(input())): l, r = map(int, input().split()) print(solver(l, r))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER VAR NUMBER IF VAR STRING VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Chef's new friend hErd gave him two functions f and g. The function f is defined over x (x≥ 1) as: f(x) = \begin{cases} 0, & \text{if } x = 1 \\ f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} The function g is defined over x (x≥ 1) as: g(x) = \begin{cases} 1, & \text{if } x = 1 \\ 2\cdot g( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ 2\cdot g( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} where \lfloor z \rfloor, denotes the greatest integer less than or equal to z. He also gave Chef two integers L and R. Chef has to find the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The only line of each test case contains two space-separated integers L and R, as mentioned in the statement. ------ Output Format ------ For each test case, output on a new line the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ L ≤ R ≤ 10^{9}$ ----- Sample Input 1 ------ 3 1 1 1 2 1 20 ----- Sample Output 1 ------ 1 4 35 ----- explanation 1 ------ Test case $1$: $f(1)=0$ and $g(1)=1$. Hence, $f(x) + g(x) = 1$. Test case $2$: There are $2$ possible values of $x$. - $x = 1$: $f(1)+g(1)=1$ - $x = 2$: $f(2)+g(2)=(f(1) + 1) + (2\cdot g(1) + 1) = 1 + 3 = 4$. Hence the maximum value of $f(x) + g(x) = 4$.
t = int(input()) def f(x): if x == 1: return 0 elif not x & 1: return f(x // 2) + 1 else: return f((x - 1) // 2) def g(x): if x == 1: return 1 elif not x & 1: return 2 * g(x // 2) + 1 else: return 2 * g((x - 1) // 2) def go(x): return g(x) + f(x) def solve(): L, R = [int(x) for x in input().split(" ")] for i in range(32, -1, -1): if L <= 1 << i <= R: print(go(1 << i)) return best = go(L) for x in range(L + 1, min(L + 15, R + 1)): tmp = go(x) if tmp > best: best = tmp print(best) while t: solve() t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER
Chef's new friend hErd gave him two functions f and g. The function f is defined over x (x≥ 1) as: f(x) = \begin{cases} 0, & \text{if } x = 1 \\ f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} The function g is defined over x (x≥ 1) as: g(x) = \begin{cases} 1, & \text{if } x = 1 \\ 2\cdot g( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ 2\cdot g( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} where \lfloor z \rfloor, denotes the greatest integer less than or equal to z. He also gave Chef two integers L and R. Chef has to find the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The only line of each test case contains two space-separated integers L and R, as mentioned in the statement. ------ Output Format ------ For each test case, output on a new line the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ L ≤ R ≤ 10^{9}$ ----- Sample Input 1 ------ 3 1 1 1 2 1 20 ----- Sample Output 1 ------ 1 4 35 ----- explanation 1 ------ Test case $1$: $f(1)=0$ and $g(1)=1$. Hence, $f(x) + g(x) = 1$. Test case $2$: There are $2$ possible values of $x$. - $x = 1$: $f(1)+g(1)=1$ - $x = 2$: $f(2)+g(2)=(f(1) + 1) + (2\cdot g(1) + 1) = 1 + 3 = 4$. Hence the maximum value of $f(x) + g(x) = 4$.
t = int(input()) def f(n): if n == 1: return 0 elif n % 2 == 0: return f(n // 2) + 1 else: return f(n // 2) def g(n): if n == 1: return 1 elif n % 2 == 0: return 2 * g(n // 2) + 1 else: return 2 * g(n // 2) for _ in range(t): l, r = map(int, input().split()) m = 0 i = 2**29 flag = 0 while i != 1: if i <= r and i >= l: print(f(i) + g(i)) flag = 1 break i = i // 2 if flag == 0: m = 0 if r - l <= 100: for i in range(l, r + 1): m = max(m, f(i) + g(i)) print(m) else: for i in range(l, l + 15): m = max(m, f(i) + g(i)) print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER 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 ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Chef's new friend hErd gave him two functions f and g. The function f is defined over x (x≥ 1) as: f(x) = \begin{cases} 0, & \text{if } x = 1 \\ f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} The function g is defined over x (x≥ 1) as: g(x) = \begin{cases} 1, & \text{if } x = 1 \\ 2\cdot g( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ 2\cdot g( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} where \lfloor z \rfloor, denotes the greatest integer less than or equal to z. He also gave Chef two integers L and R. Chef has to find the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The only line of each test case contains two space-separated integers L and R, as mentioned in the statement. ------ Output Format ------ For each test case, output on a new line the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ L ≤ R ≤ 10^{9}$ ----- Sample Input 1 ------ 3 1 1 1 2 1 20 ----- Sample Output 1 ------ 1 4 35 ----- explanation 1 ------ Test case $1$: $f(1)=0$ and $g(1)=1$. Hence, $f(x) + g(x) = 1$. Test case $2$: There are $2$ possible values of $x$. - $x = 1$: $f(1)+g(1)=1$ - $x = 2$: $f(2)+g(2)=(f(1) + 1) + (2\cdot g(1) + 1) = 1 + 3 = 4$. Hence the maximum value of $f(x) + g(x) = 4$.
def LII(): return [int(x) for x in input().split()] def candidates(m, ln): if m == 0: return [m] rv = [] while m < 2**ln: rv.append(m) b = bin(m)[2:] i = b.rindex("1") m += 2 ** (len(b) - 1 - i) return rv def vl(x): b = bin(x)[2:] r = len(b) - 1 c = b.count("0") return c + 2 ** (r + 1) + 2**r - 1 - x for _ in range(int(input())): l, r = LII() mx = 0 i = len(bin(l)) - 3 assert 2**i <= l while 2**i <= r: l0 = max(l, 2**i) r0 = min(r, 2 ** (i + 1) - 1) for x in candidates(l0 - 2**i, i): y = x + 2**i if y <= r0 and vl(y) > mx: mx = vl(y) i += 1 print(mx)
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN LIST VAR ASSIGN VAR LIST WHILE VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR VAR WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef's new friend hErd gave him two functions f and g. The function f is defined over x (x≥ 1) as: f(x) = \begin{cases} 0, & \text{if } x = 1 \\ f( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ f( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} The function g is defined over x (x≥ 1) as: g(x) = \begin{cases} 1, & \text{if } x = 1 \\ 2\cdot g( \frac{x}{2} ) + 1, & \text{if } x \text{ is even} \\ 2\cdot g( \lfloor \frac{x}{2} \rfloor ), & \text{if } x \text{ is odd} \\ \end{cases} where \lfloor z \rfloor, denotes the greatest integer less than or equal to z. He also gave Chef two integers L and R. Chef has to find the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The only line of each test case contains two space-separated integers L and R, as mentioned in the statement. ------ Output Format ------ For each test case, output on a new line the maximum value of f(x)+g(x) for L ≤ x ≤ R. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ L ≤ R ≤ 10^{9}$ ----- Sample Input 1 ------ 3 1 1 1 2 1 20 ----- Sample Output 1 ------ 1 4 35 ----- explanation 1 ------ Test case $1$: $f(1)=0$ and $g(1)=1$. Hence, $f(x) + g(x) = 1$. Test case $2$: There are $2$ possible values of $x$. - $x = 1$: $f(1)+g(1)=1$ - $x = 2$: $f(2)+g(2)=(f(1) + 1) + (2\cdot g(1) + 1) = 1 + 3 = 4$. Hence the maximum value of $f(x) + g(x) = 4$.
def base_2(x): return bin(x)[2:] def f_plus_g(x): b = base_2(x) k = len(b) - 1 c = b.count("0") return c + 2 ** (k + 1) + 2**k - 1 - x for _ in range(int(input())): l, r = [int(x) for x in input().split()] assert l >= 1 and l <= r k = len(base_2(r)) - 1 if 2**k >= l: print(k + 2 ** (k + 1) - 1) continue x = l mx = 0 while x <= r: mx = max(mx, f_plus_g(x)) i = (x ^ x - 1).bit_length() - 1 x += 2**i print(mx)
FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): def rec(i, n, res): if i == n: return res[-1] row = [] temp = res[-1] for j in range(len(temp)): row.append("0" + str(temp[j])) for j in range(len(temp) - 1, -1, -1): row.append("1" + str(temp[j])) res.append(row) return rec(i + 1, n, res) res = [] res.append([0, 1]) return rec(1, n, res)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): N = n res = [] for i in range(2**N): s = str((i & 1 << N - 1) >> N - 1) for j in range(1, N): p = (i & 1 << N - j - 1) >> N - j - 1 q = (i & 1 << N - j) >> N - j s += str(p ^ q) res.append(s) return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): for i in range(1 << n): val = i ^ i >> 1 s = bin(val)[2:] print(s.zfill(n), end=" ") return []
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING RETURN LIST
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def gcode(self, temp1, temp2): for i in range(len(temp1)): temp1[i] = "0" + temp1[i] for j in range(len(temp2)): temp2[j] = "1" + temp2[j] return temp1, temp2 def graycode(self, n): temp1 = ["0"] temp2 = ["1"] for i in range(2, n + 1): a = temp1 + temp2 b = a[::-1] temp1, temp2 = self.gcode(a, b) return temp1 + temp2
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST STRING ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): patterns = [] i = 0 while i < 1 << n: val = i ^ i >> 1 gray = bin(val).replace("0b", "").zfill(n) patterns.append(gray) i += 1 return patterns
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR STRING STRING VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): l = [[0, 1]] for i in range(2, n + 1): modified = [("0" + str(i)) for i in l[-1]] modified_rev = [("1" + str(i)) for i in l[-1][::-1]] l.append(modified + modified_rev) return l[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR NUMBER
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): ans = [] for i in range(1 << n): val = i ^ i >> 1 string = "" while val: string += str(val % 2) val //= 2 while len(string) < n: string += "0" ans.append(string[::-1]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): ans = ["0", "1"] if n == 1: return ans for i in range(1, n): add0 = ans[:] for j in range(len(ans)): add0[j] = "0" + add0[j] add1 = ans[::-1] for j in range(len(ans)): add1[j] = "1" + add1[j] ans = add0 + add1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): arr = [] for i in range(2**n): x = bin(i).replace("0b", "").rjust(n, "0") a = x[0] for j in range(1, len(x)): a += str(int(x[j - 1]) ^ int(x[j])) arr.append(a) return arr
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR STRING STRING VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): ans = [] for i in range(1 << n): s = ["0"] * n k = 0 for j in range(n): if i & 1 << j: s[n - 1 - j] = "1" s = "".join(s) g = s[0] for k in range(1, n): if s[k] == s[k - 1]: g += "0" else: g += "1" ans.append(g) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): if n == 1: return ["0", "1"] ans = [] temp = self.graycode(n - 1) for i in range(0, len(temp)): bitString = "0" + temp[i] ans.append(bitString) for j in range(len(temp) - 1, -1, -1): bitString_ = "1" + temp[j] ans.append(bitString_) return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def gray(self, n): if n == 1: l = ["0", "1"] return l l = self.gray(n - 1) res = [] for i in l: res.append("0" + i) for i in reversed(l): res.append("1" + i) return res def graycode(self, n): l = self.gray(n) return l
CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR LIST STRING STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): a = ["0", "1"] len_a = 2 for i in range(1, n): for j in range(len_a - 1, -1, -1): a.append("1" + a[j]) for j in range(len_a): a[j] = "0" + a[j] len_a *= 2 return a
CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR VAR NUMBER RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): ans = ["0", "1"] if n == 1: return ans for i in range(2, n + 1): new_ans = [] for j in ans: new_ans.append("0" + j) for j in ans[::-1]: new_ans.append("1" + j) ans = new_ans return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): def funk(n): if n == 1: return ["0", "1"] b = funk(n - 1) c = [] for r in range(2 ** (n - 1)): c += ["0" + b[r]] for rr in range(2 ** (n - 1) - 1, -1, -1): c += ["1" + b[rr]] return c return funk(n)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR LIST BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR LIST BIN_OP STRING VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): def rec(n): if n == 1: return ["0", "1"] temp = rec(n - 1) ans = [] for i in range(len(temp)): ans.append("0" + temp[i]) for i in range(len(temp) - 1, -1, -1): ans.append("1" + temp[i]) return ans if n == 1: return ["0", "1"] ans = rec(n) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR RETURN VAR IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): arr = [] arr.append("0") arr.append("1") i = 2 j = 0 while True: if i >= 1 << n: break for j in range(i - 1, -1, -1): arr.append("1" + arr[j]) for j in range(0, i): arr[j] = "0" + arr[j] i <<= 1 return arr
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR VAR NUMBER RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): grey = [] i = 0 while i < 1 << n: val = i ^ i >> 1 s = "" j = 0 while j < n: if val & 1 << j != 0: s = "1" + s else: s = "0" + s j += 1 i += 1 grey.append(s) return grey
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): l = [] for i in range(0, pow(2, n)): l.append(bin(i ^ i >> 1)[2:].zfill(n)) return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): ans = [] for i in range(1 << n): val = i ^ i >> 1 bitrep = format(val, "016b") ans.append(bitrep[16 - n :]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): length = 1 << n output = [] for i in range(length): num = i ^ i >> 1 bin_num = bin(num)[2:].zfill(n) output.append(bin_num) return output
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): N = 2**n code = "0" * n result = [code] for i in range(1, N): for flip_bit in range(n, -1, -1): if i % 2**flip_bit == 0: flip_bit = n - flip_bit - 1 break code = ( code[:flip_bit] + ("0" if code[flip_bit] == "1" else "1") + code[flip_bit + 1 :] ) result.append(code) return result
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR STRING STRING STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): def helper(n): if n == 1: return ["0", "1"] x = helper(n - 1) res = [] for i in range(len(x)): s = x[i] res.append("0" + s) for i in range(len(x) - 1, -1, -1): s = x[i] res.append("1" + s) return res res = helper(n) return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): if n == 1: arr = [] arr.append("0") arr.append("1") return arr l = self.graycode(n - 1) ans = [] for i in range(len(l)): ans.append("0" + l[i]) for i in range(len(l) - 1, -1, -1): ans.append("1" + l[i]) return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode_gen(self, n: int): if n == 0: return ["0"] if n == 1: return ["0", "1"] arr = self.graycode_gen(n - 1) res = [] for i in arr: res.append("0" + i) for i in reversed(arr): res.append("1" + i) return res def graycode(self, n): return self.graycode_gen(n)
CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN LIST STRING IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): if n == 1: return ["0", "1"] temp = self.graycode(n - 1) size = len(temp) temp1 = [] for i in range(size): temp1.append("0" + temp[i]) for j in range(size - 1, -1, -1): temp1.append("1" + temp[j]) return temp1
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): def convert(string): list1 = [] list1[:0] = string return list1 arr = ["0" * n] for i in range(n - 1, -1, -1): arrLen = len(arr) - 1 for j in range(arrLen, -1, -1): strtoarr = convert(arr[j]) strtoarr[i] = "1" stragain = "".join(strtoarr) arr.append(stragain) return arr
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR RETURN VAR ASSIGN VAR LIST BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): res = ["0", "1"] x, y = "0", "1" for j in range(n - 1): cur = [] for i in range(len(res)): cur.append(res[i] + x) cur.append(res[i] + y) x, y = y, x res = cur return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING ASSIGN VAR VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): if n == 1: return ["0", "1"] l1 = self.graycode(n - 1) l2 = reversed(l1) modl1 = [("0" + i) for i in l1] modl2 = [("1" + i) for i in l2] return modl1 + modl2
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR VAR RETURN BIN_OP VAR VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): myList = ["0", "1"] for i in range(n - 1): for j in range(len(myList) - 1, -1, -1): myList.append(myList[j]) for k in range(len(myList) // 2): myList[k] = "0" + myList[k] for l in range(len(myList) // 2, len(myList)): myList[l] = "1" + myList[l] return myList
CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP STRING VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): g = [(x ^ x >> 1) for x in range(1 << n)] return ["{:0{n}b}".format(x, n=n) for x in g]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN FUNC_CALL STRING VAR VAR VAR VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
from itertools import permutations class Solution: def graycode(self, n): if n == 1: return ["0", "1"] temp = self.graycode(n - 1) ans = [] for i in temp: ans.append("0" + i) for i in range(len(temp) - 1, -1, -1): ans.append("1" + temp[i]) return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): if n < 1: return [] out = [] out.append(format(0, "0" + str(n) + "b")) c = int(math.pow(2, n)) for i in range(1, c): v = i ^ i >> 1 out.append(format(v, "0" + str(n) + "b")) return out
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): if n == 0: res = [] res.append("") return res prev = self.graycode(n - 1) res = [] if len(prev) == 0: res.append("0") res.append("1") else: for i in range(len(prev)): if i % 2 == 0: res.append(prev[i] + "0") res.append(prev[i] + "1") else: res.append(prev[i] + "1") res.append(prev[i] + "0") return res
CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): res = ["0", "1"] if n == 1: return res while n > 1: var = list() for ele in res: var.append("0" + ele) for ele in reversed(res): var.append("1" + ele) res = var n -= 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING IF VAR NUMBER RETURN VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def util(self, n, res): if n == 1: return ["0", "1"] vals = self.util(n - 1, res) ans = list() for i in range(len(vals)): ans.append("0" + vals[i]) vals = vals[::-1] for i in range(len(vals)): ans.append("1" + vals[i]) return ans def graycode(self, n): res = list() return self.util(n, res) return res
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): output = [] padding = "0{}b".format(n) code = "0" * n for i in range(1 << n): output.append(format(i ^ i >> 1, padding)) return output
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): res = [0, 1] if n == 1: return res else: for i in range(1, n): val = pow(2, i) cur = [] for j in range(len(res) - 1, -1, -1): cur.append(val + res[j]) res = res + cur final = [] for i in res: k = bin(i)[2:] a = n - len(k) k = "0" * a + k final.append(k) return final
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode_gen(self, n: int): if n == 0: return ["0"] if n == 1: return ["0", "1"] arr = self.graycode_gen(n - 1) res = [] L = len(arr) for i in range(L): res.append("0" + arr[i]) for i in range(L - 1, -1, -1): res.append("1" + arr[i]) return res def graycode(self, n): return self.graycode_gen(n)
CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN LIST STRING IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): if n == 1: return ["0", "1"] else: a = self.graycode(n - 1) Final = [] for i in range(len(a)): Final.append("0" + a[i]) Second = [] for i in range(len(a)): Second.append("1" + a[i]) Second = Second[::-1] Final.extend(Second) return Final
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
def bina(st, ind, res, n): if ind == n: res.append(st) return bina(st + "0", ind + 1, res, n) bina(st + "1", ind + 1, res, n) class Solution: def graycode(self, n): res = [] ans = [] bina("", 0, res, n) for i in range(len(res)): st = res[i][0] for j in range(1, len(res[i])): temp = str(int(res[i][j - 1]) ^ int(res[i][j])) st = st + temp ans.append(st) return ans
FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): def dec2bin(d): return bin(d).replace("0b", "").zfill(n) patterns = ["0" * n] for i in range(1, 2**n): b = dec2bin(i) g = b[0] for j in range(1, n): g += str(int(b[j]) ^ int(b[j - 1])) patterns.append(g) return patterns
CLASS_DEF FUNC_DEF FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR STRING STRING VAR ASSIGN VAR LIST BIN_OP STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): l = ["0", "1"] while len(l[-1]) != n: b = [] for i in l: if i.count("1") % 2 == 0: b.append(i + "0") b.append(i + "1") else: b.append(i + "1") b.append(i + "0") l = b return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING WHILE FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING ASSIGN VAR VAR RETURN VAR
Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit. A Gray code sequence must begin with 0. Example 1: Input: N = 2 Output: 00 01 11 10 Explanation: 00 and 01 differ by one bit. 01 and 11 differ by one bit. 11 and 10 also differ by one bit. Example 2: Input: N=3 Output: 000 001 011 010 110 111 101 100 Explanation: 000 and 001 differ by one bit. 001 and 011 differ by one bit. 011 and 010 differ by one bit. Similarly, every successive pattern differs by one bit. Your task: You don't need to read input or print anything. Your task is to complete the function graycode() which takes an integer N as input and returns a la list of patterns. Expected Time Complexity: O(2^{n}) Expected Auxiliary Space: O(2^{n}) Constraints : 1<=N<=16
class Solution: def graycode(self, n): ii = 1 temp = ["0", "1"] if n == 1: return temp while ii < n: tempp = [] for i in range(len(temp)): tempp.append("0" + temp[i]) for i in range(len(temp) - 1, -1, -1): tempp.append("1" + temp[i]) temp = tempp ii += 1 return tempp
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING IF VAR NUMBER RETURN VAR WHILE VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
class Solution: def subsetsWithDup(self, nums): def dfs(idx, path): subsets.append(path) for i in range(idx, len(nums)): if i > idx and nums[i] == nums[i - 1]: continue dfs(i + 1, path + [nums[i]]) nums.sort() subsets = [] dfs(0, []) return subsets
CLASS_DEF FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
class Solution: def subsetsWithDup(self, nums): def backtrack(nums, start, tmp, res): res.append(tmp[:]) for i in range(start, len(nums)): if i > start and nums[i] == nums[i - 1]: continue else: tmp.append(nums[i]) backtrack(nums, i + 1, tmp, res) del tmp[-1] res = list() backtrack(sorted(nums), 0, [], res) return res
CLASS_DEF FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER LIST VAR RETURN VAR
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
class Solution: def subsetsWithDup(self, nums): nums = sorted(nums) res = [[]] self._helpFun(nums, res, []) return res def _helpFun(self, nums, res, curr): if not nums: if curr not in res: res.append(curr) return for i in range(len(nums)): newCurr = curr + [nums[i]] if newCurr not in res: res.append(newCurr) self._helpFun(nums[i + 1 :], res, newCurr)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST EXPR FUNC_CALL VAR VAR VAR LIST RETURN VAR FUNC_DEF IF VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
class Solution: def subsetsWithDup(self, nums): def dfs(depth, start, cur): if cur not in res: res.append(cur) if depth == len(nums): return for i in range(start, len(nums)): dfs(depth + 1, i + 1, cur + [nums[i]]) nums.sort() res = [] dfs(0, 0, []) return list(res)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER LIST RETURN FUNC_CALL VAR VAR
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
class Solution: def subsetsWithDup(self, nums): result = [] res = [] self.df(nums, 0, result, res) return res def df(self, nums, idx, result, res): if idx > len(nums): return if result not in res: res.append(result) for i in range(idx, len(nums)): self.df(nums, i + 1, sorted(result + [nums[i]]), res)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
class Solution: def subsets(self, nums): if len(nums) == 0: return [[]] ret = [] for i, n in enumerate(nums): if i > 0 and n == nums[i - 1]: continue for s in self.subsets(nums[i + 1 :]): ret.append([n] + s) return [[]] + ret def subsetsWithDup(self, nums): nums.sort() return self.subsets(nums)
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR RETURN BIN_OP LIST LIST VAR FUNC_DEF EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters. Return the maximum possible length of s.   Example 1: Input: arr = ["un","iq","ue"] Output: 4 Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique". Maximum length is 4. Example 2: Input: arr = ["cha","r","act","ers"] Output: 6 Explanation: Possible solutions are "chaers" and "acters". Example 3: Input: arr = ["abcdefghijklmnopqrstuvwxyz"] Output: 26   Constraints: 1 <= arr.length <= 16 1 <= arr[i].length <= 26 arr[i] contains only lower case English letters.
class Solution: def maxLength(self, arr: List[str]) -> int: def valid(w): return all(v <= 1 for v in collections.Counter(w).values()) def dfs(i, cand): if i < 0: return 0 res = dfs(i - 1, cand) if valid(arr[i]) and all(c in cand for c in arr[i]): new_cand = { c for c in cand if c not in collections.Counter(arr[i]).keys() } res = max(res, dfs(i - 1, new_cand) + len(arr[i])) return res return dfs(len(arr) - 1, {chr(i + ord("a")) for i in range(26)})
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER VAR
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters. Return the maximum possible length of s.   Example 1: Input: arr = ["un","iq","ue"] Output: 4 Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique". Maximum length is 4. Example 2: Input: arr = ["cha","r","act","ers"] Output: 6 Explanation: Possible solutions are "chaers" and "acters". Example 3: Input: arr = ["abcdefghijklmnopqrstuvwxyz"] Output: 26   Constraints: 1 <= arr.length <= 16 1 <= arr[i].length <= 26 arr[i] contains only lower case English letters.
class Solution: def maxLength(self, arr: List[str]) -> int: if len(arr) == 1: if len(set(arr[0])) == len(arr[0]): return len(arr[0]) else: return 0 new_arr = [] for s in arr: if len(s) == len(set(s)): new_arr.append((set(s), len(s))) if not new_arr: return 0 ans = 0 def backtrack(ind, temp, temp_sum): nonlocal ans if temp_sum > ans: ans = temp_sum for i in range(ind, len(new_arr)): if not new_arr[i][0] & temp: backtrack(i + 1, temp | new_arr[i][0], temp_sum + new_arr[i][1]) else: backtrack(i + 1, temp, temp_sum) backtrack(0, set(), 0) return ans
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER RETURN VAR VAR
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters. Return the maximum possible length of s.   Example 1: Input: arr = ["un","iq","ue"] Output: 4 Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique". Maximum length is 4. Example 2: Input: arr = ["cha","r","act","ers"] Output: 6 Explanation: Possible solutions are "chaers" and "acters". Example 3: Input: arr = ["abcdefghijklmnopqrstuvwxyz"] Output: 26   Constraints: 1 <= arr.length <= 16 1 <= arr[i].length <= 26 arr[i] contains only lower case English letters.
class Solution: def maxLength(self, arr: List[str]) -> int: return self.backtrack(set(), arr, 0) def backtrack(self, curr, arr, index): if index >= len(arr): return len(curr) max_len = 0 for i in range(index, len(arr)): char_set = set(arr[i]) if curr.intersection(char_set) == set() and len(char_set) == len(arr[i]): curr = curr.union(char_set) max_len = max(max_len, self.backtrack(set(curr), arr, i + 1)) curr = curr.difference(char_set) else: max_len = max(max_len, self.backtrack(curr, arr, i + 1)) return max_len
CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters. Return the maximum possible length of s.   Example 1: Input: arr = ["un","iq","ue"] Output: 4 Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique". Maximum length is 4. Example 2: Input: arr = ["cha","r","act","ers"] Output: 6 Explanation: Possible solutions are "chaers" and "acters". Example 3: Input: arr = ["abcdefghijklmnopqrstuvwxyz"] Output: 26   Constraints: 1 <= arr.length <= 16 1 <= arr[i].length <= 26 arr[i] contains only lower case English letters.
class Solution: def maxLength(self, arr: List[str]) -> int: def dfs(start, seen): nonlocal res if start == len(arr): res = max(res, sum(len(arr[i]) for i in seen)) else: is_end = True for i in graph[start]: if all(len(arr[j].intersection(arr[i])) == 0 for j in seen): is_end = False seen.append(i) dfs(i, seen) seen.pop() if is_end: res = max(res, sum(len(arr[i]) for i in seen)) arr = [item for item in arr if len(item) == len(set(item))] arr = list(map(set, arr)) graph = collections.defaultdict(list) for i in range(len(arr)): for j in range(i + 1, len(arr)): if len(arr[i].intersection(arr[j])) == 0: graph[i].append(j) print(dict(graph)) res = 0 for i in range(len(arr)): dfs(i, [i]) return res
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR RETURN VAR VAR
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters. Return the maximum possible length of s.   Example 1: Input: arr = ["un","iq","ue"] Output: 4 Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique". Maximum length is 4. Example 2: Input: arr = ["cha","r","act","ers"] Output: 6 Explanation: Possible solutions are "chaers" and "acters". Example 3: Input: arr = ["abcdefghijklmnopqrstuvwxyz"] Output: 26   Constraints: 1 <= arr.length <= 16 1 <= arr[i].length <= 26 arr[i] contains only lower case English letters.
class Solution: def maxLength(self, arr: List[str]) -> int: arr = [w for w in arr if len(w) == len(set(w))] hm = {} res = 0 dp = {(0): 0} for i, w in enumerate(arr): bitmask = 0 for c in set(w): bit_set = 1 << ord(c) - ord("a") bitmask ^= bit_set hm[i] = bitmask for i in range(len(arr)): for p in list(dp.keys()): if not hm[i] & p: now = hm[i] ^ p dp[now] = max(dp.get(now, 0), dp[p] + len(arr[i])) return max(dp.values())
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters. Return the maximum possible length of s.   Example 1: Input: arr = ["un","iq","ue"] Output: 4 Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique". Maximum length is 4. Example 2: Input: arr = ["cha","r","act","ers"] Output: 6 Explanation: Possible solutions are "chaers" and "acters". Example 3: Input: arr = ["abcdefghijklmnopqrstuvwxyz"] Output: 26   Constraints: 1 <= arr.length <= 16 1 <= arr[i].length <= 26 arr[i] contains only lower case English letters.
class Solution: def maxLength(self, l: List[str]) -> int: selected = set() def ok(word, s) -> bool: return not any(v in s for v in word) and len(word) == len(set(word)) def backTrack(l: List[str], curr_index: int, selected: set) -> int: maxLength = 0 if curr_index != -1: for v in l[curr_index]: selected.add(v) for i in range(curr_index + 1, len(l)): if ok(l[i], selected): maxLength = max( maxLength, len(l[i]) + backTrack(l, i, selected.copy()) ) return maxLength return backTrack(l, -1, selected)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR RETURN VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR