description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: def prime_factors(num): pfactors = [] factor = 2 while num >= factor * factor: if num % factor == 0: pfactors.append(factor) num = num // factor ...
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBE...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: n = max(A) ans = 0 uf = UF(n) count = collections.Counter() for a in A: for num in range(2, int(sqrt(a) + 1)): if a % num == 0: uf.union(a, num) ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR VAR ASS...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int], N=100001) -> int: m = {} P = [i for i in range(N)] L = [1] * N def find(x): P[x] = P[x] if x == P[x] else find(P[x]) return P[x] def union(a, b): a = find(a) b = fi...
CLASS_DEF FUNC_DEF VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR VAR VA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DSU: def __init__(self, N): self.data = list(range(N)) def union(self, x, y): xp, yp = self.find(x), self.find(y) self.data[xp] = yp def find(self, x): if self.data[x] != x: self.data[x] = self.find(self.data[x]) return self.data[x] class Soluti...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
def sieve(n): if n < 3: return yield 2 p = set(range(5, n, 2)) i = 3 while i < n: yield i p.difference_update(list(range(i * i, n, i * 2))) for i in range(i + 2, n, 2): if i in p: p.remove(i) break else: ...
FUNC_DEF IF VAR NUMBER RETURN EXPR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN CLAS...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: n = max(A) + 1 parent = [i for i in range(n)] rank = [0] * n for a in A: for k in range(2, int(sqrt(a)) + 1): if a % k == 0: self.union(parent, a, k, rank) ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_O...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: n = len(A) if n < 2: return n A = set(A) if 1 in A: A.remove(1) if len(A) == 0: return 1 def list_primes(m): p = [True] * (m + 1) for i i...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DSU: def __init__(self, n: int): self.n = n self.root = list(range(n)) self.size = [1] * n self.max = 1 def find(self, x: int) -> int: if self.root[x] != x: self.root[x] = self.find(self.root[x]) return self.root[x] def union(self, x: int,...
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR V...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def primeFactors(self, n): pset = set() while n % 2 == 0: pset.add(2) n = n // 2 for i in range(3, int(math.sqrt(n)) + 1, 2): while n % i == 0: pset.add(i) n = n // i if n > 2: pset.add(n...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
def union(parent, rank, i, j): parI = find(parent, i) parJ = find(parent, j) if parI == parJ: return if rank[parI] < rank[parJ]: parent[parI] = parJ elif rank[parI] > rank[parJ]: parent[parJ] = parI else: parent[parJ] = parI rank[parI] += 1 def find(pare...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def primeDecompose(self, num): factor = 2 prime_factors = [] while num >= factor * factor: if not num % factor: prime_factors.append(factor) num //= factor else: factor += 1 prime_factors.append(...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A): n = len(A) primes = collections.defaultdict(list) parents = [i for i in range(n)] def find(x): if parents[x] != x: return find(parents[x]) return parents[x] def union(x, y): ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: n = len(A) record = [-1] * n primes = collections.defaultdict(list) def get_primes(i): for j in range(2, int(math.sqrt(i) + 1)): if i % j == 0: return get_primes(i /...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR LIST ...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DSU: def __init__(self, nums): self.nums = [i for i in range(len(nums))] self.cnt = [1] * len(nums) def find(self, idx): if self.nums[idx] == idx: return idx pidx = self.find(self.nums[idx]) self.nums[idx] = pidx return pidx def union(self...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Node: def __init__(self, data): self.data = data self.p = None class Solution: def find(self, node): if node.p == node: return node node.p = self.find(node.p) return node.p def prime(self, val, d): for i in range(2, int(val**0.5) + 1): ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF FUN...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: graph = defaultdict(list) uf = UnionFind(len(A)) for i, num in enumerate(A): prime_factors = set(self.get_prime_factors(num)) for factor in prime_factors: graph[factor].append(i) ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR V...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class UnionFindSet: def __init__(self, n): self.parants = [0] * n self.rank = [0] * n for i in range(n): self.parants[i] = i def find(self, x): if x != self.parants[x]: self.parants[x] = self.find(self.parants[x]) return self.parants[x] def ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN V...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DS: def __init__(self, N: int): self.p = list(range(N)) self.size = [1] * N self.max = 1 def find(self, node: int) -> int: if self.p[node] != node: self.p[node] = self.find(self.p[node]) return self.p[node] def union(self, x: int, y: int) -> None:...
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR V...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DSU: def __init__(self, n): self.parent = [i for i in range(n)] self.rank = [(0) for _ in range(n)] self.size = n def find(self, x): if x != self.parent[x]: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, x, y): ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def __init__(self): self.prime = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBE...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, nums: List[int]) -> int: uf = {} def find(x): if uf[x] != x: uf[x] = find(uf[x]) return uf[x] def union(x, y): uf.setdefault(x, x) uf.setdefault(y, y) uf[find(x)] = u...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DisjointSet: def __init__(self, size): self.parents = list(range(size)) self.sizes = [1] * size def find(self, x): if self.parents[x] != x: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): parent_x = self....
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN V...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def primeDecompose(self, num): factor = 2 prime_factors = [] while num >= factor * factor: if num % factor == 0: prime_factors.append(factor) num = num // factor else: factor += 1 prime_factors.a...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DisjointSet: def __init__(self, n): self.parent = [i for i in range(n)] self.rank = [(0) for i in range(n)] def GetParent(self, i): if self.parent[i] != i: self.parent[i] = self.GetParent(self.parent[i]) return self.parent[i] def Union(self, i, j): ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: def get_primes_set(n): for i in range(2, int(math.sqrt(n)) + 1): if n % i == 0: return get_primes_set(n // i) | set([i]) return set([n]) A = set(A) primes = set...
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR V...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: def sieve(): i = 2 while i < len(spf): spf[i] = 2 i += 2 i = 3 while i * i < len(spf): if spf[i] == i: j = i ...
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class UFDS: def __init__(self, n, nums): self.p = list(range(n + 1)) self.size = [0] * (n + 1) for num in nums: self.size[num] = 1 def find(self, n): if n != self.p[n]: self.p[n] = self.find(self.p[n]) return self.p[n] def union(self, i, j):...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL 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 V...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: rank = collections.defaultdict(int) parent = collections.defaultdict(int) def find(a): if parent[a] == 0: return a if parent[a] != a and parent[a] != 0: parent[a] = ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NU...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class UnionFind: def __init__(self, nums): self.nums = nums self.groups = list(range(len(nums))) self.sizes = [1] * len(nums) def union(self, a, b): a, b = self.find(a), self.find(b) if a == b: return if self.sizes[a] < self.sizes[b]: a, ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL 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 VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DSU: def __init__(self, n): self.parent = [i for i in range(n)] self.rank = [(1) for i in range(n)] def find(self, x): if x != self.parent[x]: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, x, y): px = self.find(x...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class UF: def __init__(self, n): self.p = [i for i in range(n)] self.s = [1] * n def find(self, x): if self.p[x] != x: self.p[x] = self.find(self.p[x]) return self.p[x] def union(self, x, y): xr, yr = self.find(x), self.find(y) self.s[xr] += sel...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR V...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class UnionFind: def __init__(self, nodes): self.parents = [-1] * len(nodes) self.map = {node: idx for idx, node in enumerate(nodes)} def find(self, node): idx = self.map[node] orig = idx while self.parents[idx] >= 0: idx = self.parents[idx] parent, ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: n = len(A) dsu = DSU(n) prime_dict = defaultdict(list) for i, num in enumerate(A): primes = self.find_p(num) for p in primes: prime_dict[p].append(i) for _, p_list in...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBE...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: class UF: def __init__(self, N): self.parents = [i for i in range(N)] self.sizes = [(1) for _ in range(N)] self.max = 1 def find(self, x): if self....
CLASS_DEF FUNC_DEF VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Union: def __init__(self, arr): self.weight = {} self.par = {} for a in range(1, max(arr) + 1): self.par[a] = a self.weight[a] = 1 def find(self, a): while self.par[a] != a: a = self.par[a] return a def union(self, a, b): ...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR RETURN 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 VA...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class ds: def __init__(self, n): self.arr = list(range(n)) self.size = [1] * n def find(self, x): if self.arr[x] != x: self.arr[x] = self.find(self.arr[x]) return self.arr[x] def union(self, x, y): Ax = self.find(x) Ay = self.find(y) if ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class UF: def __init__(self) -> None: self.parent = {} self.sz = {} self.count = 0 def add(self, p): if p not in self.parent: self.parent[p] = p self.sz[p] = 1 self.count += 1 def find(self, p: int) -> int: while p != self.parent...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER NONE FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FUNC_DEF VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
isp = [(True) for i in range(100000)] pr = [] for i in range(2, 100000): c = i if isp[i]: pr.append(i) while c < 100000 - i: c += i isp[c] = False class Solution: def largestComponentSize(self, A: List[int]) -> int: def pcr(n): r = n**0.5 ...
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR IF VAR NUMBER E...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DisjSet: def __init__(self, n): self.disj_set = [-1] * n def find(self, x): while self.disj_set[x] >= 0: x = self.disj_set[x] return x def union(self, x, y): i = self.find(x) j = self.find(y) if i == j: return if self.d...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DSU: def __init__(self, size): self.indexes = {i: i for i in range(size)} self.sizes = {i: (1) for i in range(size)} def root(self, i): node = i while i != self.indexes[i]: i = self.indexes[i] while node != i: nnode = self.indexes[node] ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: n = len(A) dic = defaultdict(list) def primefactor(x): s = [] y = x if x % 2 == 0: s.append(2) while x % 2 == 0: x //= 2 for i in...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMB...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: amax = max(A) plist = [2] for k in range(3, int(sqrt(amax)) + 2, 2): for p in plist: if p * p > k: plist.append(k) break if k % p == 0: ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DSU: def __init__(self, N): self.p = list(range(N)) def find(self, x): if self.p[x] != x: self.p[x] = self.find(self.p[x]) return self.p[x] def union(self, x, y): xr = self.find(x) yr = self.find(y) self.p[xr] = yr class Solution(object)...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR CLASS_DEF VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_O...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: parent = {} size = defaultdict(lambda: 1) def ds(a): while a in parent: a = parent[a] return a def union(a, b): if (a := ds(a)) != (b := ds(b)): ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: fa = [i for i in range(len(A))] sz = [1] * len(A) def find(x): if fa[x] != x: fa[x] = find(fa[x]) return fa[x] factor = defaultdict(list) for i in range(len(A)): ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER B...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: n = len(A) parent = [i for i in range(max(A) + 1)] def find(x): if parent[x] == x: return x parent[x] = find(parent[x]) return parent[x] def union(x, y): ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR RETU...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class DSU: def __init__(self, n): self.parent = list(range(n)) def find(self, target): if self.parent[target] != target: self.parent[target] = self.find(self.parent[target]) return self.parent[target] def union(self, c, p): self.parent[self.find(c)] = self.find...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL V...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: parent = dict() def find(x): parent.setdefault(x, x) if x != parent[x]: parent[x] = find(parent[x]) return parent[x] def union(x, y): px = find(x) ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: def pfactorization(a): N = a if a >= 2 and a % 2 == 0: Gp[2].append(N) Gn[N].append(2) while a % 2 == 0: a //= 2 for d in range(3, ma...
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER NUMBER IF VAR VAR IF BIN_OP VAR VAR NUM...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: parents = {val: val for val in A} sizes = {val: (1) for val in A} factorprez = {} maxsize = 0 def union(one, two): r1 = findroot(one) r2 = findroot(two) if r1 == r2: ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF ...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: parent = {} def find(a): if a not in parent: parent[a] = a return a if parent[a] == a: return a parent[a] = find(parent[a]) return pa...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: parent = {} size = {} def find(x): if parent[x] != x: parent[x] = find(parent[x]) return parent[x] def union(rootlist): if not rootlist: return ...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR RETURN FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR FU...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: d = {} for i in range(len(A)): for j in range(2, int(math.sqrt(A[i]) + 1)): if A[i] % j == 0: if j not in d: d[j] = [] if A[i] // j not in...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def largestComponentSize(self, A: List[int]) -> int: ma = max(A) N = len(A) m = list(range(ma + 1)) for a in A: for k in range(2, int(math.sqrt(a)) + 1): if a % k == 0: self.u(m, a, k) self.u(m, a, a...
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP V...
Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the largest connected component in the graph.   ...
class Solution: def get_factors(self, n: int): factors = set() for factor in self.total_factors: if factor > n: break if n % factor == 0: factors.add(factor) for factor in factors: self.total_factors.remove(factor) ...
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CAL...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): li = matrix.pop(0) while len(matrix): si = [] for i in zip(*matrix): si.append(i) matrix = si[::-1] li += matrix.pop(0) return li
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER RETURN VAR
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, a, m, n): res = [] k = 0 l = 0 while k < m and l < n: for i in range(l, n): res.append(a[k][i]) k += 1 for i in range(k, m): res.append(a[i][n - 1]) n -= 1 ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): def spirallyTraverseUtil(matrix, out, startr, endr, startc, endc): if startr > endr or startc > endc: return if endr == startr: for i in range(startc, endc + 1): out.append...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_C...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): l = [] startc = 0 startr = 0 endc = c - 1 endr = r - 1 while startr <= endr and startc <= endc: for i in range(startc, endc): l.append(matrix[startr][i]) for i in range(...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): left = 0 right = c - 1 top = 0 bottom = r - 1 ans = [] while left <= right and top <= bottom: for i in range(left, right + 1): ans.append(matrix[top][i]) top += 1 ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMB...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): sr, sc = 0, 0 er, ec = len(matrix) - 1, len(matrix[0]) - 1 res = [] while sr <= er and sc <= ec: for i in range(sc, ec + 1): res.append(matrix[sc][i]) for i in range(sr + 1, er + 1): ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL ...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): start_row = 0 start_col = 0 end_row = r - 1 end_col = c - 1 ans = [] while len(ans) < r * c and start_row <= end_row and start_col <= end_col: for i in range(start_col, end_col + 1): ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR F...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, m, r, c): a = [] count = 0 up = 0 left = 0 right = c - 1 down = r - 1 while count < r * c: for i in range(left, right + 1): if count >= r * c: break a.a...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): top, bottom, left, right = 0, r - 1, 0, c - 1 direction = 0 result = [] while top <= bottom and left <= right: if direction == 0: for i in range(left, right + 1): result.append(...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): ans = [] rowStart, rowEnd = 0, r - 1 columnStart, columnEnd = 0, c - 1 while rowStart <= rowEnd and columnStart <= columnEnd: for i in range(columnStart, columnEnd + 1): ans.append(matrix[rowStart]...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR ...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): u = 0 d = r - 1 l = 0 ri = c - 1 count = 0 i = 0 j = 0 k = 0 res = [] p = r * c - 1 while count < p: while count < r * c and j < ri: res.appe...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR WHILE VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR V...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): current_row = 0 current_column = -1 lst = [] while r > 0 and c > 0: count = 0 while count < c: current_column += 1 count += 1 lst.append(matrix[current_r...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMB...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): ROWS = list(range(r)) COLS = list(range(c)) res = [] while ROWS and COLS: rowStart = ROWS.pop(0) for col in COLS: res.append(matrix[rowStart][col]) colEnd = COLS.pop() ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VA...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): res = [] while matrix: res += matrix.pop(0) matrix = list(zip(*matrix))[::-1] return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): res = [] if len(matrix) == 0: return [] m = len(matrix) di = 0 row = [0, 1, 0, -1] col = [1, 0, -1, 0] x = 0 y = 0 seen = [[(0) for i in range(c)] for j in range(m)] ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): ans = [] if len(matrix) == 0: return ans m = len(matrix) n = len(matrix[0]) seen = [[(0) for i in range(n)] for j in range(m)] dr = [0, 1, 0, -1] dc = [1, 0, -1, 0] x = 0 y ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NU...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): x1, y1 = 0, 0 x2, y2 = r - 1, c - 1 ans = [] i, j = 0, 0 p = 0 while p < r * c: if i == x1 and j != y2: ans.append(matrix[i][j]) j += 1 elif j == y2 and ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR ...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): n = r * c d = 1 p = 0 + 0.0j x0, x1 = 0, c - 1 y0, y1 = 0, r - 1 ans = [] while n > 0: y, x = int(p.imag), int(p.real) ans.append(matrix[y][x]) if d == 1 + 0.0j and ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER VA...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): top = 0 down = r left = 0 right = c d = 0 l = [] while top < down and left < right: if d == 0: for i in range(left, right): l.append(matrix[top][i]) ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): flattened_matrix = [] while matrix: try: r1 = matrix.pop(0) for i in r1: flattened_matrix.append(i) except: pass try: for...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VA...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): start_row_ind = 0 start_col_ind = 0 end_row_ind = r - 1 end_col_ind = c - 1 count = 0 total_elements = r * c res = [] while count < total_elements: i = start_col_ind whi...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): ans = [] top = -1 right = c left = -1 bottom = r i = 0 j = 0 while top < bottom and left < right: while j < right: ans.append(matrix[i][j]) j += 1 ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR EXPR FUNC_C...
Given a matrix of size r*c. Traverse the matrix in spiral form. Example 1: Input: r = 4, c = 4 matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15,16}} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Explanation: Example 2: Input: r = 3, c = 4 matrix[][] = {{1, 2, 3...
class Solution: def spirallyTraverse(self, matrix, r, c): i = r j = c r = 0 c = 0 ans = [] direction = [[0, 1], [1, 0], [0, -1], [-1, 0]] reverse = [[1, -1], [-1, -1], [-1, 1], [1, 1]] order = 0 def outOfBound(r, c): return r < 0 ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF RETURN V...
Shashank loves trees and math. He has a rooted tree, $\mathbf{T}$, consisting of $N$ nodes uniquely labeled with integers in the inclusive range $[1,N]$. The node labeled as $1$ is the root node of tree $\mathbf{T}$, and each node in $\mathbf{T}$ is associated with some positive integer value (all values are initially ...
mod = 10**9 + 7 cache = {(0): (0, 1), (1): (1, 1)} def fib_pair(n): if n in cache: return cache[n] hn = n // 2 f, fp = fib_pair(hn) if n & 1: res = (fp * fp + f * f) % mod, (2 * f + fp) * fp % mod else: fm = fp - f if fm < 0: fm += mod res = (2 *...
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR ...
You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word. (Recall that a permutation of letters is a bijection from...
class Solution: def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]: def encodeWord(word): d, nextChar, i, encoding = ( {}, iter([chr(ord("a") + i) for i in range(26)]), 0, [], ) wh...
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR DICT FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR NUMBER NUMBER LIST WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUM...
You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word. (Recall that a permutation of letters is a bijection from...
class Solution: def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]: out = [] for word in words: pat_dict = dict() used = set() if len(word) == len(pattern): can_be = True for i in range(len(word)): ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR V...
You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word. (Recall that a permutation of letters is a bijection from...
class Solution: def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]: plen = len(pattern) pat = [-1] * plen pat[0] = 0 for i in range(1, plen): for j in range(i): if pattern[j] == pattern[i]: pat[i] = j ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL ...
You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word. (Recall that a permutation of letters is a bijection from...
class Solution: def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]: def match(list1, pattern): for i in range(len(pattern)): if list1[i] != pattern[i]: return False return True def number_arr(list1): ...
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_...
You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word. (Recall that a permutation of letters is a bijection from...
class Solution: def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]: ans = [] for i in words: temp = i if len(pattern) == len(temp): as1 = {} got = [] found = 1 for i1 in range(len(temp)...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC...
You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word. (Recall that a permutation of letters is a bijection from...
class Solution: def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]: output = [] for word in words: fwd, reverse, valid = defaultdict(str), defaultdict(str), True for i in range(len(word)): if word[i] not in fwd and pattern[i] not in ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR IF VAR EXPR FUNC_CAL...
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(a, n): l = [] r = [] for i in a: if i < 0: l.append(i) else: r.append(i) x = len(l) for i in range(n): if i < x: a[i] = l[i] else: a[i] = r[i - x]
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(arr, n): current_index = 0 ar = [] for i in range(n): if arr[i] < 0: p = arr[i] arr[i] = arr[current_index] arr[current_index] = p current_index += 1 else: ar.append(arr[i]) count = 0 for j in range(current_ind...
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def merge(arr, l, m, r): i, j, k = 0, 0, 0 n1 = m - l + 1 n2 = r - m L = [arr[l + i] for i in range(n1)] R = [arr[m + 1 + j] for j in range(n2)] i = 0 j = 0 k = l while i < n1 and L[i] < 0: arr[k] = L[i] k += 1 i += 1 while j < n2 and R[j] < 0: arr...
FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN ...
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(a, n): x = [] x[:] = a[:] a.clear() index = 0 for i in range(len(x)): if x[i] < 0: a.insert(index, x[i]) index += 1 else: a.append(x[i])
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(a, n): neg = 0 for i in range(n): if a[i] < 0: c = a.pop(i) a.insert(neg, c) neg += 1 return a
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(a, n): negative = [] positive = [] for i in a: if i < 0: negative.append(i) else: positive.append(i) a[:] = negative + positive
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(a, n): list1 = [] list2 = [] for i in a: if i < 0: list1.append(i) else: list2.append(i) ans = list1 + list2 for i in range(n): a[i] = ans[i] return a
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(arr, n): negative = [] positive = [] for i in range(n): if arr[i] < 0: negative.append(arr[i]) else: positive.append(arr[i]) negative.extend(positive) arr[:] = negative[:]
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(a, n): l1 = [] for i in a: if i < 0: l1.append(i) for i in a: if i >= 0: l1.append(i) for i in range(n): a[i] = l1[i]
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(arr, N): p = [] n = [] for i in range(N): if arr[i] >= 0: p.append(arr[i]) else: n.append(arr[i]) for i in range(len(n)): arr[i] = n[i] for i in range(len(p)): arr[i + len(n)] = p[i] return arr
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(a, n): b = [] c = [] i = 0 while i < n: if a[i] >= 0: b.append(a[i]) else: c.append(a[i]) i += 1 j = 0 for i in c: a[j] = i j += 1 for i in b: a[j] = i j += 1
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(a, n): l = [] l.extend(a) k = [] z = [] a.clear() for i in l: if i < 0: z.append(i) else: k.append(i) z.extend(k) a.extend(z)
FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(a, n): po = [] ne = [] for i in range(n): if a[i] < 0: ne.append(a[i]) else: po.append(a[i]) for i in range(n): if i < len(ne): a[i] = ne[i] else: a[i] = po[i - len(ne)]
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers. (Maintain the order of all -ve and non-negative numbers as given in the original array). Example 1: Input: N = 4 Arr[] = {-3, 3, -2, 2} Output: -3 -2 3 2 Explanation: In the giv...
def Rearrange(a, n): l = [] x = [] for i in a: if i < 0: l.append(i) else: x.append(i) b = [] b.extend(l) b.extend(x) for i in range(0, len(b)): a[i] = b[i] return a
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR