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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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 in enumerate(a):
store[v] -= 1
for i in range(1, 32):
if power[i] > v and power[i] - v in store:
ans += store[power[i] - v]
return ans
answer = main()
print(answer) | 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 NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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 NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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:
ss[i] = 1
return res
print(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 VAR NUMBER RETURN VAR EXPR 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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:
diction[num - i] = 1
print(ans) | 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_OP 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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:
textNum = str(num)
if textNum not in done:
for v in compare:
if v > num:
need = v - num
textNeed = str(need)
aStyle = textNum + "-" + textNeed
bStyle = textNeed + "-" + textNum
if textNeed in dic and aStyle not in done and bStyle not in dic:
if num == need:
amount = dic.get(textNum)
count += amount * (amount - 1) // 2
else:
count += dic.get(textNum) * dic.get(textNeed)
done[aStyle] = 1
done[bStyle] = 1
done[textNum] = 1
print(count) | 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 FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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 in range(32):
ans += mp.get((1 << k) - i, 0)
mp[i] = mp.get(i, 0) + 1
print(ans) | 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 ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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:
if b == a:
num += dic[b] - 1
else:
num += dic[b]
num = int(num / 2)
print(num) | 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 BIN_OP NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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 += d[i] * d[curr - i]
x += 1
count = int((count - m) / 2)
return count
n = int(input())
d = {}
for i in list(map(int, input().split())):
if i not in d:
d[i] = 1
else:
d[i] += 1
print(countPairs(n, d)) | 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 BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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_CALL 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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[i]]
f = 0
except KeyError:
pass
if f == 0:
d[l[i]] += 1
else:
d[l[i]] = 1
print(k) | 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 BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR VAR VAR VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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] = v.get(y - x, 0) + 1
r = 0
for x in c:
r += v.get(x, 0)
for x in d.values():
r += x * (x - 1)
print(r // 2) | 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 VAR FOR VAR VAR FOR VAR VAR IF BIN_OP NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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 VAR 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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:
total += counts[p - num]
if p - num == num:
total -= 1
total //= 2
print(total) | 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 VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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 VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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:
m += b[k] * b[d - k]
else:
m += b[k] * (b[k] - 1)
print(m // 2) | 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 IF VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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
elif x > i and x in d:
ans += d[i] * d[x]
print(ans) | 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 VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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]] += 1
else:
dic[a[i]] = 1
print(total)
solution() | 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 IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR 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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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:
ans += b[i] * b[y]
print(s + ans // 2) | 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 VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER |
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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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]
else:
ans += d[j] * d[t - j]
print(ans // 2) | 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 VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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
else:
res += occ[2**power - i]
print(res // 2) | 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 BIN_OP NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
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 line contains n positive integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9).
-----Output-----
Print the number of pairs of indexes i, j (i < j) that a_{i} + a_{j} is a power of 2.
-----Examples-----
Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3
-----Note-----
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).
In the second example all pairs of indexes (i, j) (where i < j) include in answer. | 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 distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer $k$ that if the shark in some day traveled the distance strictly less than $k$, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to $k$; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least $k$.
The shark never returned to the same location after it has moved from it. Thus, in the sequence of $n$ days we can find consecutive nonempty segments when the shark traveled the distance less than $k$ in each of the days: each such segment corresponds to one location. Max wants to choose such $k$ that the lengths of all such segments are equal.
Find such integer $k$, that the number of locations is as large as possible. If there are several such $k$, print the smallest one.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) β the number of days.
The second line contains $n$ distinct positive integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β the distance traveled in each of the day.
-----Output-----
Print a single integer $k$, such that the shark was in each location the same number of days, the number of locations is maximum possible satisfying the first condition, $k$ is smallest possible satisfying the first and second conditions.
-----Examples-----
Input
8
1 2 7 3 4 8 5 6
Output
7
Input
6
25 1 2 3 14 36
Output
2
-----Note-----
In the first example the shark travels inside a location on days $1$ and $2$ (first location), then on $4$-th and $5$-th days (second location), then on $7$-th and $8$-th days (third location). There are three locations in total.
In the second example the shark only moves inside a location on the $2$-nd day, so there is only one location. | 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):
if x < 0 or y < 0:
return
x = self.find(x)
y = self.find(y)
if x != y:
if self.rank[x] < self.rank[y]:
self.p[x] = y
self.rank[y] += self.rank[x]
else:
self.p[y] = x
self.rank[x] += self.rank[y]
def add(d, key, val):
if key not in d:
d[key] = 0
d[key] += val
if d[key] == 0:
del d[key]
def update(i, used, d, u):
used[i] = True
add(d, 1, 1)
if i - 1 >= 0 and used[i - 1] == True:
par_1 = u.find(i)
par_2 = u.find(i - 1)
size_1 = u.rank[par_1]
size_2 = u.rank[par_2]
add(d, size_1, -1)
add(d, size_2, -1)
add(d, size_1 + size_2, 1)
u.union(par_1, par_2)
if i + 1 < n and used[i + 1] == True:
par_1 = u.find(i)
par_2 = u.find(i + 1)
size_1 = u.rank[par_1]
size_2 = u.rank[par_2]
add(d, size_1, -1)
add(d, size_2, -1)
add(d, size_1 + size_2, 1)
u.union(par_1, par_2)
n = int(input())
a_ = list(map(int, input().split()))
a = [(i, x) for i, x in enumerate(a_)]
a = sorted(a, key=lambda x: x[1])
u = Union(n)
d = {}
used = [False] * n
ans = -1
max_length = -1
for index, val in a:
update(index, used, d, u)
if len(d) == 1:
len_ = list(d.values())[0]
if max_length < len_:
max_length = len_
ans = val + 1
print(ans) | 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 VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP 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 distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer $k$ that if the shark in some day traveled the distance strictly less than $k$, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to $k$; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least $k$.
The shark never returned to the same location after it has moved from it. Thus, in the sequence of $n$ days we can find consecutive nonempty segments when the shark traveled the distance less than $k$ in each of the days: each such segment corresponds to one location. Max wants to choose such $k$ that the lengths of all such segments are equal.
Find such integer $k$, that the number of locations is as large as possible. If there are several such $k$, print the smallest one.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) β the number of days.
The second line contains $n$ distinct positive integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β the distance traveled in each of the day.
-----Output-----
Print a single integer $k$, such that the shark was in each location the same number of days, the number of locations is maximum possible satisfying the first condition, $k$ is smallest possible satisfying the first and second conditions.
-----Examples-----
Input
8
1 2 7 3 4 8 5 6
Output
7
Input
6
25 1 2 3 14 36
Output
2
-----Note-----
In the first example the shark travels inside a location on days $1$ and $2$ (first location), then on $4$-th and $5$-th days (second location), then on $7$-th and $8$-th days (third location). There are three locations in total.
In the second example the shark only moves inside a location on the $2$-nd day, so there is only one location. | 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 enumerate(a, 1):
i = pos[item]
seg[i]["left"], seg[i]["right"] = i, i
seg_num += 1
size = seg[i]["right"] - seg[i]["left"] + 1
if size > seg_size:
seg_size = size
li = pos[item] - 1
if 0 <= li:
if seg[li]["right"] == i - 1:
seg[i]["left"] = seg[li]["left"]
seg[li]["right"] = seg[i]["left"]
seg_num -= 1
size = seg[i]["right"] - seg[i]["left"] + 1
if size > seg_size:
seg_size = size
ri = pos[item] + 1
if ri < n:
if seg[ri]["left"] == i + 1:
seg[i]["right"] = seg[ri]["right"]
seg[ri]["left"] = seg[i]["left"]
seg_num -= 1
size = seg[i]["right"] - seg[i]["left"] + 1
if size > seg_size:
seg_size = size
if seg_size * seg_num == n_items and seg_num > fi_seg_num:
fi_seg_num = seg_num
fi_k = item + 1
print(fi_k)
main() | 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 NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR STRING VAR VAR STRING VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR STRING NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF NUMBER VAR IF VAR VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR STRING NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR STRING NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL 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 distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer $k$ that if the shark in some day traveled the distance strictly less than $k$, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to $k$; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least $k$.
The shark never returned to the same location after it has moved from it. Thus, in the sequence of $n$ days we can find consecutive nonempty segments when the shark traveled the distance less than $k$ in each of the days: each such segment corresponds to one location. Max wants to choose such $k$ that the lengths of all such segments are equal.
Find such integer $k$, that the number of locations is as large as possible. If there are several such $k$, print the smallest one.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) β the number of days.
The second line contains $n$ distinct positive integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β the distance traveled in each of the day.
-----Output-----
Print a single integer $k$, such that the shark was in each location the same number of days, the number of locations is maximum possible satisfying the first condition, $k$ is smallest possible satisfying the first and second conditions.
-----Examples-----
Input
8
1 2 7 3 4 8 5 6
Output
7
Input
6
25 1 2 3 14 36
Output
2
-----Note-----
In the first example the shark travels inside a location on days $1$ and $2$ (first location), then on $4$-th and $5$-th days (second location), then on $7$-th and $8$-th days (third location). There are three locations in total.
In the second example the shark only moves inside a location on the $2$-nd day, so there is only one location. | 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
def decCount(val):
nonlocal lengths
if lengths[val] == 1:
del lengths[val]
else:
lengths[val] -= 1
def mergeIntervals(a, b):
return min(a[0], b[0]), max(a[1], b[1])
for v, i in appear:
if v == BigNum:
continue
inter = intervals[i]
if aa[i - 1] < aa[i]:
li = intervals[i - 1]
decCount(li[1] - li[0] + 1)
inter = mergeIntervals(li, inter)
if aa[i] > aa[i + 1]:
ri = intervals[i + 1]
decCount(ri[1] - ri[0] + 1)
inter = mergeIntervals(ri, inter)
intervals[inter[0]] = inter
intervals[inter[1]] = inter
incCount(inter[1] - inter[0] + 1)
if len(lengths) == 1:
count = list(lengths.values())[0]
if count > maxLocations:
maxLocations = count
ans = v + 1
print(ans) | 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 VAR VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP 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 distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer $k$ that if the shark in some day traveled the distance strictly less than $k$, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to $k$; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least $k$.
The shark never returned to the same location after it has moved from it. Thus, in the sequence of $n$ days we can find consecutive nonempty segments when the shark traveled the distance less than $k$ in each of the days: each such segment corresponds to one location. Max wants to choose such $k$ that the lengths of all such segments are equal.
Find such integer $k$, that the number of locations is as large as possible. If there are several such $k$, print the smallest one.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) β the number of days.
The second line contains $n$ distinct positive integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β the distance traveled in each of the day.
-----Output-----
Print a single integer $k$, such that the shark was in each location the same number of days, the number of locations is maximum possible satisfying the first condition, $k$ is smallest possible satisfying the first and second conditions.
-----Examples-----
Input
8
1 2 7 3 4 8 5 6
Output
7
Input
6
25 1 2 3 14 36
Output
2
-----Note-----
In the first example the shark travels inside a location on days $1$ and $2$ (first location), then on $4$-th and $5$-th days (second location), then on $7$-th and $8$-th days (third location). There are three locations in total.
In the second example the shark only moves inside a location on the $2$-nd day, so there is only one location. | 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 = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
n = int(input())
a = list(map(int, input().split()))
b = [(a[i], i) for i in range(n)]
b.sort(key=lambda x: x[0])
group_cnt = [0] * (n + 1)
ans = 0
uf = UnionFind(n)
segment_count = 0
for add_count, (value, index) in enumerate(b):
group_cnt[1] += 1
if index > 0:
if a[index - 1] < value:
if uf.same(index - 1, index) == False:
size_l, size_r = uf.size(index - 1), uf.size(index)
group_cnt[size_l] -= 1
group_cnt[size_r] -= 1
uf.union(index - 1, index)
size_new = uf.size(index)
group_cnt[size_new] += 1
if index < n - 1:
if a[index + 1] < value:
if uf.same(index, index + 1) == False:
size_l, size_r = uf.size(index), uf.size(index + 1)
group_cnt[size_l] -= 1
group_cnt[size_r] -= 1
uf.union(index, index + 1)
size_new = uf.size(index)
group_cnt[size_new] += 1
size_new = uf.size(index)
if group_cnt[size_new] * size_new == add_count + 1:
if segment_count < group_cnt[size_new]:
ans = value + 1
segment_count = group_cnt[size_new]
print(ans) | 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 FUNC_DEF RETURN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR 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 distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer $k$ that if the shark in some day traveled the distance strictly less than $k$, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to $k$; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least $k$.
The shark never returned to the same location after it has moved from it. Thus, in the sequence of $n$ days we can find consecutive nonempty segments when the shark traveled the distance less than $k$ in each of the days: each such segment corresponds to one location. Max wants to choose such $k$ that the lengths of all such segments are equal.
Find such integer $k$, that the number of locations is as large as possible. If there are several such $k$, print the smallest one.
-----Input-----
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) β the number of days.
The second line contains $n$ distinct positive integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β the distance traveled in each of the day.
-----Output-----
Print a single integer $k$, such that the shark was in each location the same number of days, the number of locations is maximum possible satisfying the first condition, $k$ is smallest possible satisfying the first and second conditions.
-----Examples-----
Input
8
1 2 7 3 4 8 5 6
Output
7
Input
6
25 1 2 3 14 36
Output
2
-----Note-----
In the first example the shark travels inside a location on days $1$ and $2$ (first location), then on $4$-th and $5$-th days (second location), then on $7$-th and $8$-th days (third location). There are three locations in total.
In the second example the shark only moves inside a location on the $2$-nd day, so there is only one location. | 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[y] = x
res = x
while x1 != x:
temp = x1
x1 = a[x1]
a[temp] = res
while y1 != y:
temp = y1
y1 = a[y1]
a[temp] = res
def find(x, y, a):
x1 = x
y1 = y
while a[x] > 0:
x = a[x]
while a[y] > 0:
y = a[y]
ans = False
if y == x:
ans = True
while y1 != y:
temp = a[y1]
a[y1] = y
y1 = temp
while x1 != x:
temp = a[x1]
a[x1] = x
x1 = temp
return ans
def getsize(x, par):
x1 = x
while par[x] > 0:
x = par[x]
ans = -1 * par[x]
while x != x1:
temp = par[x1]
par[x1] = x
x1 = temp
return ans
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
a[i] = [a[i], i]
a.sort()
par = [(-2.1) for i in range(n + 1)]
max1 = 0
maxk = a[0][0] - 1
numloc = 0
each = [(0) for i in range(n + 1)]
i = 0
while i < n:
curk = a[i][0]
while i < n and a[i][0] == curk:
loc = a[i][1]
if loc == 0:
par[0] = -1
numloc += 1
each[1] += 1
if par[1] != -2.1:
numloc -= 1
union(0, 1, par, each)
elif loc == n - 1:
par[n - 1] = -1
numloc += 1
each[1] += 1
if par[n - 2] != -2.1:
numloc -= 1
union(n - 1, n - 2, par, each)
else:
numloc += 1
each[1] += 1
par[loc] = -1
if par[loc - 1] != -2.1:
union(loc, loc - 1, par, each)
numloc -= 1
if par[loc + 1] != -2.1:
union(loc, loc + 1, par, each)
numloc -= 1
i += 1
if each[getsize(loc, par)] == numloc:
if numloc > max1:
maxk = curk
max1 = numloc
print(maxk + 1) | 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 ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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
op2 = op2 + "0"
solve(one, zero + 1, n - 1, op2, res)
return
res = []
op = ""
one = 0
zero = 0
solve(one, zero, n, op, res)
return res | 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 VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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] + 1])
frontier = nxt
lvl += 1
a = [i[0] for i in frontier]
return a | 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 NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 = output + "0"
helper(zero + 1, one, li, n - 1, output1)
else:
output += "1"
helper(zero, one + 1, li, n - 1, output)
li = []
helper(0, 0, li, N, "")
return li | 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 NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 ones >= zeros:
ans.append(s)
return
if ones < zeros:
return
self.NBitBinaryHelper(s + "1", ones + 1, zeros, n, ans)
self.NBitBinaryHelper(s + "0", ones, zeros + 1, n, ans) | 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 VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
rec(n, one, zero + 1, curr + "0", res)
res = []
rec(N, 0, 0, "", res)
return res | 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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"
ones3 = ones - 1
solve(res1, ones3, zeros, n - 1)
else:
res1 = res + "1"
ones1 = ones - 1
n1 = n - 1
solve(res1, ones1, zeros, n1)
res2 = res + "0"
zeros1 = zeros - 1
n2 = n - 1
solve(res2, ones, zeros1, n2)
solve(res, ones, zeros, n)
return ans | 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_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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")
helper(0, 0, N, "")
return res | 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 < ones:
curr.append("0")
backtrack(curr, ones, zeros + 1)
curr.pop()
backtrack([], 0, 0)
return sol | 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_CALL VAR LIST 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 += "1"
recur(cur, o + 1, z, n - 1, res)
cur = cur[: len(cur) - 1] + "0"
recur(cur, o, z + 1, n - 1, res)
return
recur("", 0, 0, N, res)
return res | 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 FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN EXPR FUNC_CALL VAR STRING NUMBER NUMBER VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
print_binary(n - 1, o, z + 1, "".join([output, "0"]))
print_binary(N, 0, 0, "")
return ans | 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 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
op2 = op + "0"
solve(one, zero + 1, N - 1, op2)
return
solve(one, zero, N, op)
v.sort(reverse=True)
return v | 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_OP VAR NUMBER VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
op2 = op
op2 += "0"
funtoprint(ones, zeros + 1, n - 1, op2)
funtoprint(0, 0, N, "")
return ans | 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 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
self.solve(n - 1, one, zero + 1, op + "0", ans) | 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
op2 = op[:]
op2.append("0")
solve(one, zero + 1, N - 1, op2[:], vec)
op = []
one = 0
zero = 0
vec = []
solve(one, zero, N, op, vec)
return vec | 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 ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 - 1, ones + 1, zeros, out1)
if ones > zeros:
out2 = output
out2 += "0"
solve(N - 1, ones, zeros + 1, out2)
solve(N, ones, zeros, output)
return outlist | 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 NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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)
else:
helper(n - 1, one + 1, zero, op + "1", res)
res = list()
helper(N, 0, 0, str(), res)
return 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 STRING VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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
op1 = op + "1"
self.NBitBinary2(N - 1, ones + 1, zeros, op1, result)
if ones > zeros:
op1 = op + "0"
self.NBitBinary2(N - 1, ones, zeros + 1, op1, result)
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 VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN |
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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
solver(n, temp + "0", count1, count2 + 1, ind + 1)
ans = []
solver(n, "", 0, 0, 0)
return ans | 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 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
AllNumber(noOne, noZero + 1, number + "0")
AllNumber(0, 0, "")
return ans | 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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, op2 = op.copy(), op.copy()
op1.append("1")
solve(ans, N, op1, o + 1, z)
op2.append("0")
solve(ans, N, op2, o, z + 1)
ans = []
op, o, z = [], 0, 0
solve(ans, N, op, o, z)
return ans | 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 VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 + 1, zeros)
else:
self.solve(op + "1", ans, N, ones + 1, zeros)
self.solve(op + "0", ans, N, ones, zeros + 1)
return ans | 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 FUNC_CALL VAR BIN_OP VAR STRING VAR VAR VAR BIN_OP VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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"
self.solve(i + 1, N, on, ze + 1, ans, lst)
lst = lst[: len(lst) - 1]
def NBitBinary(self, N):
ans = []
self.solve(0, N, 0, 0, ans)
return ans | 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_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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(N, c1, c0 + 1, str + "0", ans)
return ans
return help(N - 1, 1, 0, "1", ans) | 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 NUMBER NUMBER NUMBER 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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)
if c1 > c0:
op2 = outp
op2 += "0"
solve(op2)
solve("1")
return res | 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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"
op2 = op + "0"
ip = ip - 1
solve(ip, op1, ones + 1, zeros)
solve(ip, op2, ones, zeros + 1)
else:
op1 = op + "1"
ip = ip - 1
solve(ip, op1, ones + 1, zeros)
solve(ip, op, ones, zeros)
return ans | 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 EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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(res + "0", ones, zeros + 1, n - 1, arr)
def NBitBinary(self, N):
arr = []
self.solve("", 0, 0, N, arr)
return arr | 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 FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING NUMBER NUMBER VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 > zero:
op1 = op + "1"
op2 = op + "0"
self.Solve(one + 1, zero, op1, N - 1, answer)
self.Solve(one, zero + 1, op2, N - 1, answer)
elif one == zero:
op = op + "1"
self.Solve(one + 1, zero, op, N - 1, answer) | 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 BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
op1 = op
op2 = op
op1 = op1 + "1"
op2 = op2 + "0"
ans = self.solve(N, ones + 1, zeros, op1, ans)
ans = self.solve(N, ones, zeros + 1, op2, ans)
elif ones == zeros:
op1 = op
op1 = op1 + "1"
ans = self.solve(N, ones + 1, zeros, op1, ans)
return ans | 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 VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
helper(N - 1, num + "0", count_ones, count_zeroes + 1)
helper(N, "", 0, 0)
return ans | 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 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
op1 = op
op1 += "1"
self.solve(ip - 1, op1, one + 1, zero, l)
if one > zero:
op2 = op
op2 += "0"
self.solve(ip - 1, op2, one, zero + 1, l)
return l | 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 ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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)
curr.pop()
def NBitBinary(self, N):
self.ans = []
curr = []
self.solve(0, N, 0, [])
return self.ans | 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 ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR NUMBER LIST 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
fun(Z, O + 1, op + "1", N)
fun(0, 1, "1", N)
ans.sort(reverse=True)
return ans | 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 EXPR FUNC_CALL VAR NUMBER NUMBER STRING VAR EXPR FUNC_CALL VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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(
one_count + 1, zero_count, generated_string + "1"
)
result.extend(choice_1)
elif one_count < N and zero_count < N and one_count > zero_count:
choice_1 = helperFunction(
one_count + 1, zero_count, generated_string + "1"
)
choice_2 = helperFunction(
one_count, zero_count + 1, generated_string + "0"
)
result.extend(choice_1)
result.extend(choice_2)
return result
answer = helperFunction(0, 0, "")
return answer | 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 NUMBER BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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(n - 1, z, o + 1, op2, a)
else:
op1 = op + "1"
prefixes(n - 1, z, o + 1, op1, a)
a = []
prefixes(N - 1, 0, 0, "1", a)
a.sort(reverse=True)
return a | 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 VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER STRING VAR EXPR FUNC_CALL VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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)
return result | 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 one > zero and one + zero < n:
fun(one + 1, zero, n, curr + "1")
fun(one, zero + 1, n, curr + "0")
elif one + zero > n:
return
fun(0, 0, N, "")
return ans | 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 NUMBER VAR BIN_OP VAR STRING IF BIN_OP VAR VAR VAR RETURN 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 = ""
self.solve(n, i, ans, out)
return ans | 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 VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
generate(N - 1, ones, zeros + 1, op + "0")
generate(N, ones, zeros, op)
return ans | 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 VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
slove(output + "0", one, zero - 1, n - 1)
slove(output, N, N, N)
return ls | 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 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 * 10
binary(op1, o + 1, z)
binary(op2, o, z + 1)
binary(out, o, z)
return li | 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 VAR NUMBER EXPR FUNC_CALL VAR VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 + 1, n - 1)
solve1("", 0, 0, N)
return res | 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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_of_ones + 1
generate(n, new_ones, no_of_zeros, op)
elif no_of_ones > no_of_zeros:
op1 = op + "1"
new_ones = no_of_ones + 1
generate(n, new_ones, no_of_zeros, op1)
op2 = op + "0"
new_zeros = no_of_zeros + 1
generate(n, no_of_ones, new_zeros, op2)
op = ""
generate(N, 0, 0, op)
return ans | 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 ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 + 1, zero, op, lis)
else:
op1 = op
op2 = op
op1 += "1"
op2 += "0"
solve(N - 1, one + 1, zero, op1, lis)
solve(N - 1, one, zero + 1, op2, lis)
solve(N, one, zero, op, lis)
return lis | 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 BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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)
strr = ""
ans = []
printer(strr, 0, N)
return ans | 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
helper(N - 1, ones + 1, zeros, res, op + "1")
return
op1 = op + "0"
op2 = op + "1"
helper(N - 1, ones, zeros + 1, res, op1)
helper(N - 1, ones + 1, zeros, res, op2)
return
helper(N - 1, ones, zeros, res, op)
res.sort(reverse=True, key=lambda x: int(x, 2))
return res | 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_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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_bin(s2, n - 1, arr)
generate_bin(s1, n - 1, arr)
if s.count("1") == s.count("0"):
generate_bin(s + "1", n - 1, arr)
return arr
return generate_bin(s, N, arr) | 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 STRING FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR STRING BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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"
arr = self.solve(inp - 1, out1, zero + 1, one, arr)
arr = self.solve(inp - 1, out2, zero, one + 1, arr)
return arr
def NBitBinary(self, N):
arr = self.solve(N, "", 0, 0, [])
arr.sort(reverse=True)
return arr | 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_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER NUMBER LIST EXPR FUNC_CALL VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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))
if item[1] > 0:
temp.add((item[0] + "0", item[1] - 1))
return self.helper(n - 1, temp) | 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 NUMBER STRING BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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, e + 1, o, s + "1")
def NBitBinary(self, N):
global ans
ans = []
self.util(N, 0, 0, "")
return ans | 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 LIST EXPR FUNC_CALL VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 > zeros:
helper(output + "1", ones + 1, zeros)
helper(output + "0", ones, zeros + 1)
helper("", 0, 0)
return ans | 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 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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:
n_s = out + "1"
n_y = out + "0"
fun(n_1 + 1, n_0, n_s, res, N)
fun(n_1, n_0 + 1, n_y, res, N)
res = []
out = "1"
n_1 = 1
n_0 = 0
fun(n_1, n_0, out, res, N)
return res | 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 VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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
on = 0
ze = 0
N_bit(N, ind, on, ze, st, res)
return res | 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 NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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", ones, zeros + 1, N, res)
self.generate(curr + "1", ones + 1, zeros, N, res) | 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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"
solve(one, zero + 1, n - 1, st2, res)
return
string = ""
res = []
solve(0, 0, N, string, res)
return res | 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 FUNC_CALL VAR NUMBER NUMBER VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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
elif one > zero:
op1 = op + "0"
op2 = op + "1"
self.solver(op2, zero, one + 1, res, N)
self.solver(op1, zero + 1, one, res, N)
else:
op += "1"
self.solver(op, zero, one + 1, res, N) | 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 VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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", one + 1, zero)
recursive(s + "0", one, zero + 1)
recursive("", 0, 0)
return res | 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 101 have more
than or equal 1's than 0's
User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2^{N}|)
Expected Auxiliary Space: O(2^{N})
Constraints:
1 <= N <= 20 | 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 countI < countO:
return False
return True
if N == 0:
return []
for i in range(2 ** (N - 1), 2**N):
binar = str(bin(i)).lstrip("0b")
if isonegreater(binar):
res.append(binar)
res.sort(reverse=True)
return res | 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_CALL VAR VAR STRING IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN 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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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 = []
def backtrack(i, k, string):
res.append("".join(string))
if k == 0:
return
if i >= len(string):
return
maxind = largest(i)
maxval = int(string[maxind])
if i == maxind:
backtrack(i + 1, k, string)
else:
for j in range(i, len(string)):
if int(string[j]) == maxval:
string[j], string[i] = string[i], string[j]
backtrack(i + 1, k - 1, string)
string[j], string[i] = string[i], string[j]
backtrack(0, k, s)
ans = list(map(int, res))
return str(max(ans)) if ans else "" | 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_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING |
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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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[idx] = arr[idx], arr[i]
self.solve(idx + 1, arr, k - 1, n, largest)
arr[idx], arr[i] = arr[i], arr[idx]
self.solve(idx + 1, arr, k, n, largest)
def findMaximumNum(self, s, k):
arr = list(s)
n = len(arr)
largest = [s]
self.solve(0, arr, k, n, largest)
return largest[0] | 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 NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER |
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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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(i)
if ans[0] < tem:
ans[0] = tem
return
rec(ind + 1, n, k, lis, ans)
mx = lis[ind]
for j in range(ind + 1, n):
if mx < lis[j]:
mx = lis[j]
if mx == lis[ind]:
return
for j in range(ind + 1, n):
if lis[j] == mx:
lis[ind], lis[j] = lis[j], lis[ind]
rec(ind + 1, n, k - 1, lis, ans)
lis[ind], lis[j] = lis[j], lis[ind]
rec(0, n, k, lis, ans)
return ans[0] | 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 ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER |
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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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 != s[idx]:
k -= 1
for i in range(idx, n):
if s[i] == mx:
s[idx], s[i] = s[i], s[idx]
new = "".join(s)
if int(new) > int(maxi[0]):
maxi[0] = new
recursion(maxi, s, idx + 1, k, n)
s[idx], s[i] = s[i], s[idx]
return maxi
maxi = [s]
s = [char for char in s]
n = len(s)
maxi = recursion(maxi, s, 0, k, n)
return maxi[0] | 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 STRING VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN VAR NUMBER |
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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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[i] and s[j] > s[maxj]:
maxj = j
if maxj != i:
for j in range(i + 1, n):
if s[j] == s[maxj]:
s[i], s[j] = s[j], s[i]
res = max(res, self.maxUtil(s, i + 1, k - 1))
s[i], s[j] = s[j], s[i]
else:
res = max(res, self.maxUtil(s, i + 1, k))
return res | 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_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN 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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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 in range(idx + 1, n):
max_elm = max(max_elm, int(temp[i]))
if curr != max_elm:
k = k - 1
for i in range(idx, n):
if int(temp[i]) == max_elm:
temp[idx], temp[i] = temp[i], temp[idx]
if int("".join(self.m)) < int("".join(temp)):
self.m = list(temp)
sol(temp, k, idx + 1)
temp[idx], temp[i] = temp[i], temp[idx]
return self.m
return "".join(sol(temp, k, 0)) | 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 VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER |
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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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]
for j in range(idx + 1, n):
if int(mx) < int(string[j]):
mx = string[j]
if mx != string[idx]:
k -= 1
for i in range(n):
if string[i] == mx:
string[idx], string[i] = string[i], string[idx]
new_str = "".join(x for x in string)
if int(new_str) > int(mxx[0]):
mxx[0] = new_str
fun(string, k, mxx, idx + 1)
string[idx], string[i] = string[i], string[idx]
fun(string, k, mxx, 0)
return mxx[0] | 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 FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR NUMBER |
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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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[ind]):
k -= 1
for i in range(ind, n):
if int(strg[i]) == int(maxchar):
strg[i], strg[ind] = strg[ind], strg[i]
new_strg = "".join(strg)
ans[0] = max(int(ans[0]), int(new_strg))
swap(strg, k, ans, ind + 1)
strg[i], strg[ind] = strg[ind], strg[i]
n = len(s)
ans = [s]
s = [i for i in s]
swap(s, k, ans, 0)
return ans[0] | 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 VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR NUMBER |
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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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(s1[ind])
key += "#" + str(k)
if key in cache:
return cache[key]
res = []
for ind in dic[store[j]]:
s1[ind], s1[i] = s1[i], s1[ind]
dic[s1[i]].remove(ind)
dic[s1[ind]].remove(i)
dic[s1[ind]].append(ind)
dic[s1[ind]].sort()
temp = list(func(i + 1, j + 1, s1, k - 1))
dic[s1[ind]].remove(ind)
dic[s1[i]].append(ind)
dic[s1[ind]].append(i)
dic[s1[i]].sort()
dic[s1[ind]].sort()
s1[ind], s1[i] = s1[i], s1[ind]
res = res if res > temp else temp
cache[key] = res
return res
s1 = list(map(int, s))
n = len(s1)
dic = {}
for i in range(0, n):
if s1[i] in dic:
dic[s1[i]].append(i)
else:
dic[s1[i]] = [i]
store = sorted(s1, reverse=1)
m = len(store)
cache = {}
ans = func(0, 0, s1, k)
res = ""
for i in range(0, n):
res += str(ans[i])
return res | 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 VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR RETURN 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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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[i] == mx:
string[ctr], string[i] = string[i], string[ctr]
new_str = "".join(string)
if int(new_str) > int(maxm[0]):
maxm[0] = new_str
findMaximumNum(string, k, maxm, ctr + 1)
string[ctr], string[i] = string[i], string[ctr]
class Solution:
def findMaximumNum(self, s, k):
maxm = [s]
s = [char for char in s]
findMaximumNum(s, k, maxm, 0)
return maxm[0] | 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 VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR NUMBER |
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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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) > ans[-1]:
ans[-1] = "".join(s)
self.recursion(curr + 1, s, k, n, ans)
s[i], s[curr] = s[curr], s[i]
return ans[-1]
def findMaximumNum(self, s, k):
s = [c for c in s]
ans = ["".join(s)]
self.recursion(0, s, k, len(s), ans)
return ans[-1] | 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 ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER |
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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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]:
ind = x
ls = []
for x in range(i, n):
if s[x] == s[ind]:
ls.append(x)
for ind in ls:
if s[ind] > s[i]:
t = list(s)
t[i], t[ind] = t[ind], t[i]
swaps("".join(t), i + 1, k - 1, n)
else:
swaps(s, i + 1, k, n)
return
swaps(s, 0, k, n)
return maxval[0] | 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 VAR FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR NUMBER |
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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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):
m = max(m, arr[j])
if m == arr[i]:
rec(i + 1, swaps)
return
for j in range(i + 1, n):
if arr[j] == m:
arr[i], arr[j] = arr[j], arr[i]
rec(i + 1, swaps + 1)
arr[i], arr[j] = arr[j], arr[i]
rec(0, 0)
return ans[0] | 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_CALL VAR BIN_OP VAR NUMBER VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER RETURN VAR NUMBER |
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 and finally 3 with 5
Example 2:
Input:
K = 3
str = "3435335"
Output:
5543333
Explanation:
Three swaps can make the input
3435335 to 5543333, swapping 3
with 5, 4 with 5 and finally 3 with 4
Your task:
You don't have to read input or print anything. Your task is to complete the function findMaximumNum() which takes the string and an integer as input and returns a string containing the largest number formed by perfoming the swap operation at most k times.
Expected Time Complexity: O(n!/(n-k)!) , where n = length of input string
Expected Auxiliary Space: O(n)
Constraints:
1 β€ |str| β€ 30
1 β€ K β€ 10 | 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 in string[i + 1 :]:
maxch = max(ch, maxch)
if maxch != string[i]:
k -= 1
for j in range(i, n):
if string[j] == maxch:
swapped = self.swap(i, j, string, n)
self.result = max(swapped, self.result)
self.solve(i + 1, k, swapped, n)
return False
def swap(self, i, j, string, n):
if i == j:
return string
swapped = (
string[:i] + string[j] + string[i + 1 : j] + string[i] + string[j + 1 :]
)
return swapped | 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 VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.