description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: class TrieNode: def __init__(self): self.children = collections.defaultdict(TrieNode) self.word_inds = [] def add(self, x, word_ind): if x: self.children[x[0]].add(x[1:], word_ind) else: self.word_inds.append(word_ind) root = TrieNode() for ind, word in enumerate(words): root.add(sorted(set(word)), ind) counts = [] for puzzle in puzzles: nodes = [root] for letter in sorted(puzzle): new_nodes = [] for node in nodes: if letter in node.children: new_nodes.append(node.children[letter]) nodes += new_nodes count = 0 for node in nodes: for word_ind in node.word_inds: if puzzle[0] in words[word_ind]: count += 1 counts.append(count) return counts
CLASS_DEF FUNC_DEF VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: cc = collections.Counter() for w in words: cc[tuple(sorted(set(list(w))))] += 1 res = [] for pzl in puzzles: possible_sets = [set([pzl[0]])] for i in range(1, 7): possible_sets.extend([st.union([pzl[i]]) for st in possible_sets]) nvalid = sum(cc[tuple(sorted(st))] for st in possible_sets) res.append(nvalid) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: count = collections.Counter() for word in words: n = 0 for w in word: n = n | 1 << ord(w) - ord("a") count[n] += 1 result = [] for puzzle in puzzles: bfs = [1 << ord(puzzle[0]) - 97] m = 0 for p in puzzle[1:]: bfs += [(m | 1 << ord(p) - 97) for m in bfs] result.append(sum(count[m] for m in bfs)) return result
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: wdict = defaultdict(int) def getbitmask(word): mask = 0 for w in word: i = ord(w) - ord("a") mask |= 1 << i return mask op = [0] * len(puzzles) for word in words: mask = getbitmask(word) wdict[mask] += 1 for i, pz in enumerate(puzzles): mask = getbitmask(pz) fi = ord(pz[0]) - ord("a") submask = mask count = 0 while submask != 0: if submask >> fi & 1: count += wdict[submask] submask = submask - 1 & mask op[i] = count return op
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: dic = {} for word in words: cur = dic for c in word: if c not in cur: cur[c] = {} cur = cur[c] if "*" not in cur: cur["*"] = 1 else: cur["*"] += 1 res = [(0) for _ in range(len(puzzles))] def dfs(i, dic, check_head): puzzle = puzzles[i] if "*" in dic and check_head: res[i] += dic["*"] for k in dic: if k in puzzle: if puzzle[0] == k or check_head: dfs(i, dic[k], True) else: dfs(i, dic[k], False) for i in range(len(puzzles)): dfs(i, dic, False) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR IF STRING VAR ASSIGN VAR STRING NUMBER VAR STRING NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR IF STRING VAR VAR VAR VAR VAR STRING FOR VAR VAR IF VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: def generate(n): res = [0] cur = 1 while n: if n & 1 == 1: res += [(i + cur) for i in res] n >>= 1 cur <<= 1 return set(res) def to_bit(w): res = 0 for c in w: res |= 1 << ord(c) - ord("a") return res m = collections.defaultdict(dict) for w in words: visited = set() for c in w: if c in visited: continue visited.add(c) b = to_bit(w) if b not in m[c]: m[c][b] = 1 else: m[c][b] += 1 res = [] for w in puzzles: s1, count = m[w[0]], 0 for i in generate(to_bit(w)): if i in s1: count += s1[i] res.append(count) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: counter = {} for word in words: mask = self.to_int(word) if mask not in counter: counter[mask] = 0 counter[mask] += 1 output = [] for puzzle in puzzles: output.append(0) sub_strings = [""] mask = self.to_int(puzzle[1:]) for c in puzzle[1:]: for sub_string in list(sub_strings): sub_strings.append(sub_string + c) for sub_string in list(sub_strings): mask = self.to_int(puzzle[0] + sub_string) output[-1] += counter.get(mask, 0) return output def to_int(self, word): mask = 0 for ch in word: mask |= 1 << ord(ch) - 97 return mask
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: words = [frozenset(word) for word in words if len(set(word)) <= 7] counter = Counter(words) res = [] for p in puzzles: pre = (p[0],) t = set(p[1:]) tmp = 0 for i in range(len(t) + 1): for c in itertools.combinations(t, i): tmp += counter[frozenset(pre + c)] res += [tmp] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR LIST VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: count = collections.Counter(frozenset(w) for w in words) res = [] for p in puzzles: subs = [p[0]] for c in p[1:]: subs += [(s + c) for s in subs] res.append(sum(count[frozenset(s)] for s in subs)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR
With respect to a given puzzle string, a word is valid if both the following conditions are satisfied: word contains the first letter of puzzle. For each letter in word, that letter is in puzzle. For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle). Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].   Example : Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] Output: [1,1,3,2,4,0] Explanation: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.   Constraints: 1 <= words.length <= 10^5 4 <= words[i].length <= 50 1 <= puzzles.length <= 10^4 puzzles[i].length == 7 words[i][j], puzzles[i][j] are English lowercase letters. Each puzzles[i] doesn't contain repeated characters.
class Solution: def conv(self, c): return ord(c) - ord("a") def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: first_letters = {chr(c): set([]) for c in range(ord("a"), ord("z") + 1)} word_masks = {} for i, w in enumerate(words): mask = 0 for c in w: first_letters[c].add(i) mask = 1 << self.conv(c) | mask if mask not in word_masks: word_masks[mask] = [] word_masks[mask].append(i) puzzle_masks = [] for p in puzzles: mask = 0 for c in p: mask = 1 << self.conv(c) | mask puzzle_masks.append(mask) answer = [] for i, m in enumerate(puzzle_masks): s = m total = 0 while s != 0: if s in word_masks: for w in word_masks[s]: if w in first_letters[puzzles[i][0]]: total += 1 s = s - 1 & m answer.append(total) return answer
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): for i in range(1, n): x = bin(i)[2:].count("1") if i + x == n: return 0 return 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): low = 0 high = n for i in range(n, 0, -1): count = 0 mask = 1 num = i while num: if num & mask == 1: count += 1 num >>= 1 if i + count == n: return 0 else: return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): for i in range(n): cb = bin(i).count("1") if i + cb == n: return 0 return 1 if __name__ == "__main__": T = int(input()) for i in range(T): n = int(input()) ob = Solution() ans = ob.is_bleak(n) print(ans)
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def setBits(self, N): cnt = 0 while N != 0: N = N & N - 1 cnt += 1 return cnt def is_bleak(self, n): for i in range(1, n): b = self.setBits(i) + i if b == n: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): dp = [0] * (n + 1) for i in range(1, n): if i % 2: dp[i] = dp[i - 1] + 1 else: dp[i] = dp[i // 2] if i + dp[i] == n: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): m = len(bin(n)[2:]) for i in range(1, m + 1): if sum(item == "1" for item in bin(n - i)[2:]) == i: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): for _ in range(n): flag = 0 if n > 14: s = n - 14 else: s = 1 for i in range(s, n): if i + bin(i)[2:].count("1") == n: flag = 1 break if flag: return 0 else: return 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def setbit(self, i): c = 0 while i: i = i & i - 1 c += 1 return c def is_bleak(self, n): for j in range(n - 1, 0, -1): a = self.setbit(j) if n - a == j: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): a = 1 i = 1 while a < n: a *= 2 i += 1 for x in range(n - i, n): if x + self.countSetBits(x) == n: return 0 return 1 def countSetBits(self, x): count = 0 while x > 0: x &= x - 1 count += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): def countSetbit(n): count = 0 while n: count += n & 1 n >>= 1 return count a = n - 17 m = 1 for i in range(a, n): if i > 0 and i + countSetbit(i) == n: m = 0 break return m
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def count_set(self, x): count = 0 while x: count += x & 1 x >>= 1 return count def is_bleak(self, n): for i in range(n, 0, -1): if i + self.count_set(i) == n: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def set_bits(self, n): cnt = 0 while n: if n & 1: cnt += 1 n = n >> 1 return cnt def log2(self, n): cnt = 0 n = n - 1 while n: n = n >> 1 cnt += 1 return cnt def is_bleak(self, n): x = n - self.log2(n) for i in range(x, n + 1): if i + self.set_bits(i) == n: return 0 return 1 if __name__ == "__main__": T = int(input()) for i in range(T): n = int(input()) ob = Solution() ans = ob.is_bleak(n) print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def check(self, x): cnt = 0 while x: x = x & x - 1 cnt += 1 return cnt def is_bleak(self, n): if n == 1: return 1 ans = 1 for i in range(1, n): if i + self.check(i) == n: ans = 0 break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): z = n c = 0 while z > 0: z = z // 2 c = c + 1 mina = n - c for i in range(n - c, n): z = i sb = 0 while z > 0: a = z % 2 z = z // 2 if a == 1: sb = sb + 1 if sb + i == n: return 0 return 1 if __name__ == "__main__": T = int(input()) for i in range(T): n = int(input()) ob = Solution() ans = ob.is_bleak(n) print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): z = n c = 0 while z > 0: z = z // 2 c = c + 1 mina = n - c for i in range(n - c, n): z = i sb = 0 while z > 0: a = z % 2 z = z // 2 if a == 1: sb = sb + 1 if sb + i == n: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): def countSetBits(i): a = "{0:b}".format(i) return a.count("1") for i in range(0, n): if i + countSetBits(i) == n: return 0 return 1 if __name__ == "__main__": T = int(input()) for i in range(T): n = int(input()) ob = Solution() ans = ob.is_bleak(n) print(ans)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: num_bits = 32 def count_set_bits(self, x): set_bits = 0 while x: set_bits += 1 x = x & x - 1 return set_bits def is_bleak(self, n): x = max(1, n - self.num_bits) while x < n: if x + self.count_set_bits(x) == n: return 0 x += 1 return 1
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): def f(a): cnt = 0 while a > 0: cnt += 1 a = a & a - 1 return cnt for i in range(n - 32, n): if f(i) + i == n: return 0 return 1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
def count_set_bits(n): ret = 0 while n: if n & 1: ret += 1 n >>= 1 return ret class Solution: def is_bleak(self, n): a = 0 for j in range(14, -1, -1): if n > j: a = j break for m in range(n - a, n): if count_set_bits(m) + m == n: return 0 return 1
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): for i in range(1, n + 1): x = i z = bin(i).replace("0b", "") x1 = z.count("1") if int(x1) + x == n: return 0 else: return 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
def csb(n): b = bin(n).replace("b", "") return b.count("1") class Solution: def is_bleak(self, n): if n == 1: return 1 for i in range(n): if i + csb(i) == n: return 0 return 1
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING RETURN FUNC_CALL VAR STRING CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): def findBits(x): cnt = 0 while x: x = x & x - 1 cnt += 1 return cnt for i in range(1, n + 1): if i + findBits(i) == n: return 0 return 1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): l, r = 1, n for i in range(1, n + 1): if i + self.countSetBits(i) == n: return 0 return 1 def countSetBits(self, num): cnt = 0 while num != 0: num = num & num - 1 cnt += 1 return cnt
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def countSetBits(self, x): count = 0 while x: x = x & x - 1 count = count + 1 return count def ceilLog2(self, x): count = 0 x = x - 1 while x > 0: x = x >> 1 count = count + 1 return count def is_bleak(self, n): for x in range(n - self.ceilLog2(n), n): if x + self.countSetBits(x) == n: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: d = {} for i in range(10**4): temp = i count = 0 ans = 0 while temp: temp = temp & temp - 1 count += 1 ans = i + count d[ans] = i def is_bleak(self, n): if n in self.d: return 0 return 1
CLASS_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): if n == 1: return 1 else: for i in range(n - 1, 0, -1): t = i count = 0 while t > 0: t = t & t - 1 count = count + 1 if count + i == n: return 0 return 1
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def set_bits(self, n): bin_num = bin(n).replace("Ob", "") set_bits = str(bin_num).count("1") return set_bits def is_bleak(self, n): for i in range(n): if i + self.set_bits(i) == n: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def total_bits(self, m): res = 0 while m: res += 1 m //= 2 return res def set_bits(self, m): res = 0 while m: res += m % 2 m //= 2 return res def is_bleak(self, n): for num in range(max(n - self.total_bits(n), 1), n): if num + self.set_bits(num) == n: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: table = [(-1) for i in range(10**4 + 1)] def countSetBits(self, n): if self.table[n] != -1: return self.table[n] set_bits = 0 while n: if n & 1: set_bits += 1 n = n >> 1 self.table[n] = set_bits return set_bits def is_bleak(self, n): for i in range(n - 1, 0, -1): set_bits = 0 if self.table[i] != -1: set_bits = self.table[i] else: y = i while y: if y & 1: set_bits += 1 y = y >> 1 self.table[y] = set_bits if i + set_bits == n: return 0 return 1
CLASS_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
def countSetBits(num): count = 0 while num > 0: mask = num - 1 num &= mask count += 1 return count class Solution: def is_bleak(self, n): for ele in range(n): if ele + countSetBits(ele) == n: return 0 else: return 1
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): for i in range(n - 1, 0, -1): c = 0 t = i while t > 0: t = t & t - 1 c += 1 x1 = c if n == x1 + i: return 0 return 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): res = 0 num = 1 while num < n: res = 0 binary = str(bin(num)[2:]) ones = binary.count("1") res = num + ones if res == n: return 0 num += 1 return 1 if __name__ == "__main__": T = int(input()) for i in range(T): n = int(input()) ob = Solution() ans = ob.is_bleak(n) print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): flag = 1 for i in range(n, 0, -1): b = self.setbit(i) + i if b == n: flag = 0 break return flag def setbit(self, n): count = 0 while n > 0: if n & 1: count += 1 n = n >> 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): c = 1 for i in range(n): if i + bin(i).count("1") == n: c = 0 break return c
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR ASSIGN VAR NUMBER RETURN VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def countSetBits(self, x): k = bin(x).replace("b", "") return k.count("1") def is_bleak(self, n): for x in range(n): if self.countSetBits(x) + x == n: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING RETURN FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): dp = [0] * (10**4 + 1) dp[1] = 1 dp[2] = 1 dp[3] = 2 for i in range(n): dp[i] = dp[i // 2] if i % 2: dp[i] += 1 for i in range(n): if i + dp[i] == n: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def sol(self, num): ans = 0 while num: num = num & num - 1 ans = ans + 1 return ans def is_bleak(self, n): for i in range(1, n): if i + self.sol(i) == n: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
def setb(n): return bin(n).count("1") class Solution: def is_bleak(self, n): for i in range(1, n): if i + setb(i) == n: return 0 return 1
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): l = n // 2 r = n ans = 1 while r: mid_bits = setBits(r) if n == r + mid_bits: ans = 0 break r -= 1 return ans def setBits(n): count = 0 while n: n = n & n - 1 count += 1 return count if __name__ == "__main__": T = int(input()) for i in range(T): n = int(input()) ob = Solution() ans = ob.is_bleak(n) print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): for i in range(n - 32, n, 1): if i > 0 and n - i > 0 and bin(i).count("1") == n - i: return 0 return 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR VAR STRING BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): for i in range(n - 1, 0, -1): if i + self.count(i) == n: return 0 return 1 def count(self, n): cont = 0 while n: cont += n & 1 n >>= 1 return cont
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given an integer, check whether it is Bleak or not. A number n is called Bleak if it can be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. Example 1: Input: 4 Output: 1 Explanation: There is no any possible x such that x + countSetbit(x) = 4 Example 2: Input: 3 Output: 0 Explanation: 3 is a Bleak number as 2 + countSetBit(2) = 3. Your Task: You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. Expected Time Complexity: O(log(n) * log(n)) Expected Space Complexity: O(1) Constraints: 1 <= n <= 10^{4}
class Solution: def is_bleak(self, n): for d in range(n // 2, n): count = 0 x = d while d > 0: if d & (d & -d) > 0: count += 1 d = d - (d & -d) if x + count == n: return 0 return 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class TrieNode: def __init__(self): self.child = [0] * 2 class Trie: def __init__(self): self.root = TrieNode() def insert(self, num): curNode = self.root for i in range(31, -1, -1): setBit = 0 if 1 << i & num: setBit = 1 if curNode.child[setBit] == 0: curNode.child[setBit] = TrieNode() curNode = curNode.child[setBit] def maxXor(self, arr, n): ans = 0 for num in arr: maxi = 0 curNode = self.root for i in range(31, -1, -1): bit = 1 if 1 << i & num: bit = 0 if curNode.child[bit] == 0: curNode = curNode.child[bit ^ 1] maxi |= (bit ^ 1) << i else: curNode = curNode.child[bit] maxi |= bit << i ans = max(ans, num ^ maxi) return ans class Solution: def max_xor(self, arr, n): trie = Trie() for num in arr: trie.insert(num) return trie.maxXor(arr, n)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class TrieNode: def __init__(self): self.children = {} class Trie: def __init__(self): self.root = TrieNode() def insert(self, n): temp = self.root i = 31 while i >= 0: bit = n >> i & 1 if bit not in temp.children: temp.children[bit] = TrieNode() temp = temp.children[bit] i -= 1 def max_xor_helper(self, value): temp = self.root current_ans = 0 i = 31 while i >= 0: bit = value >> i & 1 if bit ^ 1 in temp.children: temp = temp.children[bit ^ 1] current_ans += 1 << i else: temp = temp.children[bit] i -= 1 return current_ans class Solution: def max_xor(self, arr, n): trie = Trie() max_val = 0 trie.insert(arr[0]) for num in arr[1:]: max_val = max(trie.max_xor_helper(num), max_val) trie.insert(num) return max_val
CLASS_DEF FUNC_DEF ASSIGN VAR DICT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Trie: def __init__(self): self.left = None self.right = None def insert(n, l): head = None for i in range(n): val = l[i] current_node = head for j in range(31, -1, -1): present_bit = val >> j & 1 node = Trie() if head is None: head = node current_node = node node1 = Trie() if present_bit == 0: current_node.left = node1 else: current_node.right = node1 current_node = node1 elif present_bit == 0: if current_node.left: current_node = current_node.left else: current_node.left = node current_node = node elif current_node.right: current_node = current_node.right else: current_node.right = node current_node = node return head def getMaxXor(n, l, head): ans = 0 for i in range(n): temp_ans = 0 current_node = head for j in range(31, -1, -1): present_bit = l[i] >> j & 1 if present_bit == 1: if current_node.left: current_node = current_node.left temp_ans += 2**j else: current_node = current_node.right elif current_node.right: current_node = current_node.right temp_ans += 2**j else: current_node = current_node.left if temp_ans > ans: ans = temp_ans return ans class Solution: def max_xor(self, arr, n): head = insert(n, arr) return getMaxXor(n, arr, head)
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER IF VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class node: def __init__(self): self.o = None self.z = None self.v = 0 class tri: def __init__(self): self.r = node() def ins(self, a): p = self.r for i in range(31, -1, -1): x = a & 1 << i if x: if p.o: p = p.o else: p.o = node() p = p.o elif p.z: p = p.z else: p.z = node() p = p.z p.v = a def iss(self, a): p = self.r for i in range(31, -1, -1): x = a & 1 << i if x == 0: if p.o: p = p.o else: p = p.z elif p.z: p = p.z else: p = p.o return p.v ^ a class Solution: def max_xor(self, arr, n): t = tri() ans = 0 for i in arr: t.ins(i) for i in arr: ans = max(ans, t.iss(i)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Solution: def max_xor(self, arr, n): xor = 0 ma = 0 s = set() for i in range(30, -1, -1): xor |= 1 << i amax = ma | 1 << i for j in range(n): s.add(arr[j] & xor) for j in s: if j ^ amax in s: ma = amax break s.clear() return ma
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Solution: def max_xor(self, nums, n): m, mask = 0, 0 for i in range(32)[::-1]: mask |= 1 << i prefixes = {(n & mask) for n in nums} tmp = m | 1 << i if any(prefix ^ tmp in prefixes for prefix in prefixes): m = tmp return m
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Trie: def __init__(self): self.root = {} def put(self, num): node = self.root for i in range(30, -1, -1): bit = num >> i & 1 if not bit in node: node[bit] = {} node = node[bit] def get(self, num): node = self.root ans = 0 for i in range(30, -1, -1): bit = num >> i & 1 op_bit = 1 - bit if op_bit in node: node = node[op_bit] ans |= 1 << i else: node = node[bit] return ans class Solution: def max_xor(self, arr, n): t = Trie() t.put(arr[0]) ans = 0 for i in range(1, len(arr)): ans = max(ans, t.get(arr[i])) t.put(arr[i]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class TrieNode: def __init__(self): self.bits = [None, None] def ContainsKey(self, bit): return self.bits[bit] != None def Get(self, bit): return self.bits[bit] def PutKey(self, bit, node): self.bits[bit] = node class Trie: def __init__(self): self.node = TrieNode() def insert(self, num): temp = self.node for i in range(31, -1, -1): bit = num >> i & 1 if not temp.ContainsKey(bit): temp.PutKey(bit, Trie()) temp = temp.Get(bit).node def Get_Max(self, num): temp = self.node M = 0 for i in range(31, -1, -1): bit = num >> i & 1 if not temp.ContainsKey(1 ^ bit): temp = temp.Get(bit).node else: M |= 1 << i temp = temp.Get(1 ^ bit).node return M class Solution: def max_xor(self, arr, n): trie = Trie() for a in arr: trie.insert(a) M = 0 for b in arr: M = max(M, trie.Get_Max(b)) return M
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NONE NONE FUNC_DEF RETURN VAR VAR NONE FUNC_DEF RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Solution: def max_xor(self, nums, n): nums.sort(reverse=True) m = len(str(bin(max(nums)))) - 2 dup_m = m res = 0 bit = 1 << m - 1 for i in range(0, m): dup_m, dic, temp = dup_m - 1, {}, [] temp = [] for i in range(0, n): if nums[i] & bit in dic: dic[nums[i] & bit] += 1 else: dic[nums[i] & bit] = 1 temp.append(nums[i] & bit) k = len(temp) for i in range(0, k): if bit ^ temp[i] in dic: if bit ^ temp[i] == temp[i] and dic[bit ^ temp[i]] < 2: continue res = bit break else: bit ^= 1 << dup_m if dup_m - 1 >= 0: bit ^= 1 << dup_m - 1 return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER DICT LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class TrieNode: def __init__(self): self.zero = None self.one = None class Solution: def max_xor(self, arr, n): max_res = float("-inf") root = TrieNode() for ele in arr: self.insertNode(ele, root) for ele in arr: max_res = max(max_res, self.findMax(ele, root)) return max_res def insertNode(self, data, root): i = 31 while i >= 0: if data >> i & 1: if not root.one: root.one = TrieNode() root = root.one else: if not root.zero: root.zero = TrieNode() root = root.zero i -= 1 def findMax(self, data, root): res = 0 i = 31 while i >= 0: if data >> i & 1: if root.zero: res |= 1 << i root = root.zero else: root = root.one elif root.one: res |= 1 << i root = root.one else: root = root.zero i -= 1 return res if __name__ == "__main__": T = int(input()) for i in range(T): n = int(input()) arr = list(map(int, input().split())) ob = Solution() print(ob.max_xor(arr, n))
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Node: def __init__(self, val=None): self.val = val self.child = [None, None] class Trie: def __init__(self): self.head = Node() def insert(self, s): def solve(head, s): if len(s) == 0: return if head.child[int(s[0])] != None: child = head.child[int(s[0])] else: child = Node(int(s[0])) head.child[int(s[0])] = child solve(child, s[1:]) solve(self.head, s) def find(self, s): ans = [] def solve2(head, s): if len(s) == 0: return if head.child[1 - int(s[0])] is not None: ans.append("1") child = head.child[1 - int(s[0])] else: ans.append("0") child = head.child[int(s[0])] solve2(child, s[1:]) solve2(self.head, s) return int("".join(ans), 2) class Solution: def max_xor(self, arr, n): t = Trie() fill = len(bin(max(arr))) for i in arr: tmp = bin(i)[2:] tmp = tmp.zfill(fill) t.insert(tmp) def solve3(arr, x, t): x = bin(x)[2:] x = x.zfill(fill) ans = t.find(x) return ans ans = 0 for i in arr: tmp = solve3(arr, i, t) ans = max(ans, tmp) return ans
CLASS_DEF FUNC_DEF NONE ASSIGN VAR VAR ASSIGN VAR LIST NONE NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN IF VAR FUNC_CALL VAR VAR NUMBER NONE ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN IF VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER NONE EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class TrieNode: def __init__(self): self.children = [None] * 2 def insert(root, num): curr = root for i in range(31, -1, -1): bit = num >> i & 1 if curr.children[bit] is None: curr.children[bit] = TrieNode() curr = curr.children[bit] class Solution: def max_xor(self, arr, n): root = TrieNode() for num in arr: insert(root, num) max_xor = 0 for num in arr: curr = root xor = 0 for i in range(31, -1, -1): bit = num >> i & 1 if curr.children[bit ^ 1] is not None: xor |= 1 << i curr = curr.children[bit ^ 1] else: curr = curr.children[bit] max_xor = max(max_xor, xor) return max_xor
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NONE VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class TrieNode: def __init__(self): self.child = {} def increase(self, number): cur = self for i in range(31, -1, -1): bit = number >> i & 1 if bit not in cur.child: cur.child[bit] = TrieNode() cur = cur.child[bit] def findMax(self, number): cur, ans = self, 0 for i in range(31, -1, -1): bit = number >> i & 1 if 1 - bit in cur.child: cur = cur.child[1 - bit] ans |= 1 << i else: cur = cur.child[bit] return ans class Solution: def max_xor(self, nums, n): trieNode = TrieNode() for x in nums: trieNode.increase(x) ans = 0 for x in nums: ans = max(ans, trieNode.findMax(x)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Solution: def max_xor(self, nums, n): overAllMax = 0 trie = Trie() for num in nums: trie.insert(num) for num in nums: currMax = trie.getMaxXor(num) overAllMax = max(overAllMax, currMax) return overAllMax class Node: def __init__(self): self.links = [None, None] def put(self, bit): self.links[bit] = Node() def containsKey(self, bit): return None != self.links[bit] def getNext(self, bit): return self.links[bit] class Trie: def __init__(self): self.root = Node() def insert(self, num): node = self.root for i in range(32 - 1, -1, -1): currBit = num >> i & 1 if not node.containsKey(currBit): node.put(currBit) node = node.getNext(currBit) def getMaxXor(self, num2): maxXor = 0 node = self.root for i in range(32 - 1, -1, -1): currBit = num2 >> i & 1 desired = 1 - currBit if node.containsKey(desired): maxXor = maxXor | 1 << i node = node.getNext(desired) else: node = node.getNext(currBit) return maxXor
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST NONE NONE FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_DEF RETURN NONE VAR VAR FUNC_DEF RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class node: def __init__(self, data): self.data = data self.childs = {} class trie: def __init__(self): self.head = node(0) self.array = [] def insert(self, num): curr = self.head inbit = "" for i in range(32, -1, -1): if num & 1 << i: inbit += "1" if "1" in curr.childs: curr = curr.childs["1"] else: curr.childs["1"] = node("1") curr = curr.childs["1"] else: inbit += "0" if "0" in curr.childs: curr = curr.childs["0"] else: curr.childs["0"] = node("0") curr = curr.childs["0"] curr.childs["#"] = num self.array.append(inbit) def getmax(self, i): inbit = self.array[i] start = 0 curr = self.head while start < len(inbit): if inbit[start] == "1": currbit = "0" else: currbit = "1" if currbit in curr.childs: curr = curr.childs[currbit] else: curr = curr.childs[inbit[start]] start += 1 return curr.childs["#"] class Solution: def max_xor(self, arr, n): if n == 1: return 0 else: t = trie() for i in arr: t.insert(i) maxi = 0 for i in range(len(arr)): curr = t.getmax(i) maxi = max(maxi, curr ^ arr[i]) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR DICT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR STRING IF STRING VAR ASSIGN VAR VAR STRING ASSIGN VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR STRING VAR STRING IF STRING VAR ASSIGN VAR VAR STRING ASSIGN VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR STRING VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR STRING CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class TrieNode: def __init__(self): self.bin_dict = {} class Solution: def Insert_Into_Trie(self, binary_str, root): for bit in binary_str: if bit in root.bin_dict: root = root.bin_dict[bit] else: root.bin_dict[bit] = TrieNode() root = root.bin_dict[bit] def Get_Binary_Str(self, no): binary_str = bin(no)[2:] return "0" * (32 - len(binary_str)) + binary_str def Construct_Trie(self, arr): root, ans, n = TrieNode(), 0, len(arr) binary_str, temp_root = self.Get_Binary_Str(arr[0]), root for bit in binary_str: temp_root.bin_dict[bit] = TrieNode() temp_root = temp_root.bin_dict[bit] for i in range(1, n): temp_root, bin_str, bin_form = root, "", self.Get_Binary_Str(arr[i]) for bit in bin_form: opp_bit = "1" if bit == "0" else "0" if opp_bit in temp_root.bin_dict: bin_str += "1" temp_root = temp_root.bin_dict[opp_bit] else: bin_str += "0" temp_root = temp_root.bin_dict[bit] ans = max(ans, int(bin_str, 2)) self.Insert_Into_Trie(bin_form, root) return ans def max_xor(self, arr, n): return self.Construct_Trie(arr)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT CLASS_DEF FUNC_DEF FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR STRING FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR STRING STRING STRING IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
from sys import stdin input = stdin.readline class trie: def __init__(self): self.child = [] self.data = None def insert(self, value): self.child.append(trie()) self.child[-1].data = value def make(self, value): for x in self.child: if x.data == value: return x self.insert(value) return self.child[-1] def dfs(root, val, lvl=0): ans = 0 if len(root.child) == 1: value = val >> 29 - lvl & 1 ans += dfs(root.child[0], val, lvl + 1) ans += (value != root.child[0].data) * (1 << 29 - lvl) elif len(root.child) == 2: value = val >> 29 - lvl & 1 left, right = root.child[0], root.child[1] if left.data == 1: left, right = right, left if value != left.data: ans += dfs(left, val, lvl + 1) else: ans += dfs(right, val, lvl + 1) ans += 1 << 29 - lvl return ans def answer(): head = trie() for i in range(n): root = head for bit in range(30 - 1, -1, -1): root = root.make(a[i] >> bit & 1) ans = 0 for i in range(n): ans = max(ans, dfs(head, a[i])) return ans for T in range(int(input())): n = int(input()) a = list(map(int, input().split())) print(answer()) exit(0)
ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NONE FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_DEF FOR VAR VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER FUNC_DEF NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class node: def __init__(self, val=None): self.val = val self.one = None self.zero = None class Solution: def __init__(self): self.root = node(None) def insert(self, x): temp = self.root for i in range(31, -1, -1): bit = 1 << i & x if bit: if not temp.one: temp.one = node() temp = temp.one else: if not temp.zero: temp.zero = node() temp = temp.zero temp.val = x def show(self, root): if not root: return if root.val != None: print(root.val) return self.show(root.zero) self.show(root.one) def max_xor_check(self, x): temp = self.root for i in range(31, -1, -1): bit = 1 << i & x if bit: if temp.zero: temp = temp.zero else: temp = temp.one elif temp.one: temp = temp.one else: temp = temp.zero return temp.val ^ x def max_xor(self, arr, n): temp = self.root self.insert(arr[0]) m_xor = 0 for x in arr[1:]: m_xor = max(m_xor, self.max_xor_check(x)) self.insert(x) return m_xor
CLASS_DEF FUNC_DEF NONE ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NONE FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR RETURN IF VAR NONE EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Node: def __init__(self): self.children = dict() def get(self, node): return self.children[node] def contains_key(self, key): return key in self.children def put(self, key): self.children[key] = Node() class Trie: def __init__(self): self.root = Node() def insert(self, num): node = self.root for i in reversed(range(32)): bit = num >> i & 1 if not node.contains_key(bit): node.put(bit) node = node.get(bit) def get_max_xor(self, num): node = self.root max_num = 0 for i in reversed(range(32)): bit = num >> i & 1 if node.contains_key(1 - bit): max_num += 2**i node = node.get(1 - bit) else: node = node.get(bit) return max_num class Solution: def max_xor(self, nums, n): trie = Trie() max_xor = 0 for num in nums: trie.insert(num) for num in nums: max_xor = max(max_xor, trie.get_max_xor(num)) return max_xor
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Solution: def max_xor(self, arr, n): ans = 0 for i in range(31, -1, -1): prefixs = set([(num >> i) for num in arr]) ans <<= 1 candidate = ans + 1 for pre in prefixs: if candidate ^ pre in prefixs: ans = candidate break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Solution: class Trie: class TrieNode: def __init__(self): self.links = [None for i in range(2)] def __init__(self): self.root = self.TrieNode() def getbit(self, num, ind): return num >> ind & 1 def insert(self, num, maxlen): node = self.root binary = bin(num) for i in range(maxlen - 1, -1, -1): bit = self.getbit(num, i) if node.links[bit] == None: node.links[bit] = self.TrieNode() node = node.links[bit] def getMax(self, num, maxlen): node = self.root maxnum = 0 for i in range(maxlen - 1, -1, -1): bit = self.getbit(num, i) if node.links[1 - bit] != None: maxnum = maxnum | 1 << i node = node.links[1 - bit] else: node = node.links[bit] return maxnum def max_xor(self, A, n): r = self.Trie() maxlen = len(bin(max(A))[2:]) ans = 0 for i in A: r.insert(i, maxlen) ans = max(ans, r.getMax(i, maxlen)) return ans
CLASS_DEF CLASS_DEF CLASS_DEF FUNC_DEF ASSIGN VAR NONE VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR NONE ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Solution: def max_xor(self, arr, n): prefix_mask = 0 ans = 0 for i in range(31, -1, -1): prefix_mask |= 1 << i prefixes = set([(prefix_mask & num) for num in arr]) newMax = ans | 1 << i for pre in prefixes: if pre ^ newMax in prefixes: ans = newMax break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Node: def __init__(self): self.zero = None self.one = None class Trie: def __init__(self): self.root = Node() def insert(self, val): tem = self.root for i in range(31, -1, -1): bit = val >> i & 1 if bit == 0: if tem.zero == None: tem.zero = Node() tem = tem.zero else: if tem.one == None: tem.one = Node() tem = tem.one def max_xor_fin(self, val): tem = self.root ans = 0 for i in range(31, -1, -1): bit = val >> i & 1 if bit == 0: if tem.one: tem = tem.one ans += 1 << i else: tem = tem.zero elif tem.zero: tem = tem.zero ans += 1 << i else: tem = tem.one return ans def max_xor(self, arr, n): maxi = 0 self.insert(arr[0]) for i in range(1, n): maxi = max(self.max_xor_fin(arr[i]), maxi) self.insert(arr[i]) return maxi class Solution: def max_xor(self, arr, n): tri = Trie() return tri.max_xor(arr, n)
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Solution: def max_xor(self, arr, n): r = 0 k = 0 for i in range(31, -1, -1): m = r | 1 << i s = set() k = k | 1 << i for j in arr: l = j & k s.add(l) for pre in s: if m ^ pre in s: r = m break return r
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class node: def __init__(self): self.c = {} self.end = -1 class Trie: def __init__(self): self.r = node() def insert(self, word, i): iter = self.r for c in word: if c not in iter.c: iter.c[c] = node() iter = iter.c[c] iter.end = i def search(self, word): iter = self.r for c in word: f = "1" if c == "0" else "0" if f in iter.c: iter = iter.c[f] else: iter = iter.c[c] return iter.end class Solution: def max_xor(self, arr, n): N = max(arr) mask = 1 while N: N //= 2 mask += 1 t = Trie() for i in range(n): val = bin(arr[i])[2:] val = val.zfill(mask) t.insert(val, i) ans = 0 for i in range(n): val = bin(arr[i])[2:] val = val.zfill(mask) idx = t.search(val) ans = max(ans, arr[i] ^ arr[idx]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR STRING STRING STRING IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Trie: def __init__(self): self.children = {} class Solution: def __init__(self): self.root = Trie() def insert(self, num): binary = bin(num)[2:].zfill(32) curr = self.root for c in binary: if c not in curr.children: curr.children[c] = Trie() curr = curr.children[c] def findMax(self, num): binary = bin(num)[2:].zfill(32) curr = self.root res = "" for bit in binary: if bit == "0": opp_bit = "1" elif bit == "1": opp_bit = "0" if opp_bit in curr.children: res += opp_bit curr = curr.children[opp_bit] else: res += bit curr = curr.children[bit] return int(res, 2) ^ num def max_xor(self, nums, n): maxXor = 0 for num in nums: self.insert(num) for num in nums: maxXor = max(maxXor, self.findMax(num)) return maxXor
CLASS_DEF FUNC_DEF ASSIGN VAR DICT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Solution: def max_xor(self, arr, n): trie = self.getTrie(arr, n) freq = {} for i in arr: if i not in freq: freq[i] = 1 else: freq[i] += 1 ans = -1 for i in range(n): num = arr[i] binNum = bin(num)[2:] binNum = "0" * (32 - len(binNum)) + binNum current = trie fn = "" for value in binNum: v = "1" if value == "0" else "0" if v in current: fn += v current = current[v] else: fn += value current = current[value] fn = int(fn, 2) ans = max(ans, fn ^ num) return ans def getTrie(self, arr, n): trie = {} for i in range(n): currentNumber = arr[i] binNum = bin(currentNumber)[2:] binNum = "0" * (32 - len(binNum)) + binNum current = trie for value in binNum: if value not in current: current[value] = {} current = current[value] return trie
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR STRING STRING STRING IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
SIZE = 19 class TrieNode: def __init__(self): self.children = [None] * 2 class Trie: def __init__(self): self.root = TrieNode() def insert(self, N): curr = self.root for i in range(SIZE, -1, -1): bit = N >> i & 1 if curr.children[bit] is None: curr.children[bit] = TrieNode() curr = curr.children[bit] def search(self, N): curr = self.root ret = 0 for i in range(SIZE, -1, -1): bit = N >> i & 1 if curr.children[bit]: if curr.children[bit ^ 1]: curr = curr.children[bit ^ 1] ret += 1 << i else: curr = curr.children[bit] else: curr = curr.children[bit ^ 1] ret += 1 << i return ret class Solution: def max_xor(self, A, N): ans = 0 trie = Trie() trie.insert(A[0]) for i in range(1, N): ans = max(ans, trie.search(A[i])) trie.insert(A[i]) return ans
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array of non-negative integers of size N. Find the maximum possible XOR between two numbers present in the array. Example 1: Input: Arr = {25, 10, 2, 8, 5, 3} Output: 28 Explanation: The maximum result is 5 ^ 25 = 28. Example 2: Input : Arr = {1, 2, 3, 4, 5, 6, 7} Output : 7 Explanation : The maximum result is 1 ^ 6 = 7. Your task : You don't need to read input or print anything. Your task is to complete the function max_xor() which takes an array of integers and it's size as input and returns maximum XOR of two numbers in an array. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 2 <= N <=5*10^{4} 1<= A[i] <= 10^{6}
class Solution: def max_xor(self, arr, n): ans = 0 mask = 0 for i in range(31, -1, -1): mask |= 1 << i temp = ans | 1 << i s = set() for j in arr: num = j & mask if num ^ temp in s: ans = temp break s.add(num) s.clear() return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
def popcnt(x): cnt = 0 while x != 0: cnt += x & 1 x >>= 1 return cnt class Solution: def minVal(self, a, b): nbits = popcnt(b) x = a mask = 1 while popcnt(x) < nbits: x |= mask mask <<= 1 mask = 1 while popcnt(x) > nbits: x &= ~mask mask <<= 1 return x
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): ans = 0 totalb = bin(b)[2:].count("1") for i in range(31, -1, -1): if totalb == 0: return ans if a & 1 << i: ans |= 1 << i totalb -= 1 for i in range(32): if totalb == 0: return ans if a & 1 << i == 0: ans |= 1 << i totalb -= 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR NUMBER RETURN VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def countSetBit(self, num): i = 0 while num: num = num & num - 1 i += 1 return i def minVal(self, a, b): i = self.countSetBit(b) a = bin(a)[2:] s = "" for x in a: if x == "1" and i != 0: s += "1" i -= 1 elif x == "1" or x == "0": s += "0" else: pass if i != 0: s = list(s[::-1]) for x in range(len(s)): if s[x] == "0": s[x] = "1" i -= 1 if i == 0: break s = "".join(s)[::-1] if i != 0: s += "1" * i return int(s, 2)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR NUMBER VAR STRING VAR NUMBER IF VAR STRING VAR STRING VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR NUMBER IF VAR NUMBER VAR BIN_OP STRING VAR RETURN FUNC_CALL VAR VAR NUMBER
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): if b == 0: return b countb = 0 for i in bin(b).replace("0b", ""): if i == "1": countb += 1 z = 0 bina = bin(a).replace("0b", "") for i in range(len(bina)): if bina[i] == "1": z += int("1" + "0" * len(bina[i + 1 :])) countb -= 1 if countb == 0: break if countb > 0: for i in range(len(bina) - 1, -1, -1): if bina[i] == "0": z += int("1" + "0" * len(bina[i + 1 :])) countb -= 1 if countb == 0: break if countb > 0: z += int("1" * countb + "0" * len(bina)) return int(str(z), 2)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): set_bits = 0 while b > 0: set_bits += b % 2 b = b // 2 x = 0 for i in range(31, -1, -1): if set_bits > 0 and a & 1 << i: x = x ^ 1 << i set_bits -= 1 if set_bits > 0: for i in range(32): if x & 1 << i == 0: x = x ^ 1 << i set_bits -= 1 if set_bits == 0: break return x
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
def bitcount(a): c = 0 while a: c += a & 1 a >>= 1 return c def topBitsIn(a, n): mask = 1 while mask < a: mask <<= 1 res = 0 taken = 0 while taken < n: if a & mask != 0: res |= mask taken += 1 mask >>= 1 if mask == 0: raise ValueError() return res def bottomBitsNotIn(a, n): mask = 1 taken = 0 res = 0 while taken < n: if a & mask == 0: res |= mask taken += 1 mask <<= 1 return res class Solution: def minVal(self, a, b): ac = bitcount(a) needed = bitcount(b) if needed < ac: return topBitsIn(a, needed) return a | bottomBitsNotIn(a, needed - ac)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def countOnes(self, n): res = 0 while n > 0: if n % 2 == 1: res += 1 n //= 2 return res def minVal(self, a, b): a1 = self.countOnes(a) b1 = self.countOnes(b) if a1 == b1: return a a2 = list(bin(a).replace("0b", "")) b2 = list(bin(b).replace("0b", "")) a3 = a2.copy() if a1 > b1: i = a1 - b1 for j in range(len(a3) - 1, -1, -1): if a3[j] == "1": a3[j] = "0" i -= 1 if i == 0: break elif a1 < b1: x = len(a2) y = len(b2) if x > y: for i in range(x - y): b2.insert(0, "0") if x < y: for i in range(y - x): a2.insert(0, "0") i = b1 - a1 a3 = a2.copy() for j in range(len(a3) - 1, -1, -1): if a3[j] == "0": a3[j] = "1" i -= 1 if i == 0: break a3.insert(0, "b") a3.insert(0, "0") a3 = "".join(a3) a3 = int(a3, 2) return a3
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER STRING IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): B = bin(b)[2:] b_one = B.count("1") A = bin(a)[2:] ans = [] for i in range(len(A)): if A[i] == "1": if b_one: ans.append("1") b_one -= 1 else: ans.append("0") else: ans.append("0") for j in range(len(A) - 1, -1, -1): if ans[j] == "0": if b_one: ans[j] = "1" b_one -= 1 if b_one: ans = ["1"] * b_one + ans x = "".join(ans) return int(x, 2)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR ASSIGN VAR VAR STRING VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING VAR VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR NUMBER
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): set_bits = 0 while b > 0: set_bits += b % 2 b = b // 2 s = [] for i in range(31, -1, -1): bit = a & 1 << i if bit and set_bits > 0: s.append("1") set_bits -= 1 else: s.append("0") s = s[::-1] for i in range(32): if set_bits > 0: if s[i] == "0": s[i] = "1" set_bits -= 1 else: break result = 0 for i in range(32): result += 2**i * int(s[i]) return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): x = bin(b)[2:] z = x.count("1") ans = [] for i in bin(a)[2:]: if i == "0": ans += [i] elif z > 0: ans += ["1"] z -= 1 else: ans += ["0"] if z > 0: for i in range(len(ans) - 1, -1, -1): if ans[i] == "0": ans[i] = "1" z -= 1 if z == 0: break ans = ["1"] * z + ans return int("".join(map(str, ans)), 2)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING VAR LIST VAR IF VAR NUMBER VAR LIST STRING VAR NUMBER VAR LIST STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST STRING VAR VAR RETURN FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): if b == 0: return 0 if a > b: n = len(bin(a)) - 2 else: n = len(bin(b)) - 2 a, b = bin(a)[2:], bin(b)[2:] bc = b.count("1") ac = a.count("1") if ac == bc: return int(a, 2) if ac < bc: p, i = bc - ac, 1 s = list(a) while p: try: if s[-i] == "0": s[-i] = "1" p -= 1 i += 1 except: s = ["1" for z in range(p)] + s break return int("".join(s), 2) else: s = ["0" for i in range(n)] if len(a) != n: a = "0" * (n - len(a)) + a for i in range(n): if a[i] == "1": s[i] = "1" bc -= 1 if not bc: break return int("".join(s), 2)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP STRING VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): a = bin(a)[2:] a = "0" * (32 - len(a)) + a x = bin(b)[2:].count("1") ans = [(0) for _ in range(32)] for i in range(32): if x == 0: break if a[i] == "1": ans[i] = 1 x -= 1 ans = ans[::-1] for i in range(32): if x == 0: break if ans[i] == 0: ans[i] = 1 x -= 1 t = 0 for i in range(32): t += ans[i] * 2**i return t
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): setbits = bin(b).count("1") res = 0 for i in range(30, -1, -1): if a & 1 << i and setbits: res |= 1 << i setbits -= 1 i = 0 while setbits: if not res & 1 << i: res |= 1 << i setbits -= 1 i += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): l = [] c = 0 d = 0 y = a while a != 0: c += a % 2 l.append(str(a % 2)) a = a // 2 d = bin(b).count("1") if d == c: return y elif d < c: n = len(l) for i in range(n - 1, -1, -1): if d > 0 and l[i] == "1": l[i] = "0" d -= 1 s = "".join(l) return int(s[::-1], 2) ^ y for i in range(7): l.append("0") g = d - c for i in range(len(l)): if l[i] == "1": l[i] = "0" elif g > 0 and l[i] == "0": l[i] = "1" g -= 1 else: pass s = "".join(l) return int(s[::-1], 2) ^ y
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING IF VAR NUMBER VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def binaryToDecimal(self, binary_num): dec_num = 0 m = 1 for i in range(len(binary_num) - 1, -1, -1): digit = int(binary_num[i]) dec_num = dec_num + digit * m m = m * 2 return dec_num def minVal(self, A, B): a = [] b = [] while A > 0: bit = A % 2 a.insert(0, bit) A = int(A / 2) while B > 0: bit = B % 2 b.insert(0, bit) B = int(B / 2) count1 = 0 count2 = 0 r = [] diff = b.count(1) - a.count(1) count0 = a.count(0) for i in range(0, len(a), 1): x = a[i] if x == 1: if count1 < b.count(1): r.append(1) count1 = count1 + 1 else: r.append(0) elif x == 0: if diff >= count0: r.append(1) count1 = count1 + 1 diff = diff - 1 count0 = count0 - 1 else: r.append(0) count0 = count0 - 1 count2 = count2 + 1 while r.count(1) < b.count(1): r.append(1) return self.binaryToDecimal(binary_num=r)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): b = bin(b)[2:] a = bin(a)[2:] c = 0 for i in b: if i == "1": c += 1 arr = [(0) for i in range(len(a))] i = 0 while i < len(a) and c > 0: if a[i] == "1": arr[i] = 1 c -= 1 i += 1 i -= 1 while i >= 0 and c != 0: if arr[i] == 0: arr[i] = 1 c -= 1 i -= 1 arr += [1] * c ans = 0 arr = arr[::-1] for i in range(len(arr)): if arr[i] == 1: ans += 2**i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): bit_a = 0 bit_b = 0 tmp_a = a while tmp_a != 0: bit_a += 1 tmp_a = tmp_a & tmp_a - 1 tmp_b = b while tmp_b != 0: bit_b += 1 tmp_b = tmp_b & tmp_b - 1 if bit_a == bit_b: num = a elif bit_a > bit_b: num = a mask = 1 while bit_a != bit_b: if num & mask: num = num ^ mask bit_a -= 1 mask = mask << 1 else: num = a mask = 1 while bit_a != bit_b: if num & mask == 0: num = num ^ mask bit_a += 1 mask = mask << 1 return num
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: def minVal(self, a, b): B = b A = a count_set_bit_b = bin(b).count("1") result = 0 i = 31 while i >= 0 and count_set_bit_b > 0: if A & 1 << i > 0: result |= 1 << i count_set_bit_b -= 1 i -= 1 i = 0 while i < 31 and count_set_bit_b > 0: if A & 1 << i == 0: result |= 1 << i count_set_bit_b -= 1 i += 1 return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B. Example 1: Input: A = 3, B = 5 Output: 3 Explanation: Binary(A) = Binary(3) = 011 Binary(B) = Binary(5) = 101 The XOR will be minimum when x = 3 i.e. (3 XOR 3) = 0 and the number of set bits in 3 is equal to the number of set bits in 5. Example 2: Input: A = 7, B = 12 Output: 6 Explanation: (7)_{2}= 111 (12)_{2}= 1100 The XOR will be minimum when x = 6 i.e. (6 XOR 7) = 1 and the number of set bits in 6 is equal to the number of set bits in 12. Your task : You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question. Expected Time Complexity : O(log N) Expected Auxiliary Space : O(1) Constraints : 0 <= A, B <= 10^{9}
class Solution: @staticmethod def get_set_bit_indices(v: int): indices, cur_index = [], 0 while v: if v & 1: indices.append(cur_index) v >>= 1 cur_index += 1 return indices def minVal(self, a: int, b: int): if b == 0: return 0 bit_count = len(self.get_set_bit_indices(b)) indices = self.get_set_bit_indices(a) ans = 0 for i in indices[-bit_count:]: ans |= 1 << i diff = bit_count - len(indices) cur, indices_set = 0, set(indices) while diff > 0: set_bit = 1 << cur if cur not in indices_set and ans & set_bit == 0: ans |= set_bit diff -= 1 cur += 1 return ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR LIST NUMBER WHILE VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR