description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, N, A): ans = 0 mod = 10**9 + 7 for i in range(32): mask = 1 << i ones = 0 zeros = 0 for val in A: if val & mask > 0: ones += 1 else: ze...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR...
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, N, A): num_ones = [0] * 31 for ele in A: i = 0 while ele: if ele & 1: num_ones[i] += 1 i += 1 ele = ele >> 1 res = 0 for ele in num_ones: res +...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER ...
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, N, A): max_val = max(A) bin_max = bin(max_val)[2:] max_len = len(bin_max) countOnesZerosEachBit = [[0, 0] for i in range(max_len)] for num in A: bin_num = bin(num)[2:].zfill(max_len) for d in range(max_len): ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR BIN_OP BIN_OP VAR VA...
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, n, arr): lis = [0] * 33 for num in arr: i = num j = 0 while i > 0: if i & 1 == 1: lis[j] += 1 i //= 2 j += 1 ans = 0 m = 10**9 + 7 for ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR ...
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, n, arr): l = [(0) for i in range(32)] for val in arr: for i in range(32): if val & 1 << i: l[i] += 1 ans = 0 for i in range(32): ans += 2 * (n - l[i]) * l[i] % (10**9 + 7) return ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR BIN_OP BIN_...
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, N, A): maxOfA = max(A) ans = 0 while maxOfA > 0: countOfZero = 0 countOfOne = 0 for i in range(N): lastBit = A[i] & 1 if lastBit == 0: countOfZero += 1 ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN ...
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, N, A): ma = len(bin(max(A))) - 2 ar0 = [(0) for x in range(ma)] ar1 = [(0) for x in range(ma)] for x in A: for y in range(ma): bit = x & 1 if bit == 1: ar1[y] += 1 els...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR...
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, N, A): t = 0.5 ans = 0 for i in range(32): c1 = c2 = 0 t *= 2 for j in range(N): if A[j] & 1 << i: c1 += 1 else: c2 += 1 ans += c1 * c2...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, N, A): has = {} for i in range(31): has[i] = 0 for j in range(N): if A[j] > 0: has[i] += A[j] % 2 A[j] //= 2 res = 0 for i in has: res += has[i] * (N - has...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, N, A): mx = max(A) ans = 0 bits = len(bin(mx)[2:]) for x in range(0, bits + 2): cnt = 0 for i in range(N): if A[i] & 1 << x: cnt += 1 ans += cnt * (N - cnt) * 2 return...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMB...
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, N, A): ans = 0 m = 10**9 + 7 for i in range(0, 32): z = 0 o = 0 for j in A: if 1 << i & j == 0: z += 1 else: o += 1 ans += z * o ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, N, A): sum = 0 n = len(A) x = 1000000007 for i in range(0, 32): count = 0 for j in range(0, n): if A[j] & 1 << i: count += 1 sum += 2 * count * (n - count) return sum ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
from itertools import combinations class Solution: def bin_diffrence(self, pair): bin_diff_bet_pair = 0 a = min(pair) b = max(pair) bin_a = bin(a)[2:] bin_b = bin(b)[2:] diff_bet_len = len(bin_b) - len(bin_a) bin_a = "0" * diff_bet_len + bin_a i = l...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CAL...
We define f (X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f (2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively. The first and the third bit differ, so f (2, 7) = 2. You are given an array A of N integers, A_{1}, A_{2} ,…, A_{N}. Find sum...
class Solution: def countBits(self, N, A): sum = 0 for i in range(32): count1 = 0 for j in range(N): if A[j] & 1 << i: count1 += 1 count0 = N - count1 sum += 2 * count0 * count1 return sum % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
INF = 1 << 60 class Solution: def shortestSuperstring(self, A: List[str]) -> str: n = len(A) overlap = [([0] * n) for _ in range(n)] for i, j in product(range(n), repeat=2): l = min(len(A[i]), len(A[j])) overlap[i][j] = max( k for k in range(l + 1) ...
ASSIGN VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR B...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: @lru_cache(None) def dp(p, mask): if mask == 0: return 0, 0 result = 0 k = -1 for j in range(n): if not mask >> j & 1: continue ...
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETU...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: n = len(A) graph = [([0] * n) for _ in range(n)] for i, word1 in enumerate(A): for j, word2 in enumerate(A): if i == j: continue for k in range(min(len(word1),...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT ...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: N = len(A) def Cost(x, y): cost = len(y) max_similarity = 0 for i in range(1, min(len(x), len(y))): if x[-i:] == y[:i]: max_similarity = i return ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR FOR...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: def dist(s1, s2): ans = 0 for k in range(min(len(s1), len(s2))): if s1[len(s1) - k :] == s2[:k]: ans = len(s2) - k return ans n = len(A) g = [([0] * ...
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_C...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: n = len(A) overlaps = [([0] * n) for _ in range(n)] for i, x in enumerate(A): for j, y in enumerate(A): for ans in range(min(len(x), len(y)), -1, -1): if x.endswith(y[:ans]): ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: n = len(A) common = {} for i, s1 in enumerate(A): for j in range(i): s2 = A[j] longest1, longest2 = 0, 0 for k in range(min(len(s1), len(s2)) + 1): ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR V...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: n = len(A) overlaps = [([0] * n) for x in range(n)] for i, x in enumerate(A): for j, y in enumerate(A): if i != j: for l in range(min(len(x), len(y)), -1, -1): ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASS...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: N = len(A) cost = [([0] * N) for _ in range(N)] for i in range(N): for j in range(N): cost[i][j] = math.inf for k in range(len(A[i])): if A[j].startswith(A[i][...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR V...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: def dist(a, b): for i in range(min(len(a), len(b)), 0, -1): if a[-i:] == b[:i]: return len(b) - i return len(b) def build_graph(): graph = [([0] * n) for _ i...
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR A...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: n = len(A) w = [([0] * n) for _ in range(n)] for i in range(n): for j in range(n): for k in range(min(len(A[i]), len(A[j])), 0, -1): if A[j].startswith(A[i][-k:]): ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_D...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: A = [ a for i, a in enumerate(A) if all(a not in b for j, b in enumerate(A) if i != j) ] def memo(f): dic = {} def f_alt(*args): if args not in d...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR RETU...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: def cost(a, b): alen, blen = len(a), len(b) minlen = min(alen, blen) for i in range(minlen - 1, 0, -1): if a[alen - i :] == b[:i]: return blen - i return ...
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VA...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def int2arr(self, x, n): arr = [] x1 = x for i in range(n): arr.append(x1 % 2) x1 = x1 // 2 return arr def arr2int(self, arr): x = 0 for i in range(len(arr)): x += arr[i] * 2**i return x def shorte...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: def extra_to_append(w1, w2): for i in range(len(w1)): if w2.startswith(w1[i:]): return w2[len(w1) - i :] return w2 n = len(A) extra = [([""] * n) for _ in range(...
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: n = len(A) self.cost = [[(0) for j in range(n)] for i in range(n)] for i in range(n): for j in range(n): w1 = A[i] w2 = A[j] k = min(len(w1), len(w2)) ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSI...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: size = len(A) costs = [([0] * size) for _ in range(size)] for i in range(size): for j in range(size): if i == j: costs[i][j] = 0 continue w...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_C...
Given an array A of strings, find any smallest string that contains each string in A as a substring. We may assume that no string in A is substring of another string in A.   Example 1: Input: ["alex","loves","leetcode"] Output: "alexlovesleetcode" Explanation: All permutations of "alex","loves","leetcode" would also b...
class Solution: def shortestSuperstring(self, A: List[str]) -> str: n, N = len(A), 1 << len(A) w = [([0] * n) for _ in range(n)] for i in range(n): for j in range(n): for k in range(min(len(A[i]), len(A[j])), 0, -1): if A[j].startswith(A[i][-k...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR V...
Given an array arr of N integers and an integer K, find the number of subsets of arr having XOR of elements as K. Example 1: Input: N = 4 k = 6 arr: 6 9 4 2 Output: 2 Explanation: The subsets are {4,2} and {6} Example 2: Input: N = 5 K = 4 arr: 1 2 3 4 5 Output: 4 Explanation: The subsets are {1, 5}, {4}, {1, 2, 3, ...
class Solution: def subsetXOR(self, arr, N, K): ht = {(0): 1} cnt = 0 for i in arr[:N]: temp = i ^ K if temp in ht: cnt += ht[temp] for k, v in list(ht.items()): xor = i ^ k ht[xor] = ht.get(xor, 0) + v ...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR
Given an array arr of N integers and an integer K, find the number of subsets of arr having XOR of elements as K. Example 1: Input: N = 4 k = 6 arr: 6 9 4 2 Output: 2 Explanation: The subsets are {4,2} and {6} Example 2: Input: N = 5 K = 4 arr: 1 2 3 4 5 Output: 4 Explanation: The subsets are {1, 5}, {4}, {1, 2, 3, ...
class Solution: def solve(self, arr, n, k, i, xor): if i == n: return 1 if xor == k else 0 if dp[i][xor] != -1: return dp[i][xor] x = self.solve(arr, n, k, i + 1, arr[i] ^ xor) y = self.solve(arr, n, k, i + 1, xor) dp[i][xor] = x + y return dp...
CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUN...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: ksums = [{0}] maxk = len(A) // 2 for a in A: ksums = [[0]] + [ ( (ksums[k] if k < len(ksums) else set()) | {(a + x) for x in ksums[k - 1]} ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FU...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: N = len(A) if N <= 1: return False max_sum = sum(A) sums = [0] * (max_sum + 1) for i in range(N): num = A[i] for s in range(max_sum, num - 1, -1): if su...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: dp = set([(0, 0)]) suma = sum(A) N = len(A) A.sort() for num in A: for k, v in list(dp): if k >= N // 2: continue if num + v > suma * (k + 1...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR ...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: tot = sum(A) n = len(A) target = tot / n visited = [False] * len(A) m = n // 2 possible = False for i in range(1, m + 1): if tot * i % n == 0: possible = True ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: A.sort(reverse=True) def helper(goal, cnt): @lru_cache(None) def dp(i, tot, left): if tot == goal and left == 0: return True if tot > goal or left == ...
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NONE RETURN FUNC_CALL VAR...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: S = sum(A) L = len(A) if L == 1: return False sizes = {(0, 0)} for n in A: for s, c in sizes.copy(): if c < L // 2: sizes.add((s + n, c + 1)) ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR VAR IF BIN_OP VAR BIN_...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: la = len(A) sa = sum(A) memo = {} def find(target, k, i): if k == 0: return target == 0 if k + i > la: return False if (target, k, i) in memo: ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CAL...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: memo = {} def dfs(k, idx, total): if k == 0: return total == 0 if k + idx > len(A): return False if (k, idx, total) in memo: return memo[k, idx...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BI...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: total = sum(A) l = len(A) dp = [0] * (total + 1) dp[A[0]] = 2 for i in range(1, l): for j in range(total - A[i], -1, -1): dp[j + A[i]] |= dp[j] << 1 dp[A[i]] |= 2 ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER FOR VA...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: m = len(A) total = sum(A) target = total / m A.sort() @lru_cache(None) def dfs(k, i, sum_b): if k == 0: return sum_b == 0 if i == len(A): r...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBE...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: A_sum = float(sum(A)) max_sublen = len(A) // 2 targets = {} last_valid_target = None for i in range(1, max_sublen + 1): target = A_sum * i / len(A) if target % 1 == 0: ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR I...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, nums): def find(target, k, i): if (target, k) in d and d[target, k] <= i: return False if k == 0: return target == 0 if k + i > len(nums): return False res = find...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: if len(A) < 2: return False sumA, lenA = sum(A), len(A) if sumA / lenA in A: return True if len(A) < 3: return False if not any(sumA * size % lenA == 0 for size in rang...
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER A...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def dp(self, A, index, sum_, size, aux, dp): if index == len(A): return False if size == 0: return sum_ == 0 key = str(index) + str(sum_) + str(size) if key in dp: return dp[key] if A[index] <= sum_: aux.append(...
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: total = sum(A) sets = [{0}] + [set() for _ in A] for num in A: for j in reversed(list(range(len(A) - 1))): for k in sets[j]: s = k + num if total * (j +...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR RETURN NUMBER E...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A): N, S = len(A), sum(A) if N == 1: return False A = [(z * N - S) for z in A] mid, left, right = N // 2, {A[0]}, {A[-1]} if not any(S * size % N == 0 for size in range(1, mid + 1)): return False ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER FOR VAR FUN...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: memo = {} def dfs(idx, k, total) -> bool: if k == 0: return total == 0 if k + idx > len(A): return False if (idx, k, total) in memo: return mem...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: A.sort() DP = [set() for _ in range(len(A) // 2 + 1)] all_sum = sum(A) DP[0] = set([0]) for item in A: for count in range(len(DP) - 2, -1, -1): if len(DP[count]) > 0: ...
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR LIST NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A) -> bool: if sum(A) == 0 and len(A) > 1: return True elif len(A) == 1: return False mem = {} ss = sum(A) n = len(A) def rec(index, cur_sum, length): if ( cur_sum * ...
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR RETURN NUM...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: def dp(idx, rem, cnt): if cnt == 0 and rem == 0: return True if idx == N or cnt == 0 or rem == 0: return False if (idx, rem, cnt) in memo: return memo[...
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASS...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: def dp(idx, L, target): if L == 0 and target == 0: return True if idx == N or L == 0 or target < 0: return False if (idx, L, target) in memo: return me...
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR V...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: dp = [] def is_possible(self, l, actual_sum, index, curr_sum, curr_element_count): if index >= len(l): return False if Solution.dp[curr_element_count][curr_sum] != None: return Solution.dp[curr_element_count][curr_sum] if ( curr_elemen...
CLASS_DEF ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NONE RETURN VAR VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR B...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: total_sum = sum(A) sum_dict = collections.defaultdict(set) for val in A: temp_list = [(1, val)] for key, set_val in sorted(sum_dict.items()): if key == len(A) // 2: ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FU...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: if len(A) < 3: return False sumA, lenA = sum(A), len(A) if sumA / lenA in A: return True A = [(i * lenA - sumA) for i in A] A.sort() a = [i for i in A if i < 0] b =...
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL V...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: su = sum(A) ii = len(A) // 2 + 1 arr = [set() for _ in range(ii)] arr[0].add(0) for x in A: for t in reversed(range(1, ii)): arr[t] |= set(map(lambda s: s + x, arr[t - 1])) ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NU...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: n = len(A) k = int(n / 2) dp = [set() for i in range(k + 1)] dp[0].add(0) for i, v in enumerate(A): for j in range(min(i + 1, k), 0, -1): for m in dp[j - 1]: ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR BIN_OP VAR NU...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A: List[int]) -> bool: avg, N = sum(A), len(A) if N < 2: return False if N == 2: return A[0] == A[1] A = [(num * N - avg) for num in A] dp = collections.defaultdict(set) for i, n in enumerate(A[:...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR...
In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty. Example : Input: [1,2,3,4,5,6,7,8] Output...
class Solution: def splitArraySameAverage(self, A): N, S, P = len(A), sum(A), [1] for a in A: P[1:] = [(p << a | q) for p, q in zip(P, P[1:] + [0])] return any(S * n % N == 0 and P[n] & 1 << S * n // N for n in range(1, N))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR LIST NUMBER FOR VAR VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER LIST NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL...
Roy has N coin boxes numbered from 1 to N. Every day he selects two indices [L,R] and adds 1 coin to each coin box starting from L to R (both inclusive). He does this for M number of days. After M days, Roy has a query: How many coin boxes have atleast X coins. He has Q such queries. Input: First line contains N ...
def bs(i, j, key): global final if key > final[j]: return j if key < final[i]: return -1 while j - i > 1: mid = (i + j) / 2 if final[mid] < key: i = mid elif final[mid] >= key: j = mid - 1 if final[j] < key: return j elif fi...
FUNC_DEF IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VA...
Roy has N coin boxes numbered from 1 to N. Every day he selects two indices [L,R] and adds 1 coin to each coin box starting from L to R (both inclusive). He does this for M number of days. After M days, Roy has a query: How many coin boxes have atleast X coins. He has Q such queries. Input: First line contains N ...
num_box = int(input()) num_days = int(input()) boxes_arr = [0] * (num_box + 2) boxcount_numcoin = [0] * (num_days + 1) retval_list = [0] * (num_days + 1) num_queries = 0 query = 0 for iterv in range(num_days): LR_list = [int(elem) for elem in input().split(" ")] boxes_arr[LR_list[0]] += 1 boxes_arr[LR_list[...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUN...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
class Solution: def check(self, string): temp = int(string, 2) while temp > 1: if temp % 5 != 0: return False temp /= 5 return True def cuts(self, s): n = len(s) if n == 0 or s[0] == "0": return -1 if self.chec...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER STRING RETURN NUMBER IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER V...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
import sys class Solution: def isPower(self, num, exp): pwr = 1 pcnt = 0 while num > 1: if num % exp == 0: num //= exp pwr += 1 else: return 0 return pwr def solve(self, s, start, count, mcount): ...
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN IF VAR FUNC_CALL VAR VAR RETURN IF VAR VAR STRING RETURN ASSIGN VAR NUMBER ASSIGN VA...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
class Solution: def cuts(self, s): def check(s, start, end): num = 0 j = 1 for i in range(end, start - 1, -1): if s[i] == "1": num += j j *= 2 while num > 1: if num % 5 != 0: ...
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR STRING RETURN NUMBER IF VAR VAR VAR NUM...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
class Solution: def p(self, n): if n == 1: return True while n >= 5: if n % 5 != 0: return False n /= 5 return n == 1 def cuts(self, s): m = 0 j = 0 while len(s) - 1 >= 0: c = 0 i = len(...
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER IF VAR NUMBER STRING RETURN NUMBER ...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
class Solution: def cuts(self, s): n = len(s) power = [1, 5, 25, 125, 625] binary = ["1", "101", "11001", "1111101", "1001110001"] dp = [float("inf")] * (n + 1) dp[0] = 0 for i in range(1, n + 1): for j in range(5): l = len(binary[j]) ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN ...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
class Solution: def is_power_of_5(self, n): while n % 5 == 0: n //= 5 return n == 1 def cuts(self, s): n = len(s) dp = [float("inf")] * (n + 1) dp[0] = 0 for i in range(1, n + 1): for j in range(i): num = int(s[j:i], 2) ...
CLASS_DEF FUNC_DEF WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
class Solution: def cuts(self, s): if not s: return -1 if s[0] == "0": return -1 pow5, local, out = 1, 0, 99 for i in range(len(s)): local *= 2 if s[i] == "1": local += 1 if local > pow5: pow...
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR NUMBER STRING RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL ...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
class Solution: def cuts(self, s): if s == "1": return 1 c = 0 d = 0 l = [ "1", "101", "11001", "1111101", "1001110001", "110000110101", "11110100001001", "10011000100101101",...
CLASS_DEF FUNC_DEF IF VAR STRING RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_C...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
def pow_5(n): if n == 0: return False if n == 1: return True if n % 5 == 0: return pow_5(n / 5) return False class Solution: def cuts(self, s): n = len(s) dp = [0] * (n + 1) for i in range(1, n + 1): j = i - 1 if s[j] == "0":...
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
class Solution: def cuts(self, s): def ok(ss): n = int(ss, 2) while n > 1 and n % 5 == 0: n //= 5 return n == 1 n = len(s) dp = [float("inf")] * (n + 1) dp[0] = 0 for i in range(n): for k in range(i, -1, -1): ...
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF ...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
class Solution: def cuts(self, s): c = list(s) l = len(c) dp = [0] * (l + 1) dp[0] = 0 for i in range(1, l + 1): index = i - 1 if c[index] == "0": dp[i] = -1 else: dp[i] = -1 t = 1000 ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUM...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
class Solution: def check(self, s): j = 1 num = 0 for i in range(len(s) - 1, -1, -1): if s[i] == "1": num = num + j j *= 2 while num > 1: if num % 5 != 0: return False num = num // 5 return True ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN...
Given a string s containing 0's and 1's. You have to return the smallest positive integer C, such that the binary string can be cut into C pieces and each piece should be of the power of 5 with no leading zeros. Note: The string s is a binary string. And if no such cuts are possible, then return -1. Example 1: Input: ...
class Solution: st = set() @staticmethod def initialize(): p = 1 for i in range(1, 23): Solution.st.add(bin(p)[2:]) p *= 5 @staticmethod def helper(s, idx): if idx >= len(s): return 0 ans = float("inf") for i in range(idx,...
CLASS_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF...
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ vases in a row (numbered $1$ through $N$, initially from left to right). He wants to sort them in a particular order which he finds the most beautiful. You are given a permutation $p_{1}, p_{2}, \ldots,...
import sys Ri = lambda: [int(x) for x in sys.stdin.readline().split()] ri = lambda: sys.stdin.readline().strip() def deletearrele(arr, ele, no): newarr = [] ite = 0 for i in range(len(arr)): if arr[i] != ele: newarr.append(arr[i]) elif ite < no: ite += 1 el...
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ...
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ vases in a row (numbered $1$ through $N$, initially from left to right). He wants to sort them in a particular order which he finds the most beautiful. You are given a permutation $p_{1}, p_{2}, \ldots,...
rr = lambda: input().strip() rrm = lambda: map(int, rr().split()) def solve(N, M, p): group, count = [0] * N, 0 j = 1 for i in range(M): x, y = rrm() if group[x - 1] + group[y - 1] == 0: group[x - 1] = group[y - 1] = j j += 1 elif group[x - 1] == 0: ...
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR ...
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ vases in a row (numbered $1$ through $N$, initially from left to right). He wants to sort them in a particular order which he finds the most beautiful. You are given a permutation $p_{1}, p_{2}, \ldots,...
for _ in range(int(input())): n, m = map(int, input().split()) p = [0] + list(map(int, input().split())) cor, tree, res = ( [sorted(list(map(int, input().split()))) for i in range(m)], [[] for i in p], 0, ) for i, j in cor: tree[i].append(j) tree[j].append(i) ...
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR LIST VAR VAR NUM...
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ vases in a row (numbered $1$ through $N$, initially from left to right). He wants to sort them in a particular order which he finds the most beautiful. You are given a permutation $p_{1}, p_{2}, \ldots,...
papa_array = [] cg = [] def spapa(kx): if papa_array[kx] != kx: sl = spapa(papa_array[kx]) papa_array[kx] = sl return papa_array[kx] def add_edge(kx, ky): sx = spapa(kx) sy = spapa(ky) if sx != sy: if cg[sx] == cg[sy]: papa_array[sy] = sx cg[sx] +=...
ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ...
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ vases in a row (numbered $1$ through $N$, initially from left to right). He wants to sort them in a particular order which he finds the most beautiful. You are given a permutation $p_{1}, p_{2}, \ldots,...
for _ in range(int(input())): N, M = list(map(int, input().split())) P = [0] P.extend(list(map(int, input().split()))) xy = [] for _ in range(M): xy.append(list(map(int, input().split()))) Tree = [[] for _ in range(N + 1)] for i, j in xy: Tree[i].append(j) Tree[j].app...
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CAL...
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ vases in a row (numbered $1$ through $N$, initially from left to right). He wants to sort them in a particular order which he finds the most beautiful. You are given a permutation $p_{1}, p_{2}, \ldots,...
def swap_items(l, swapped, ind): for i in range(ind, n): if swapIndex[l[i]] != swapIndex[i + 1]: j = 0 while j < n: if ( swapIndex[l[i]] == swapIndex[j + 1] and swapIndex[l[j]] == swapIndex[i + 1] ): ...
FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR BIN_OP ...
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has $N$ vases in a row (numbered $1$ through $N$, initially from left to right). He wants to sort them in a particular order which he finds the most beautiful. You are given a permutation $p_{1}, p_{2}, \ldots,...
from sys import stdin class UnionFind: def __init__(self, n): self.size = [(1) for _ in range(n)] self.parent = [i for i in range(n)] def find(self, i): while self.parent[i] != self.parent[self.parent[i]]: self.parent[i] = self.parent[self.parent[i]] return self.p...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ...
Elena is the topper of the class. Once her teacher asked her a problem. He gave Elena an array of integers of length n. He calls a subset of array arr good if its product can be represented as a product of one or more distinct prime numbers. He asked her to find the number of different good subsets in arr modulo 10^{9}...
mod = 10**9 + 7 mapper = [0] * 31 prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] for i in range(2, 31): if 0 == i % 4 or 0 == i % 9 or 25 == i: continue mask = 0 for j in range(10): if 0 == i % prime[j]: mask |= 1 << j mapper[i] = mask class Solution: def goodSubsets...
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF N...
Read problems statements in Mandarin Chinese and Russian. Alok-nath is man of equality. He needs your help to divide his “sanskars” evenly amongst all his followers. By doing this, Alok-nath can create equality amongst his followers and he'll be called a true “sanskari”. Alok-nath has N sanskars, and K followers. E...
def divide(l, num, dn): if dn == 0: return True if num == -1: return False if l[num] > dn or l[num] == 0: return divide(l, num - 1, dn) if divide(l, num - 1, dn - l[num]): l[num] = 0 return True return divide(l, num - 1, dn) for _ in range(int(input())): ...
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR F...
Read problems statements in Mandarin Chinese and Russian. Alok-nath is man of equality. He needs your help to divide his “sanskars” evenly amongst all his followers. By doing this, Alok-nath can create equality amongst his followers and he'll be called a true “sanskari”. Alok-nath has N sanskars, and K followers. E...
def ret_res(arr, vis_arr, ind, memo, val): if (ind, val) in memo: return memo[ind, val] elif val < 0: return None elif val == 0: return [] elif ind == -1: return None elif not vis_arr[ind]: ret1 = ret_res(arr, vis_arr, ind - 1, memo, val - arr[ind]) if...
FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER RETURN NONE IF VAR NUMBER RETURN LIST IF VAR NUMBER RETURN NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_...
Read problems statements in Mandarin Chinese and Russian. Alok-nath is man of equality. He needs your help to divide his “sanskars” evenly amongst all his followers. By doing this, Alok-nath can create equality amongst his followers and he'll be called a true “sanskari”. Alok-nath has N sanskars, and K followers. E...
import sys def is_partition(n, tot, count): if tot == 0: for v in range(count): sk.remove(l[v]) return True if tot < 0: return False if n < 0 and tot != 0: return False l[count] = sk[n] return is_partition(n - 1, tot - sk[n], count + 1) or is_partition( ...
IMPORT FUNC_DEF IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_C...
Read problems statements in Mandarin Chinese and Russian. Alok-nath is man of equality. He needs your help to divide his “sanskars” evenly amongst all his followers. By doing this, Alok-nath can create equality amongst his followers and he'll be called a true “sanskari”. Alok-nath has N sanskars, and K followers. E...
import sys tests = int(input()) for t in range(tests): line = input().split() sanskars = int(line[0]) followers = int(line[1]) intensities = list(map(int, input().split())) ti = sum(intensities) togive = ti / followers go = 1 if followers > sanskars or round(togive) != togive or max(int...
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER I...
Read problems statements in Mandarin Chinese and Russian. Alok-nath is man of equality. He needs your help to divide his “sanskars” evenly amongst all his followers. By doing this, Alok-nath can create equality amongst his followers and he'll be called a true “sanskari”. Alok-nath has N sanskars, and K followers. E...
def getans(arr, k): sm = sum(arr) if sm % k != 0 or k > len(arr): return "no" if all(i == 0 for i in arr): return "yes" req = sm // k visited = [False] * len(arr) retval = compute(arr, 0, k, req, visited, req) if retval: return "yes" return "no" def compute(arr,...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN STRING IF FUNC_CALL VAR VAR NUMBER VAR VAR RETURN STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR IF VAR RETURN STRING RETURN STRING FUNC_DEF I...
Read problems statements in Mandarin Chinese and Russian. Alok-nath is man of equality. He needs your help to divide his “sanskars” evenly amongst all his followers. By doing this, Alok-nath can create equality amongst his followers and he'll be called a true “sanskari”. Alok-nath has N sanskars, and K followers. E...
__author__ = "Rakshak.R.Hegde" target = 0 san = [] def recurse(i, sum): if sum == target: return True if sum > target or i == len(san): return False if recurse(i + 1, sum + san[i]): del san[i] return True return recurse(i + 1, sum) t = int(input()) while t: t -= 1...
ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN V...
Read problems statements in Mandarin Chinese and Russian. Alok-nath is man of equality. He needs your help to divide his “sanskars” evenly amongst all his followers. By doing this, Alok-nath can create equality amongst his followers and he'll be called a true “sanskari”. Alok-nath has N sanskars, and K followers. E...
def hasSubsetSum(A, V): if V == 0: return True, [] if len(A) == 0: return False, [] if V < A[len(A) - 1]: return hasSubsetSum(A[0 : len(A) - 1], V) else: L = [len(A) - 1] TMP = hasSubsetSum(A[0 : len(A) - 1], V - A[len(A) - 1]) if TMP[0]: TMP[1...
FUNC_DEF IF VAR NUMBER RETURN NUMBER LIST IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER LIST IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_...
Read problems statements in Mandarin Chinese and Russian. Alok-nath is man of equality. He needs your help to divide his “sanskars” evenly amongst all his followers. By doing this, Alok-nath can create equality amongst his followers and he'll be called a true “sanskari”. Alok-nath has N sanskars, and K followers. E...
from itertools import chain, combinations def list_powerset(lst, k): result = [[]] for x in lst: result.extend([(subset + [x]) for subset in result if sum(subset) + x <= k]) return result def powerset(iterable, k): xs = iterable return chain.from_iterable(combinations(xs, n) for n in ran...
FUNC_DEF ASSIGN VAR LIST LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR BIN_...
Read problems statements in Mandarin Chinese and Russian. Alok-nath is man of equality. He needs your help to divide his “sanskars” evenly amongst all his followers. By doing this, Alok-nath can create equality amongst his followers and he'll be called a true “sanskari”. Alok-nath has N sanskars, and K followers. E...
import sys sys.setrecursionlimit(10**6) book = dict() a = [] n = kk = rs0 = 0 def outc(k, j, v, rs): if (k, j, tuple(v)) in book.keys() and rs == rs0: return book[k, j, tuple(v)] elif rs != 0 and j != n: res = False if not v[j]: if a[j] <= rs: w = v.copy() ...
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUM...
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in th...
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: res = set() tot = set() for a in A: res = {(prev | a) for prev in res} | {a} tot |= res return len(tot)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in th...
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = set(A) prev = set([A[0]]) for i in A[1:]: temp = set() for p in prev: temp.add(i | p) ans.add(i | p) prev = temp prev.add(i) retur...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in th...
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: s = set() s1 = set() for n in A: s2 = set() for e in s1: s2.add(e | n) s1 = s2 s1.add(n) s |= s1 return len(s)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in th...
class Solution: def subarrayBitwiseORs(self, arr: List[int]) -> int: ans = set() current = set() current.add(0) for i in range(len(arr)): a = arr[i] s = set() s.add(a) for y in current: s.add(a | y) current ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR IF VAR RETURN NU...
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in th...
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans, frontier = set(), set() for n in A: frontier = {(x | n) for x in frontier if x | n != -1} frontier.add(n) ans |= frontier return len(ans)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in th...
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: arr_len = len(A) final_result_set = set() pre_result_set = set() pre_result_set.add(A[0]) final_result_set.add(A[0]) for i in range(1, arr_len): cur_result_set = set() for pre_...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUN...