description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
import sys def main(): ans = 0 n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split(" "))) store = {} power = {} for k, v in enumerate(a): store[v] = store.get(v, 0) + 1 power[1] = 2 for i in range(2, 32): power[i] = power[i - 1] * 2 for k, v i...
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUM...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) array = list(map(int, input().split())) d = {} powers = [(2**i) for i in range(1, 32)] s = 0 for i in range(n): e = array[i] if i > 0: for p in powers: if p > e: s += d.get(p - e, 0) if e not in d: d[e] = 0 d[e] += 1 print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER FOR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMB...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
def s(): input() res = 0 ss = dict() nums = [1] while nums[-1] < 10**9: nums.append(nums[-1] * 2) for i in map(int, input().split()): for j in nums: if j - i in ss: res += ss[j - i] if i in ss: ss[i] += 1 else: s...
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER WHILE VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR ...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
input() a = list(map(int, input().split())) count = {} ret = 0 for num in a: for shift in range(32): p = 1 << shift need = p - num if need <= 0: continue ret += count.get(need, 0) count.setdefault(num, 0) count[num] += 1 print(ret)
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL V...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) array = [int(i) for i in input().split()] ans = 0 for j in range(32): num = 2**j diction = dict() for i in array: if i in diction: ans += diction[i] if num - i > 0: if num - i in diction: diction[num - i] += 1 else: ...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) arr = list(map(int, input().split())) count = 0 maxA = 2 * 10**9 compare = [] x = 2 dic = {} done = {} while x < maxA: compare.append(x) x <<= 1 for i in range(n): text = str(arr[i]) if text in dic: dic[text] = dic.get(text) + 1 else: dic[text] = 1 for num in arr: ...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
import sys input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) ans = 0 mp = {} n = inp() for i in map(int, input().split()): for k...
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) l = list(map(int, input().split())) dic = {} for i in range(n): x = l[i] dic[x] = 0 for i in range(n): x = l[i] dic[x] += 1 s = max(l) m = 0 num = 0 while 2**m <= s: m += 1 for i in range(n): a = l[i] for j in range(1, m + 1): b = 2**j - a if b in dic: ...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE ...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
pows = [] x = 1 for i in range(32): pows.append(x) x *= 2 def countPairs(n, d): count = 0 x = 0 m = 0 for i in d.keys(): if i in pows: m += d[i] while x < 32: curr = pows[x] for i in d.keys(): if curr - i in d.keys(): count +=...
ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BI...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
def powers_of_two(n, a): c = {} s = 0 for x in a: for p in range(32): s += c.get((1 << p) - x, 0) c[x] = c.get(x, 0) + 1 return s n = int(input()) a = map(int, input().split()) print(powers_of_two(n, a))
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_C...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) arr = [int(x) for x in input().strip().split(" ")] pws = [(2**x) for x in range(33)] map = {} ans = 0 for v in arr: for p in pws: if p - v in map: ans = ans + map[p - v] if v in map: map[v] = map[v] + 1 else: map[v] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) l = list(map(int, input().split())) d = {} k = 0 for i in range(n): for j in range(1, 32): m = 2**j f = -1 try: d[m - l[i]] f = 0 except KeyError: pass if f == 0: k += d[m - l[i]] f = -1 try: d[l...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER EXPR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER VAR VAR...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) a = [int(i) for i in input().split()] a.sort() found, count, res = {}, 1, 0 for j in a: while 2**count <= j: count += 1 left = 2**count - j try: res += found[left] except: pass try: found[j] += 1 except: found[j] = 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR DICT NUMBER NUMBER FOR VAR VAR WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) c = list(map(int, input().split())) p = list() for i in range(1, int(1000000000.0)): if 2**i > int(1000000000.0) * 2: break p.append(2**i) v = dict() d = dict() for x in c: for y in p: if 2 * x == y: d[x] = d.get(x, 0) + 1 continue v[y - x] = ...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL ...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) a = [int(n) for n in input().split()] count = 0 d = {} for i in range(0, n): for j in range(1, 31): if 2**j - a[i] in d: count += d[2**j - a[i]] if a[i] in d: d[a[i]] += 1 else: d[a[i]] = 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) a = [int(t) for t in input().split()] counts = dict() for num in a: counts[num] = counts.setdefault(num, 0) + 1 m = max(counts) pows = [2] while pows[-1] < m: pows.append(2 * pows[-1]) pows.append(2 * pows[-1]) total = 0 for num in a: for p in pows: if p - num in counts: ...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL V...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) a = list(map(int, input().split())) dic = {} for i in a: if i in dic: dic[i] += 1 else: dic[i] = 1 div = 0 for i in range(n): dic[a[i]] -= 1 for j in range(1, 32): if 2**j - a[i] in dic: div += dic[2**j - a[i]] print(abs(div))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR VAR...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) a = [int(x) for x in input().split()] b = {} for t in a: if t in b: b[t] += 1 else: b[t] = 1 dva = [] for i in range(31): dva.append(2**i) m = 0 for d in dva: for k in list(b.keys()): if k < d: if not k == d // 2: if d - k in b: ...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input().strip()) d = {} for i in input().strip().split(): x = int(i) if x in d: d[x] += 1 else: d[x] = 1 ans = 0 for i in d.keys(): for j in range(1, 31): x = 2**j - i if x < i: continue elif x == i: ans += d[i] * (d[i] - 1) // 2 ...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR IF VAR VA...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
def solution(): n = int(input()) a = list(map(int, input().split())) pow2 = [(2**i) for i in range(61)] total = 0 dic = {} for i in range(n): for j in range(60): if pow2[j] - a[i] in dic: total += dic[pow2[j] - a[i]] if a[i] in dic: dic[a[i...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR I...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
import sys input() a = list(map(int, sys.stdin.readline().split())) b = {} for i in a: if i not in b: b[i] = 1 else: b[i] += 1 ans = 0 s = 0 for i in set(a): for j in range(0, 32): y = 2**j - i if y == i and i in b: s += b[i] * (b[i] - 1) // 2 elif y in b...
IMPORT EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR VA...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) a = sorted([int(i) for i in input().split()]) d = {} for i in range(n): if a[i] in d: d[a[i]] += 1 else: d[a[i]] = 1 ans = 0 for i in range(32): t = 2**i for j in d: if t - j in d: if j == t - j: ans += (d[j] - 1) * d[j] el...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR IF BIN_OP VAR VAR VA...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) arr = [int(z) for z in input().split()] arr.sort() occ = {} for i in set(arr): occ[i] = 0 for i in arr: occ[i] += 1 res = 0 for power in range(32): for i in arr: if not occ.get(2**power - i): continue if 2**power - i == i: res += occ[i] - 1 el...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP...
You are given n integers a_1, a_2, ..., a_{n}. Find the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2 (i. e. some integer x exists so that a_{i} + a_{j} = 2^{x}). -----Input----- The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers. The second li...
n = int(input()) cnt = dict() res = 0 for x in input().split(): x = int(x) for pw in [(2**j) for j in range(32)]: if pw - x in cnt: res += cnt[pw - x] if x in cnt: cnt[x] += 1 else: cnt[x] = 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations. Max is a young biologist. For $n$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the d...
class Union: def __init__(self, n): self.p = {i: i for i in range(n)} self.rank = {i: (1) for i in range(n)} def find(self, x): if x < 0: return x if self.p[x] != x: self.p[x] = self.find(self.p[x]) return self.p[x] def union(self, x, y): ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VA...
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations. Max is a young biologist. For $n$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the d...
def main(): n = int(input()) ori_a = [int(x) for x in input().split()] pos = {ori_a[i]: i for i in range(len(ori_a))} seg = [dict(left=None, right=None) for i in range(len(ori_a))] a = sorted(ori_a) seg_size = 0 seg_num = 0 fi_k = a[0] fi_seg_num = 0 for n_items, item in enumerat...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NONE NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBE...
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations. Max is a young biologist. For $n$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the d...
BigNum = 10**10 n = int(input()) aa = [BigNum] + list(map(int, input().split(" "))) + [BigNum] appear = sorted([(v, i) for i, v in enumerate(aa)]) ans = -1 maxLocations = 0 intervals = [(i, i) for i in range(len(aa))] lengths = {} def incCount(val): nonlocal lengths lengths[val] = lengths.get(val, 0) + 1 de...
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL V...
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations. Max is a young biologist. For $n$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the d...
class UnionFind: def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = s...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR NUMBER 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 IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ...
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations. Max is a young biologist. For $n$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the d...
def union(x, y, a, each): x1 = x y1 = y while a[x] > 0: x = a[x] while a[y] > 0: y = a[y] each[-1 * a[x]] -= 1 each[-1 * a[y]] -= 1 each[-1 * (a[x] + a[y])] += 1 if a[x] > a[y]: a[y] += a[x] a[x] = y res = y else: a[x] += a[y] a...
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR AS...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def solve(one, zero, n, op, res): if n == 0: res.append(op) return op1 = op op1 = op1 + "1" solve(one + 1, zero, n - 1, op1, res) if one > zero: op2 = op ...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN ASSIGN V...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): frontier = [["1", 1, 0]] lvl = 1 while lvl < N: nxt = [] for i in frontier: nxt.append([i[0] + "1", i[1] + 1, i[2]]) if i[2] + 1 <= i[1]: nxt.append([i[0] + "0", i[1], i[2] +...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST STRING NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER STRING VAR NUMBER BIN_OP VAR NUMB...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def helper(zero, one, li, n, output): if n == 0: li.append(output) return if one > zero: output2 = output + "1" helper(zero, one + 1, li, n - 1, output2) output1...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ones = 0 zeros = 0 ans = [] self.NBitBinaryHelper("", ones, zeros, N, ans) ans.sort(reverse=True) return ans def NBitBinaryHelper(self, s, ones, zeros, n, ans): if ones + zeros == n: if s[0] == "1" and...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR FUNC_DEF IF BIN_OP VAR VAR VAR IF VAR NUMBER STRING VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR VAR...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def rec(n, one, zero, curr, res): if one + zero == n: res.append(curr) return if one > n: return rec(n, one + 1, zero, curr + "1", res) if one > zero: re...
CLASS_DEF FUNC_DEF FUNC_DEF IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING VAR RETURN VAR
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ones = 0 zeros = 0 n = N res = "" ans = [] def solve(res, ones, zeros, n): if n == 0: ans.append(res) return elif ones == zeros: res1 = res + "1" ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_O...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): res = [] def helper(ones, zero, n, op): if n == 0: res.append(op) return helper(ones + 1, zero, n - 1, op + "1") if ones > zero: helper(ones, zero + 1, n - 1, op + "0") ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER VAR STRING RETURN VAR
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): sol = [] def backtrack(curr, ones, zeros): if len(curr) == N: sol.append("".join(curr)) return curr.append("1") backtrack(curr, ones + 1, zeros) curr.pop() if zeros ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CAL...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): res = [] def recur(cur, o, z, n, res): if n == 0: res.append(cur[:]) return if o == z: cur += "1" recur(cur, o + 1, z, n - 1, res) else: cur ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING EXPR...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ans = [] def print_binary(n, o, z, output): if n == 0: if o >= z: ans.append(output) return print_binary(n - 1, o + 1, z, "".join([output, "1"])) if z < o: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL STRING LIST VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL STRING LIST VAR STRING EXPR FUNC_CALL VAR VAR NUM...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): v = [] op = "" one = 0 zero = 0 def solve(one, zero, N, op): if N == 0: v.append(op) return op1 = op + "1" solve(one + 1, zero, N - 1, op1) if one > zero...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_O...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ans = [] def funtoprint(ones, zeros, n, op): if n == 0: ans.append(op) return op1 = op op1 += "1" funtoprint(ones + 1, zeros, n - 1, op1) if ones > zeros: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR STRING...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ans = [] self.solve(N, 0, 0, "", ans) return ans def solve(self, n, one, zero, op, ans): if n == 0: ans.append(op) return self.solve(n - 1, one + 1, zero, op + "1", ans) if one > zero: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING VAR RETURN VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def solve(one, zero, N, op, vec): if N == 0: vec.append("".join(op)) return op1 = op[:] op1.append("1") solve(one + 1, zero, N - 1, op1[:], vec) if one > zero: ...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSI...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ones = 0 zeros = 0 output = "" outlist = [] def solve(N, ones, zeros, output): if N == 0: outlist.append(output) return out1 = output out1 += "1" solve(N...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def helper(n, one, zero, op, res): if n == 0: res.append(op) return if one > zero: helper(n - 1, one + 1, zero, op + "1", res) helper(n - 1, one, zero + 1, op + "0", res) ...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR STRIN...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ones = 1 zeros = 0 op = "1" result = [] self.NBitBinary2(N - 1, ones, zeros, op, result) return result def NBitBinary2(self, N, ones, zeros, op, result): if N == 0: result.append(op) return...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def solver(n, temp, count1, count2, ind): if ind >= n: ans.append(temp) return if count1 < n: solver(n, temp + "1", count1 + 1, count2, ind + 1) if count2 < count1: ...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR STRING NUMBER NUMBER NUMBER ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ans = [] def AllNumber(noOne, noZero, number): if noOne + noZero == N: ans.append(number) return if noOne < N: AllNumber(noOne + 1, noZero, number + "1") if noZero < noOne: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING EXPR FUNC_CALL VAR NUMBER NUMBER STRING RETURN VAR
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def solve(ans, N, op, o, z): if len(op) == N: ans.append("".join(op)) return if o == z: op.append("1") solve(ans, N, op, o + 1, z) elif o > z: op1, o...
CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER V...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ans = [] ans = self.solve("1", ans, N, 1, 0) return ans def solve(self, op, ans, N, ones, zeros): if len(op) == N: ans.append(op) return ans if ones == zeros: self.solve(op + "1", ans, N, ones ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR VAR BIN_OP VAR NUMBER VAR EXPR FU...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def solve(self, i, N, on, ze, ans, lst=""): if i == N: ans.append(lst) return if on < N: lst += "1" self.solve(i + 1, N, on + 1, ze, ans, lst) lst = lst[: len(lst) - 1] if ze < on: lst += "0" ...
CLASS_DEF FUNC_DEF STRING IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_O...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ans = [] def help(N, c1, c0, str, ans): if len(str) == N + 1 and c1 >= c0: ans.append(str) return ans if c1 < c0: return help(N, c1 + 1, c0, str + "1", ans) help...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): res = [] def solve(outp: str): if len(outp) == N: res.append(outp) return op1 = outp op1 += "1" c1 = outp.count("1") c0 = outp.count("0") solve(op1) ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING RETURN VAR
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ip = n - 1 op = "1" ans = [] ones = 1 zeros = 0 def solve(ip, op, ones, zeros): if ip == 0: ans.append(op) return if ones > zeros: op1 = op + "1" ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EX...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def solve(self, res, ones, zeros, n, arr): if n == 0: arr.append(res) return if ones == zeros: self.solve(res + "1", ones + 1, zeros, n - 1, arr) else: self.solve(res + "1", ones + 1, zeros, n - 1, arr) self.solve(r...
CLASS_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUN...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): one = 1 zero = 0 op = "1" answer = [] self.Solve(one, zero, op, N - 1, answer) return answer def Solve(self, one, zero, op, N, answer): if N == 0: answer.append(op) return elif one ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ones = 0 zeros = 0 N = N ans = [] return self.solve(N, ones, zeros, "", ans) def solve(self, N, ones, zeros, op, ans): if ones + zeros == N: ans.append(op) return ans if ones > zeros: ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST RETURN FUNC_CALL VAR VAR VAR VAR STRING VAR FUNC_DEF IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR V...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ans = [] def helper(N, num, count_ones, count_zeroes): if N == 0: ans.append(int(num)) return helper(N - 1, num + "1", count_ones + 1, count_zeroes) if count_ones > count_zeroes: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR STRING BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING NUMBER NUMBER RETURN VA...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ip = N op = "" one = 0 zero = 0 l = [] ans = self.solve(ip, op, one, zero, l) return ans def solve(self, ip, op, one, zero, l): if ip == 0: l.append(op) return else: ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASS...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def solve(self, k, N, one, curr): if k == N: self.ans.append("".join(curr)) return curr.append("1") self.solve(k + 1, N, one + 1, curr) curr.pop() if one > 0: curr.append("0") self.solve(k + 1, N, one - 1, curr)...
CLASS_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_DEF ASSI...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ans = [] def fun(Z, O, op, N): if Z + O == N: ans.append(int(op)) return if O > Z: fun(Z + 1, O, op + "0", N) fun(Z, O + 1, op + "1", N) if O == Z: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def helperFunction(one_count, zero_count, generated_string): if len(generated_string) == N: return [generated_string] result = [] if one_count == zero_count: choice_1 = helperFunction( ...
CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN LIST VAR ASSIGN VAR LIST IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMB...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def prefixes(n, z, o, op, a): if n == 0: a.append(op) return if z <= o: op1 = op + "0" op2 = op + "1" prefixes(n - 1, z + 1, o, op1, a) prefixes(...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL V...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): result = [] def binary(s1, a, b): if len(s1) == N: result.append(s1) return binary(s1 + "1", a + 1, b) if b < a: binary(s1 + "0", a, b + 1) binary("", 0, 0) ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING NUMBER NUMBER RETURN VAR
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ans = [] def fun(one, zero, n, curr): if one + zero == n: if zero <= one: ans.append(curr) elif one <= zero and one + zero < n: fun(one + 1, zero, n, curr + "1") elif on...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING IF VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def solve(self, n, i, ans, out): if n == 0: ans.append(out) return self.solve(n - 1, i + 1, ans, out + "1") if i > 0: self.solve(n - 1, i - 1, ans, out + "0") def NBitBinary(self, N): i = 0 ans = [] out = "" ...
CLASS_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR V...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ans = [] ones = zeros = 0 op = "" def generate(N, ones, zeros, op): if N == 0: ans.append(op) return generate(N - 1, ones + 1, zeros, op + "1") if ones > zeros: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR VAR ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): output = "" ls = [] def slove(output, one, zero, n): if n == 0: ls.append(output) return if one != 0: slove(output + "1", one - 1, zero, n - 1) if zero > one: ...
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): out = 1 o = 1 z = 0 li = [] def binary(out, o, z): if z > o: return if o + z == N: li.append(out) return op1 = out * 10 + 1 op2 = out * 1...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP V...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): res = [] def solve1(op, one, zero, n): if one < zero: return if n == 0: res.append(op) return solve1(op + "1", one + 1, zero, n - 1) solve1(op + "0", one, zero +...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING NUMBER NUMBER VAR RETURN VAR
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ans = [] def generate(n, no_of_ones, no_of_zeros, op: str): if no_of_ones + no_of_zeros == n: ans.append(op) return if no_of_ones == no_of_zeros: op += "1" new_ones = no...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR STRING A...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): op = "" lis = [] one = 0 zero = 0 def solve(N, one, zero, op, lis): if N == 0: lis.append(op) return if one == zero: op += "1" solve(N - 1, one +...
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def printer(num, extra, remain): if remain == 0: ans.append(num) return printer(num + "1", extra + 1, remain - 1) if extra > 0: printer(num + "0", extra - 1, remain - 1) st...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): res = [] op = "1" ones = 1 zeros = 0 def helper(N, ones, zeros, res, op): if N == 0: if ones >= zeros: res.append(op) return if ones == zeros: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR STRING RETURN ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): s = "" arr = [] def generate_bin(s, n, arr): if n == 0: arr.append(s) return if s.count("1") > s.count("0"): s1 = s + "0" s2 = s + "1" generate_b...
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR STRI...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def solve(self, inp, out, zero, one, arr): if inp == 0: arr.append(out) return arr if zero == one: out += "1" arr = self.solve(inp - 1, out, zero, one + 1, arr) return arr out1 = out + "0" out2 = out + "1" ...
CLASS_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): return sorted(self.helper(N - 1, set([("1", 1)])), reverse=True) def helper(self, n, setti): if n == 0: return [item[0] for item in setti] temp = set() for item in setti: temp.add((item[0] + "1", item[1] + 1)) ...
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR LIST STRING NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NU...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def util(self, n, e, o, s): global ans if e + o > n: return if e + o == n: ans.append(s) return if e > o: self.util(n, e + 1, o, s + "1") self.util(n, e, o + 1, s + "0") else: self.util(n...
CLASS_DEF FUNC_DEF IF BIN_OP VAR VAR VAR RETURN IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR STRING FUNC_DEF ASSIGN VAR ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): ones = 0 zeros = 0 ans = [] def helper(output, ones, zeros): if len(output) == N: ans.append(output) elif ones <= zeros: helper(output + "1", ones + 1, zeros) elif ones > ze...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR BIN_OP VAR ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def fun(n_1, n_0, out, res, N): if n_1 + n_0 == N: res.append(out) return res if n_1 == n_0: n_s = out + "1" fun(n_1 + 1, n_0, n_s, res, N) elif n_1 > n_0: ...
CLASS_DEF FUNC_DEF FUNC_DEF IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VA...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
def N_bit(N, ind, on, ze, st, res): if ze > on: return if ind >= N: res.append(st) return N_bit(N, ind + 1, on + 1, ze, st + "1", res) N_bit(N, ind + 1, on, ze + 1, st + "0", res) class Solution: def NBitBinary(self, N): res = [] st = "" ind = 0 ...
FUNC_DEF IF VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR STRING VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR ...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): res = [] self.generate("", 0, 0, N, res) return res[::-1] def generate(self, curr, ones, zeros, N, res): if len(curr) == N: res.append(curr) return if ones > zeros: self.generate(curr + "0", on...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING NUMBER NUMBER VAR VAR RETURN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR VAR VAR
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): def solve(one, zero, n, st, res): if n == 0: res.append(st) return st1 = st st1 += "1" solve(one + 1, zero, n - 1, st1, res) if one > zero: st2 = st + "0" ...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN ASSIGN VAR STRING ASSIGN VAR LIST EXPR FU...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): op = "" count_zero = 0 count_one = 0 res = [] self.solver(op, count_zero, count_one, res, N) return res def solver(self, op, zero, one, res, N): if one + zero == N: res.append(op) return ...
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VA...
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): res = [] def recursive(s, one, zero): if one + zero == N: if s[0] != "0" and one >= zero: res.append(s) return if one < zero: return recursive(s + "1", o...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF BIN_OP VAR VAR VAR IF VAR NUMBER STRING VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING NUMBER NUMBER RETURN VAR
Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. Example 1: Input: N = 2 Output: 11 10 Explanation: 11 and 10 have more than or equal 1's than 0's Example 2: Input: N = 3 Output: 111 110 101 Explanation: 111, 110 and 10...
class Solution: def NBitBinary(self, N): res = [] def isonegreater(bins): countO = 0 countI = 0 for el in bins: if el == "1": countI += 1 elif el == "0": countO += 1 if count...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_C...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def findMaximumNum(self, s, k): s = list(s) def largest(ind): maxi = -float("inf") for i in range(ind, len(s)): if maxi < int(s[i]): maxi = int(s[i]) maxind = i return maxind res = ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER RETURN IF VAR FUNC_C...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def solve(self, idx, arr, k, n, largest): largest[0] = max(largest[0], "".join(i for i in arr)) if k == 0 or idx >= n: return maxc = max(arr[idx:]) for i in range(idx + 1, n): if arr[i] == maxc and arr[idx] != maxc: arr[i], arr...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL STRING VAR VAR VAR IF VAR NUMBER VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBE...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def findMaximumNum(self, s, k): lis = [] for i in s: lis.append(int(i)) n = len(lis) ans = [s] def rec(ind, n, k, lis, ans): if ind == n or k == 0: tem = "" for i in lis: tem += str(...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_DEF IF VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSI...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def findMaximumNum(self, s, k): def recursion(maxi, s, idx, k, n): if k == 0 or not s or idx >= n: return maxi mx = s[idx] for i in range(idx + 1, n): if int(s[i]) > int(mx): mx = s[i] if mx...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def findMaximumNum(self, s, k): sarr = list(s) return self.maxUtil(sarr, 0, k) def maxUtil(self, s, i, k): n = len(s) res = "".join(s) if k == 0 or i == n: return res maxj = i for j in range(i + 1, n): if s[j] > s[...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR FOR VAR FUNC_...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def findMaximumNum(self, s, k): temp = [i for i in s] self.m = list(temp) def sol(temp, k, idx): n = len(temp) if k == 0 or idx == n - 1: return max_elm = int(temp[idx]) curr = int(temp[idx]) for i ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR ...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def findMaximumNum(self, s, k): mxx = [s] n = len(s) string = [x for x in s] def fun(string, k, mxx, idx): if k == 0: return n = len(string) if idx == n: return mx = string[idx] ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER F...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def findMaximumNum(self, s, k): def swap(strg, k, ans, ind): if k <= 0 or ind >= n - 1: return maxchar = int(strg[ind]) for i in range(ind + 1, n): maxchar = max(maxchar, int(strg[i])) if maxchar != int(strg[in...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN V...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def findMaximumNum(self, s, k): def func(i, j, s1, k): if i == n or j == m or k == 0: return s1 if s1[i] == store[j]: return func(i + 1, j + 1, s1, k) key = "" for ind in range(0, n): key += str...
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER RETURN VAR IF VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP STRING FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR VAR ...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
def findMaximumNum(string, k, maxm, ctr): if k == 0 or ctr >= len(string): return n = len(string) mx = string[ctr] for i in range(ctr + 1, n): if int(string[i]) > int(mx): mx = string[i] if mx != string[ctr]: k = k - 1 for i in range(ctr, n): if string...
FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VA...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def recursion(self, curr, s, k, n, ans): if k == 0 or curr >= n: return mx = max(s[curr:]) if mx != s[curr]: k -= 1 for i in range(curr, n): if mx == s[i]: s[i], s[curr] = s[curr], s[i] if "".join(s)...
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL STRING VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR AS...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def findMaximumNum(self, s, k): maxval = ["-1"] n = len(s) def swaps(s, i, k, n): maxval[0] = max(maxval[0], s) if i >= n or k <= 0: return ind = i for x in range(i, n): if s[x] > s[ind]: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR V...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def findMaximumNum(self, s, k): n = len(s) arr = list(s) ans = [s] def rec(i, swaps): if i == n or swaps == k: ans[0] = max(ans[0], "".join(arr)) return m = arr[i] for j in range(i + 1, n): ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL STRING VAR RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC...
Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times. Example 1: Input: K = 4 str = "1234567" Output: 7654321 Explanation: Three swaps can make the input 1234567 to 7654321, swapping 1 with 7, 2 with 6...
class Solution: def findMaximumNum(self, string, k): self.result = string n = len(string) i = 0 self.solve(i, k, string, n) return self.result def solve(self, i, k, string, n): if k == 0 or i >= n: return None maxch = string[i] for ch...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NONE ASSIGN VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR V...