description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | T = int(input())
for _ in range(T):
n = int(input())
a = list(map(int, input().split()))
d1 = {}
d2 = {}
for i in range(n):
b = bin(a[i])[2:]
lb = len(b)
if lb not in d1:
d1[lb] = a[i]
else:
d1[lb] = max(d1[lb], a[i])
for i in range(n):
b = bin(a[i])[2:]
lb = len(b)
if lb not in d2:
d2[lb] = a[i]
else:
d2[lb] = min(d2[lb], a[i])
ans = -1
for i in d1:
for j in d2:
A = 2**j - 1
B = 2**i - 1
ans = max(abs(A * d1[i] - B * d2[j]), ans)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | def bc(x, y):
binx = bin(x).replace("0b", "")
biny = bin(y).replace("0b", "")
return int(binx + biny, 2) - int(biny + binx, 2)
def large(A, n):
A.sort()
m = -99999999999
for i in range(n - 1):
mx = abs(bc(A[i], A[n - 1]))
if mx > m:
m = mx
print(m)
def small(A, n):
m = -99999999999
for i in range(n - 1):
for j in range(i, n):
if i != j:
b = abs(bc(A[i], A[j]))
if b > m:
m = b
print(m)
for TC in range(int(input())):
n = int(input())
A = list(map(int, input().split()))
if n >= 1000:
large(A, n)
else:
small(A, n) | FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | def BinaryConcatenation(X, Y):
binX = str(bin(X).replace("0b", ""))
binY = str(bin(Y).replace("0b", ""))
binXplusY = binX + binY
binYplusX = binY + binX
XplusY = int(binXplusY, 2)
YplusX = int(binYplusX, 2)
return abs(XplusY - YplusX)
t = int(input())
for _ in range(t):
n = int(input())
L = list(map(int, input().split()))
a = -1000000
ans = []
if n < 500:
for i in range(n - 1):
for j in range(i + 1, n):
ans.append(max(a, BinaryConcatenation(L[i], L[j])))
else:
m = max(L)
a = -2147483647
for i in L:
ans.append(max(m, BinaryConcatenation(i, m)))
print(max(ans)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | def binfun(x, y):
binx = str(bin(x))
binx = binx[2:]
biny = str(bin(y))
biny = biny[2:]
binxpy = binx + biny
binypx = biny + binx
return abs(int(binxpy, 2) - int(binypx, 2))
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
m = 0
t = max(a)
if n <= 500:
for i in range(n - 1):
for j in range(i + 1, n):
m = max(binfun(a[j], a[i]), m)
print(m)
else:
for i in range(n):
m = max(binfun(t, a[i]), m)
print(m) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = []
for i in a:
b.append(0)
while i > 0:
b[-1] += 1
i = i >> 1
ans = -1
if n >= 500:
j = a.index(max(a))
for i in range(n):
x = a[i] * 2 ** b[j] + a[j]
y = a[j] * 2 ** b[i] + a[i]
ans = max(x - y, ans, y - x)
else:
for i in range(n):
for j in range(i, n):
x = a[i] * 2 ** b[j] + a[j]
y = a[j] * 2 ** b[i] + a[i]
ans = max(x - y, ans, y - x)
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
count = 0
b = []
for i in range(n):
b.append(bin(a[i]).replace("0b", ""))
c = b[0]
d = b[0]
for i in range(n):
p = b[i] + c
q = c + b[i]
p = int(p, 2)
q = int(q, 2)
if abs(p - q) > count:
d = b[i]
count = abs(p - q)
for i in range(n):
p = b[i] + d
q = d + b[i]
p = int(p, 2)
q = int(q, 2)
if abs(p - q) > count:
count = abs(p - q)
print(count) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR VAR STRING STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | minimumValue, maximumValue = -1000000000, 1000000000
def solve(arr, N):
maxx, minn = [minimumValue] * 32, [maximumValue] * 32
for i in range(N):
binaryLength = 0
for j in range(31, -1, -1):
bit = arr[i] & 1 << j
if bit > 0 or binaryLength > 0:
binaryLength += 1
minn[binaryLength] = min(minn[binaryLength], arr[i])
maxx[binaryLength] = max(maxx[binaryLength], arr[i])
ans = 0
for i in range(32):
for j in range(32):
if minn[j] == maximumValue or maxx[i] == minimumValue:
continue
value = ((1 << j) - 1) * maxx[i] - ((1 << i) - 1) * minn[j]
ans = max(ans, value)
print(ans)
T = int(input())
for _ in range(T):
N = int(input())
arr = list(map(int, input().split()))
solve(arr, N) | ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP LIST VAR NUMBER BIN_OP LIST VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | for _ in range(int(input())):
n = int(input())
a = [int(i) for i in input().split()]
max_bit = [0] * 32
min_bit = [10**9] * 32
for i in range(n):
b = len(bin(a[i])) - 2
max_bit[b] = max(max_bit[b], a[i])
min_bit[b] = min(min_bit[b], a[i])
ans = -1
for i in range(1, 32):
for j in range(1, 32):
X = max_bit[i]
Y = min_bit[j]
if X == 0 or Y == 10**9:
continue
else:
d = X * (2**j - 1) - Y * (2**i - 1)
ans = max(ans, d)
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | INF = float("inf")
def f(x, y):
return abs(int(bin(x)[2:] + bin(y)[2:], 2) - int(bin(y)[2:] + bin(x)[2:], 2))
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in a:
l = len(bin(i)[2:])
d[l] = d.get(l, [INF, -INF])
if i < d[l][0]:
d[l][0] = i
if i > d[l][1]:
d[l][1] = i
m = 0
for i in d.keys():
for j in d.keys():
m = max(m, f(d[i][0], d[j][1]), f(d[i][1], d[j][0]))
print(m) | ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR LIST VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | def binConcatenate(x, y):
x = bin(x)[2:]
y = bin(y)[2:]
a = int(x + y, 2)
b = int(y + x, 2)
return abs(a - b)
t = int(input())
while t:
n = int(input())
arr = list(map(int, input().split()))
ans = []
temp = max(arr)
if n < 500:
for i in range(n):
for j in range(n):
ans.append(binConcatenate(arr[i], arr[j]))
print(max(ans))
else:
for i in range(n):
ans.append(binConcatenate(temp, arr[i]))
print(max(ans))
t -= 1 | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 LIST ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | def bincon(a, b):
X = bin(a).replace("0b", "")
Y = bin(b).replace("0b", "")
XpY = int(X + Y, 2)
YpX = int(Y + X, 2)
return max(XpY - YpX, YpX - XpY)
for _ in range(int(input())):
n = int(input())
llist = list(map(int, input().split()))
ans = 0
if n < 1000:
for u in range(n):
for j in range(n):
ans = max(ans, bincon(llist[u], llist[j]))
print(ans)
else:
llist.sort()
ans = bincon(llist[0], llist[n - 1])
for u in range(n):
ans = max(ans, bincon(llist[u], llist[n - 1]))
print(ans) | FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | t = int(input())
while t > 0:
t -= 1
n = int(input())
arr = list(map(int, input().strip().split(" ")))
dp = []
for i in range(31):
k = []
dp.append(k)
for num in arr:
for j in range(30, -1, -1):
temp = 1 << j
if temp & num:
dp[j].append(num)
break
ans = 0
for a in dp:
a.sort()
for i in range(31):
if dp[i]:
for j in range(31):
if dp[j]:
l = dp[i][-1]
x = l << j + 1
r = dp[j][0]
y = r << i + 1
a1 = x + r
a2 = y + l
ans = max(ans, abs(a1 - a2))
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | for tcase in range(int(input())):
n = int(input())
a = sorted(map(int, input().split()))
b = [a[i].bit_length() for i in range(n)]
c = [a[i] for i in range(n) if i == 0 or i == n - 1 or b[i - 1] < b[i + 1]]
d = [c[i].bit_length() for i in range(len(c))]
print(
max(
[
abs((c[i] << d[j]) + c[j] - (c[j] << d[i]) - c[i])
for i in range(len(c))
for j in range(i)
]
)
) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | def binary(X, Y):
str_x = bin(X)[2:]
str_y = bin(Y)[2:]
XplusY = str_x + str_y
YplusX = str_y + str_x
Xnew = int("0b" + XplusY, 2)
Ynew = int("0b" + YplusX, 2)
return abs(Xnew - Ynew)
t = int(input())
for i in range(0, t):
n = int(input())
list1 = list(map(int, input().split()))
max1 = 0
if n <= 500:
for i in range(0, len(list1) - 1):
for j in range(i + 1, len(list1)):
x = binary(list1[i], list1[j])
if x > max1:
max1 = x
else:
continue
else:
max2 = max(list1)
for k in range(0, len(list1)):
x = binary(list1[k], max2)
if x > max1:
max1 = x
else:
continue
print(max1) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | import sys
for _ in range(int(input())):
n = int(input())
arr = [int(j) for j in input().split()]
pw = [1] * 31
for i in range(1, 31):
pw[i] = 2 * pw[i - 1]
mx = [0] * 32
mn = [sys.maxsize] * 32
for j in arr:
l = len(bin(j)[2:])
mx[l] = max(mx[l], j)
mn[l] = min(mn[l], j)
ans = 0
for j in range(1, 32):
for k in range(1, 32):
x = mx[j]
y = mn[k]
if x == 0 or y == sys.maxsize:
continue
ans = max(ans, abs(x * (pw[k] - 1) - y * (pw[j] - 1)))
print(ans) | IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Consider the following function, where + denotes string concatenation.
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
You are given a sequence $A_{1}, A_{2}, \ldots, A_{N}$, Find the maximum value of BinaryConcatenation($A_{i}$, $A_{j}$) over all valid $i$ and $j$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum of BinaryConcatenation.
------ Constraints ------
$1 β€ T β€ 10^{3}$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} < 2^{30}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $10^{6}$
------ Subtasks ------
Subtask #1 (50 points): the sum of $N$ over all test cases does not exceed $1,000$
Subtask #2 (50 points): original constraints
----- Sample Input 1 ------
2
2
5 9
7
1 2 4 8 16 64 128
----- Sample Output 1 ------
12
127
----- explanation 1 ------
Example case 1: The maximum value is $12 =$ BinaryConcatenation($5$, $9$). The function computes the following:
- binX = "101"
- binY = "1001"
- binXplusY = "1011001"
- binYplusX = "1001101"
- XplusY = $89$
- YplusX = $77$
- the return value is $89-77 = 12$
Example case 2: The maximum value is $127 =$ BinaryConcatenation($1$, $128$). | t = int(input())
def func(a, b):
bin_a = bin(a).replace("0b", "")
bin_b = bin(b).replace("0b", "")
bin_X = bin_a + bin_b
bin_y = bin_b + bin_a
p = int(bin_X, 2)
q = int(bin_y, 2)
return max(p - q, q - p)
while t != 0:
n = int(input())
a = list(map(int, input().split()))
ans = 0
if n < 1000:
for i in range(n):
for j in range(n):
ans = max(ans, func(a[i], a[j]))
else:
a = sorted(a)
ans = func(a[0], a[n - 1])
for i in range(n):
ans = max(ans, func(a[i], a[n - 1]))
print(ans)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR WHILE VAR NUMBER 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 IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
This is the hard version of the problem. The difference between the versions is that the hard version does require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are n rods arranged along one straight line and numbered from 1 to n inclusive. Each of these rods must carry an electric charge quantitatively equal to either 1 or -1 (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of n numbers characterizing the charge: either 1 or -1. Then the condition must hold: a_1 - a_2 + a_3 - a_4 + β¦ = 0, or β_{i=1}^n (-1)^{i-1} β
a_i = 0.
Sparky charged all n rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has q questions. In the ith question Sparky asks: if the machine consisted only of rods with numbers l_i to r_i inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Also Sparky wants to know the numbers of these rods. Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
Input
Each test contains multiple test cases.
The first line contains one positive integer t (1 β€ t β€ 10^3), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers n and q (1 β€ n, q β€ 3 β
10^5) β the number of rods and the number of questions.
The second line of each test case contains a non-empty string s of length n, where the charge of the i-th rod is 1 if s_i is the "+" symbol, or -1 if s_i is the "-" symbol.
Each next line from the next q lines contains two positive integers l_i ans r_i (1 β€ l_i β€ r_i β€ n) β numbers, describing Sparky's questions.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5, and the sum of q over all test cases does not exceed 3 β
10^5.
It is guaranteed that the sum of the answers (minimal number of rods that can be removed) over all test cases does not exceed 10^6.
Output
For each test case, print the answer in the following format:
In the first line print a single integer k β the minimal number of rods that can be removed.
In the second line print k numbers separated by a space β the numbers of rods to be removed.
If there is more than one correct answer, you can print any.
Example
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
5 8
2
1 11
1
9
0
1
1
2
1 2
1
2
2
1 3
1
2
2
2 3
1
3
1
3
2
3 4
1
4
Note
In the first test case for the first query you can remove the rods numbered 5 and 8, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
* For the first query, we can remove the rods numbered 1 and 11, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
* For the second query we can remove the rod numbered 9, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
* For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
def main():
n, q = map(int, input().split())
S = input().strip()
cum = [0]
for i, s in enumerate(S):
if (s == "+") ^ (i % 2 == 1):
cum.append(cum[-1] + 1)
else:
cum.append(cum[-1] - 1)
for _ in range(q):
l, r = map(int, input().split())
l -= 1
r -= 1
d = cum[r + 1] - cum[l]
if d == 0:
print(0)
continue
elif d % 2 == 1:
x = 1
print(1)
else:
x = 2
print(2)
y = r
r -= 1
d = cum[r + 1] - cum[l]
if d > 0:
d = (d + 1) // 2
ll = l - 1
rr = r
while rr - ll > 1:
mid = (rr + ll) // 2
if cum[mid + 1] - cum[l] >= d:
rr = mid
else:
ll = mid
if x == 1:
print(rr + 1)
else:
print(rr + 1, y + 1)
else:
d = (d - 1) // 2
ll = l - 1
rr = r
while rr - ll > 1:
mid = (rr + ll) // 2
if cum[mid + 1] - cum[l] <= d:
rr = mid
else:
ll = mid
if x == 1:
print(rr + 1)
else:
print(rr + 1, y + 1)
for _ in range(int(input())):
main() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
This is the hard version of the problem. The difference between the versions is that the hard version does require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are n rods arranged along one straight line and numbered from 1 to n inclusive. Each of these rods must carry an electric charge quantitatively equal to either 1 or -1 (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of n numbers characterizing the charge: either 1 or -1. Then the condition must hold: a_1 - a_2 + a_3 - a_4 + β¦ = 0, or β_{i=1}^n (-1)^{i-1} β
a_i = 0.
Sparky charged all n rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has q questions. In the ith question Sparky asks: if the machine consisted only of rods with numbers l_i to r_i inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Also Sparky wants to know the numbers of these rods. Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
Input
Each test contains multiple test cases.
The first line contains one positive integer t (1 β€ t β€ 10^3), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers n and q (1 β€ n, q β€ 3 β
10^5) β the number of rods and the number of questions.
The second line of each test case contains a non-empty string s of length n, where the charge of the i-th rod is 1 if s_i is the "+" symbol, or -1 if s_i is the "-" symbol.
Each next line from the next q lines contains two positive integers l_i ans r_i (1 β€ l_i β€ r_i β€ n) β numbers, describing Sparky's questions.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5, and the sum of q over all test cases does not exceed 3 β
10^5.
It is guaranteed that the sum of the answers (minimal number of rods that can be removed) over all test cases does not exceed 10^6.
Output
For each test case, print the answer in the following format:
In the first line print a single integer k β the minimal number of rods that can be removed.
In the second line print k numbers separated by a space β the numbers of rods to be removed.
If there is more than one correct answer, you can print any.
Example
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
5 8
2
1 11
1
9
0
1
1
2
1 2
1
2
2
1 3
1
2
2
2 3
1
3
1
3
2
3 4
1
4
Note
In the first test case for the first query you can remove the rods numbered 5 and 8, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
* For the first query, we can remove the rods numbered 1 and 11, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
* For the second query we can remove the rod numbered 9, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
* For the third query we can not remove the rods at all. | import sys
from _collections import defaultdict
input = sys.stdin.readline
T = int(input())
def bilower(a, x):
if len(a) == 0:
return -1
mi = 0
ma = len(a) - 1
if a[0] > x:
return -1
if a[ma] <= x:
return ma
while ma - mi > 1:
mid = (ma + mi) // 2
if a[mid] <= x:
mi = mid
else:
ma = mid
return mi
def bihigher(a, x):
if len(a) == 0:
return 0
mi = 0
ma = len(a) - 1
if a[ma] < x:
return ma + 1
if a[0] >= x:
return 0
while ma - mi > 1:
mid = (ma + mi) // 2
if a[mid] >= x:
ma = mid
else:
mi = mid
return ma
def birange(a, l, r):
left = bihigher(a, l)
right = bilower(a, r)
return right - left + 1
for iii in range(T):
n, q = map(int, input().split())
dic1 = defaultdict(lambda: [])
dic2 = defaultdict(lambda: [])
a = [0] + list(input()[:-1])
for i in range(1, n + 1):
if a[i] == "+":
a[i] = 1
else:
a[i] = -1
s = [0] * (n + 1)
for i in range(1, n + 1):
if i % 2 == 1:
pass
else:
a[i] *= -1
s[i] = s[i - 1] + a[i]
if a[i] > 0:
dic1[s[i - 1]].append(i)
else:
dic2[s[i - 1]].append(i)
for _ in range(q):
l, r = map(int, input().split())
res = s[r] - s[l - 1]
if res == 0:
print(0)
elif res % 2 == 1:
print(1)
if res > 0:
print(dic1[res // 2 + s[l - 1]][bihigher(dic1[res // 2 + s[l - 1]], l)])
else:
h = (res + 1) // 2
print(dic2[h + s[l - 1]][bihigher(dic2[h + s[l - 1]], l)])
else:
print(2)
res -= a[r]
if res > 0:
print(
dic1[res // 2 + s[l - 1]][bihigher(dic1[res // 2 + s[l - 1]], l)], r
)
else:
h = (res + 1) // 2
print(dic2[h + s[l - 1]][bihigher(dic2[h + s[l - 1]], l)], r) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR RETURN BIN_OP VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR |
This is the hard version of the problem. The difference between the versions is that the hard version does require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are n rods arranged along one straight line and numbered from 1 to n inclusive. Each of these rods must carry an electric charge quantitatively equal to either 1 or -1 (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of n numbers characterizing the charge: either 1 or -1. Then the condition must hold: a_1 - a_2 + a_3 - a_4 + β¦ = 0, or β_{i=1}^n (-1)^{i-1} β
a_i = 0.
Sparky charged all n rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has q questions. In the ith question Sparky asks: if the machine consisted only of rods with numbers l_i to r_i inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Also Sparky wants to know the numbers of these rods. Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
Input
Each test contains multiple test cases.
The first line contains one positive integer t (1 β€ t β€ 10^3), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers n and q (1 β€ n, q β€ 3 β
10^5) β the number of rods and the number of questions.
The second line of each test case contains a non-empty string s of length n, where the charge of the i-th rod is 1 if s_i is the "+" symbol, or -1 if s_i is the "-" symbol.
Each next line from the next q lines contains two positive integers l_i ans r_i (1 β€ l_i β€ r_i β€ n) β numbers, describing Sparky's questions.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5, and the sum of q over all test cases does not exceed 3 β
10^5.
It is guaranteed that the sum of the answers (minimal number of rods that can be removed) over all test cases does not exceed 10^6.
Output
For each test case, print the answer in the following format:
In the first line print a single integer k β the minimal number of rods that can be removed.
In the second line print k numbers separated by a space β the numbers of rods to be removed.
If there is more than one correct answer, you can print any.
Example
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
5 8
2
1 11
1
9
0
1
1
2
1 2
1
2
2
1 3
1
2
2
2 3
1
3
1
3
2
3 4
1
4
Note
In the first test case for the first query you can remove the rods numbered 5 and 8, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
* For the first query, we can remove the rods numbered 1 and 11, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
* For the second query we can remove the rod numbered 9, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
* For the third query we can not remove the rods at all. | from sys import stdin, stdout
raw_input = input
xrange = range
raw_input = lambda: stdin.readline().rstrip()
input = lambda: int(raw_input())
I = lambda: map(int, raw_input().split())
def f(l, r):
global pprefix, prefix, p
xl = sign(g(l))
if xl == 0:
return l
xr = sign(g(r))
if xr == 0:
return r
l1 = l
r1 = r
while True:
m = (r1 + l1) // 2
x = sign(g(m))
if x == 0:
return m
if x != xl:
r1 = m
elif x != xr:
l1 = m
def sign(n):
if n > 0:
return 1
elif n < 0:
return -1
else:
return 0
def g(i):
global pprefix, p
return p + pprefix[i]
t = input()
for _ in xrange(t):
n, q = I()
s = raw_input()
prefix = [0]
pprefix = [0]
c = 0
prevC = 0
for i in xrange(n):
if i % 2:
if s[i] == "-":
c += 1
else:
c -= 1
elif s[i] == "+":
c += 1
else:
c -= 1
prefix.append(c)
pprefix.append(c + prevC)
prevC = c
for _ in xrange(q):
a, b = I()
w = prefix[b] - prefix[a - 1]
if w == 0:
stdout.write("0\n")
elif (b - a) % 2 == 0:
stdout.write("1\n")
l = a
r = b
p = -(prefix[l - 1] + prefix[r])
y = f(l, r)
stdout.write(str(y) + "\n")
else:
stdout.write("2\n")
l = a
r = b - 1
p = -(prefix[l - 1] + prefix[r])
y = f(l, r)
stdout.write(str(y) + " " + str(b) + "\n") | ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, grid):
def fun(grid):
n = len(grid)
m = len(grid[0])
queue = []
visited = [[(0) for i in range(m)] for j in range(n)]
for i in range(n):
for j in range(m):
if grid[i][j] == 2:
queue.append(((i, j), 0))
visited[i][j] = 2
time = 0
drow = [-1, 0, 1, 0]
dcol = [0, 1, 0, -1]
while len(queue) > 0:
r = queue[0][0][0]
c = queue[0][0][1]
t = queue[0][1]
time = max(time, t)
queue.pop(0)
for i in range(4):
nrow = r + drow[i]
ncol = c + dcol[i]
if (
nrow >= 0
and nrow < n
and ncol >= 0
and ncol < m
and visited[nrow][ncol] != 2
and grid[nrow][ncol] == 1
):
queue.append(((nrow, ncol), t + 1))
visited[nrow][ncol] = 2
for i in range(n):
for j in range(m):
if visited[i][j] != 2 and grid[i][j] == 1:
return -1
return time
return fun(grid) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, grid):
ct = 0
res = -1
q = Queue()
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] > 0:
ct += 1
if grid[i][j] == 2:
q.put([i, j])
while not q.empty():
res += 1
size = q.qsize()
for k in range(size):
cur = q.get()
ct -= 1
for i in range(4):
x = cur[0] + dx[i]
y = cur[1] + dy[i]
if (
x >= len(grid)
or x < 0
or y >= len(grid[0])
or y < 0
or grid[x][y] != 1
):
continue
grid[x][y] = 2
q.put([x, y])
if ct:
return -1
else:
return max(0, res) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER VAR |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, gp):
d = dict()
leng = 0
for i in range(len(gp)):
for j in range(len(gp[0])):
d[i, j] = float("inf")
for i in range(len(gp)):
for j in range(len(gp[0])):
if gp[i][j] == 2:
time = 0
visted = set()
d[i, j] = 0
self.Bfs(i, j, gp, visted, d)
if gp[i][j] == 1 or gp[i][j] == 2:
leng += 1
tl = 0
for i in range(len(gp)):
for j in range(len(gp[0])):
if d[i, j] != float("inf"):
tl += 1
if tl == leng:
m = -1
for i in d.values():
if i < float("inf") and i > m:
m = i
return m
return -1
def safe(self, x, y, gp):
if (
x >= 0
and x < len(gp)
and y >= 0
and y < len(gp[0])
and gp[x][y] == 1
and gp[x][y] != 0
):
return True
return False
def Bfs(self, i, j, gp, visted, d):
queue = []
queue.append((i, j))
visted.add((i, j))
while len(queue) != 0:
i, j = queue.pop(0)
List = [(1, 0), (0, 1), (0, -1), (-1, 0)]
for a, b in List:
saf = self.safe(i + a, j + b, gp)
if saf == True and (i + a, j + b) not in visted:
visted.add((i + a, j + b))
queue.append((i + a, j + b))
if d[i, j] + 1 < d[i + a, j + b]:
d[i + a, j + b] = d[i, j] + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, grid):
q = []
m, n = len(grid), len(grid[0])
count_oranges = 0
grid1 = grid
for i in range(m):
for j in range(n):
if grid1[i][j] == 2:
q.append((i, j))
if grid1[i][j] == 1:
count_oranges += 1
if count_oranges == 0:
return 0
if not q:
return -1
dir = [(-1, 0), (1, 0), (0, -1), (0, 1)]
min = 0
while q:
size = len(q)
while size > 0:
x, y = q.pop(0)
size -= 1
for x1, y1 in dir:
i, j = x + x1, y + y1
if 0 <= i < m and 0 <= j < n and grid1[i][j] == 1:
grid1[i][j] = 2
count_oranges -= 1
q.append((i, j))
min += 1
if count_oranges == 0:
return min
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, grid):
row = len(grid)
col = len(grid[0])
vis = [[(0) for j in range(col)] for i in range(row)]
q = []
time = 0
dr = [-1, 0, 1, 0]
dc = [0, 1, 0, -1]
for i in range(row):
for j in range(col):
if grid[i][j] == 2:
q.append((i, j, 0))
vis[i][j] = 2
while q:
r, c, t = q.pop(0)
time = max(time, t)
for i in range(4):
nrow = r + dr[i]
ncol = c + dc[i]
if (
nrow >= 0
and ncol >= 0
and ncol < col
and nrow < row
and grid[nrow][ncol] == 1
and vis[nrow][ncol] == 0
):
vis[nrow][ncol] = 2
q.append((nrow, ncol, t + 1))
for i in range(row):
for j in range(col):
if grid[i][j] == 1 and vis[i][j] != 2:
return -1
return time | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, grid):
arr = grid
que = []
l = len(arr[0])
for i in range(len(arr)):
for j in range(l):
if arr[i][j] == 2:
que.append([i, j, 0])
else:
continue
t = 0
while que:
i, j, t = que.pop(0)
for r, c in [[i + 1, j], [i - 1, j], [i, j - 1], [i, j + 1]]:
if 0 <= r < len(arr) and 0 <= c < l and arr[r][c] == 1:
arr[r][c] = 2
que.append([r, c, t + 1])
return -1 if any(c == 1 for r in arr for c in r) else t | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR LIST LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, a):
q = []
n = len(a)
m = len(a[0])
for i in range(n):
for j in range(m):
if a[i][j] == 2:
q.append((i, j, 0))
ans = 0
while len(q) > 0:
l = len(q)
for i in range(l):
i, j, t = q[0]
q.pop(0)
ans = max(ans, t)
if i + 1 <= n - 1 and a[i + 1][j] == 1:
q.append((i + 1, j, t + 1))
a[i + 1][j] = 2
if i - 1 >= 0 and a[i - 1][j] == 1:
q.append((i - 1, j, t + 1))
a[i - 1][j] = 2
if j + 1 <= m - 1 and a[i][j + 1] == 1:
q.append((i, j + 1, t + 1))
a[i][j + 1] = 2
if j - 1 >= 0 and a[i][j - 1] == 1:
q.append((i, j - 1, t + 1))
a[i][j - 1] = 2
for i in range(n):
for j in range(m):
if a[i][j] == 1:
return -1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, a):
ans = 0
q = []
n = len(a)
m = len(a[0])
for i in range(n):
for j in range(m):
if a[i][j] == 2:
q.append((i, j, 0))
while len(q) != 0:
row, col, c = q.pop(0)
ans = c
for i in [-1, 1]:
if row + i >= 0 and row + i < n and a[row + i][col] == 1:
a[row + i][col] = 2
q.append((row + i, col, c + 1))
if col + i >= 0 and col + i < m and a[row][col + i] == 1:
a[row][col + i] = 2
q.append((row, col + i, c + 1))
for i in range(n):
for j in range(m):
if a[i][j] == 1:
return -1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR LIST NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, grid):
rows = len(grid)
cols = len(grid[0])
queue = []
fresh_count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 2:
queue.append((i, j))
elif grid[i][j] == 1:
fresh_count += 1
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
time = 0
while queue:
if fresh_count == 0:
return time
time += 1
for _ in range(len(queue)):
x, y = queue.pop(0)
for dx, dy in directions:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] == 1:
grid[nx][ny] = 2
queue.append((nx, ny))
fresh_count -= 1
if fresh_count > 0:
return -1
return time | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER RETURN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, grid):
queue = []
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 2:
queue.append((i, j, 0))
time_taken = 0
while queue:
i, j, time_taken = queue.pop(0)
for ni, nj in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:
if 0 <= ni < len(grid) and 0 <= nj < len(grid[0]) and grid[ni][nj] == 1:
grid[ni][nj] = 2
queue.append((ni, nj, time_taken + 1))
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
return -1
return time_taken | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, grid):
n = len(grid)
m = len(grid[0])
s = []
cntfresh = 0
vis = [[(0) for i in range(m)] for j in range(n)]
for i in range(n):
for j in range(m):
if grid[i][j] == 2:
s.append([i, j, 0])
vis[i][j] = 2
if grid[i][j] == 1:
cntfresh += 1
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
count = 0
total = 0
while len(s) > 0:
i, j, t = s.pop(0)
count = max(count, t)
for x in range(4):
ni = i + dx[x]
nj = j + dy[x]
if (
ni >= 0
and ni < n
and nj >= 0
and nj < m
and vis[ni][nj] != 2
and grid[ni][nj] == 1
):
s.append([ni, nj, t + 1])
vis[ni][nj] = 2
total += 1
if cntfresh != total:
return -1
else:
return count | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR |
Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the following meaning:
0 : Empty cell
1 : Cells have fresh oranges
2 : Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time.
Example 1:
Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
Output: 1
Explanation: The grid is-
0 1 2
0 1 2
2 1 1
Oranges at positions (0,2), (1,2), (2,0)
will rot oranges at (0,1), (1,1), (2,2) and
(2,1) in unit time.
Example 2:
Input: grid = {{2,2,0,1}}
Output: -1
Explanation: The grid is-
2 2 0 1
Oranges at (0,0) and (0,1) can't rot orange at
(0,3).
Your Task:
You don't need to read or print anything, Your task is to complete the function orangesRotting() which takes grid as input parameter and returns the minimum time to rot all the fresh oranges. If not possible returns -1.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 β€ n, m β€ 500 | class Solution:
def orangesRotting(self, grid):
def do(i, j, temp):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]):
return
if grid[i][j] == 1:
grid[i][j] += 1
temp.append([i, j])
rotten = []
temp = []
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 2:
temp.append([i, j])
rotten.append(temp)
count = 0
while len(rotten) > 0:
temp = []
for i in rotten[0]:
do(i[0] - 1, i[1], temp)
do(i[0] + 1, i[1], temp)
do(i[0], i[1] + 1, temp)
do(i[0], i[1] - 1, temp)
if temp != []:
rotten.append(temp)
count += 1
temp = []
rotten.pop(0)
for i in grid:
for j in i:
if j == 1:
return -1
return count if count > 0 else -1 | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR IF VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR NUMBER VAR NUMBER |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
if not root:
return None
if root.data == n1 or root.data == n2:
return root
left = self.lca(root.left, n1, n2)
right = self.lca(root.right, n1, n2)
if left == None and right == None:
return None
if left == None and not right == None:
return right
if not left == None and right == None:
return left
if not left == None and not right == None:
return root | CLASS_DEF FUNC_DEF IF VAR RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE VAR NONE RETURN NONE IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, p, q):
def answer(root):
if root == None or root.data == p or root.data == q:
return root
lt = answer(root.left)
rt = answer(root.right)
if rt == None:
return lt
if lt == None:
return rt
else:
return root
return answer(root) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
def findNode(root, l, x):
if root is None:
return False
l.append(root)
if root.data == x:
return True
if findNode(root.left, l, x) or findNode(root.right, l, x):
return True
l.pop()
return False
l1 = []
findNode(root, l1, n1)
l2 = []
findNode(root, l2, n2)
i = 0
j = 0
x = None
while i < len(l1) and j < len(l2):
if l1[i].data == l2[j].data:
x = l1[i]
i += 1
j += 1
else:
break
return x | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR RETURN NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
def commonAncestor(root, k1, k2):
if root != None:
l1, l2, ld = commonAncestor(root.left, k1, k2)
r1, r2, rd = commonAncestor(root.right, k1, k2)
Key1_found = True if root.data == k1 else l1 or r1
Key2_found = True if root.data == k2 else l2 or r2
key = ld if ld != None else rd
if key == None and Key1_found and Key2_found:
key = root
return Key1_found, Key2_found, key
return False, False, None
_, _, v = commonAncestor(root, n1, n2)
return v | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NONE VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR RETURN NUMBER NUMBER NONE ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
if root is None:
return None
if root.data == n1 or root.data == n2:
return root
in_left_tree = self.lca(root.left, n1, n2)
in_right_tree = self.lca(root.right, n1, n2)
if in_left_tree and in_right_tree:
return root
else:
return in_left_tree or in_right_tree | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR RETURN VAR VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
if root is None:
return None
if root.data == n1 or root.data == n2:
return root
left = self.lca(root.left, n1, n2)
right = self.lca(root.right, n1, n2)
if left and right:
return root
elif left:
return left
else:
return right
return Node(-1) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR RETURN VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
def solve(root):
if not root:
return root, False
if root.data == n1 or root.data == n2:
return root, True
if root:
lr, ls = solve(root.left)
rr, rs = solve(root.right)
if ls and rs:
return root, True
if ls:
return lr, True
if rs:
return rr, True
return root, False
ar, ast = solve(root)
return ar | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN VAR NUMBER IF VAR VAR VAR VAR RETURN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR NUMBER IF VAR RETURN VAR NUMBER IF VAR RETURN VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
ans = []
def dfs(root, n, ans=[]):
ans.append(root)
if root.data == n:
return True
if root.left:
if dfs(root.left, n, ans):
return True
if root.right:
if dfs(root.right, n, ans):
return True
ans.pop()
dfs(root, n1, ans)
x1 = ans
ans = []
dfs(root, n2, ans)
x2 = ans
l1 = len(x1)
l2 = len(x2)
if l1 > l2:
for i in range(l2):
if x1[i] != x2[i]:
return x1[i - 1]
return x2[-1]
else:
for i in range(l1):
if x1[i] != x2[i]:
return x1[i - 1]
return x1[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF LIST EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER IF VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | def roottoleaf(root, node, path):
if root == None:
return False
path.append(root)
if root.data == node:
return True
if roottoleaf(root.left, node, path) or roottoleaf(root.right, node, path):
return True
path.pop(-1)
return False
class Solution:
def lca(self, root, n1, n2):
path1 = []
path2 = []
roottoleaf(root, n1, path1)
roottoleaf(root, n2, path2)
if len(path1) == 0 or len(path2) == 0:
return -1
i = 0
while i < len(path1) and i < len(path2):
if path1[i] != path2[i]:
break
i += 1
return path1[i - 1] | FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | def findLCA(root, n1, n2, ans, curr):
if root == None:
return
if root.data == n1:
ans[0] = curr[0]
if root.data == n2:
ans[1] = curr[0]
if ans[0] != "N" and ans[1] != "N":
return
if root.left:
findLCA(root.left, n1, n2, ans, [curr[0] + "0"])
if root.right:
findLCA(root.right, n1, n2, ans, [curr[0] + "1"])
class Solution:
def lca(self, root, n1, n2):
ans = ["N", "N"]
st = ""
curr = [st]
findLCA(root, n1, n2, ans, curr)
r1, r2 = ans
n = min(len(r1), len(r2))
i = 0
while i < n:
if r1[i] == r2[i]:
i += 1
else:
break
str = r1[:i]
for s in str:
if s == "1":
root = root.right
else:
root = root.left
return root | FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING RETURN IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR LIST BIN_OP VAR NUMBER STRING IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR LIST BIN_OP VAR NUMBER STRING CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING ASSIGN VAR STRING ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Node:
def __init__(self, value):
self.left = None
self.data = value
self.right = None
class Solution:
def lca(self, root, n1, n2):
if root is None:
return None
if root.data == n1 or root.data == n2:
return root
left = self.lca(root.left, n1, n2)
right = self.lca(root.right, n1, n2)
if left is None:
return right
if right is None:
return left
return root | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def util(self, root, n1, n2):
if root == None:
return None
if root.data == n1 or root.data == n2:
return root
l = self.util(root.left, n1, n2)
r = self.util(root.right, n1, n2)
if l != None and r != None:
return root
if l == None and r == None:
return None
if l != None:
return l
if r != None:
return r
def lca(self, root, n1, n2):
return self.util(root, n1, n2) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN NONE IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
def findlca(root, n1, n2, ans):
if root is None:
return 0
leftval = findlca(root.left, n1, n2, ans)
if leftval == 2:
return leftval
rightval = findlca(root.right, n1, n2, ans)
if rightval == 2:
return rightval
foundval = leftval + rightval
if root.data == n1 or root.data == n2:
foundval += 1
if foundval == 2:
ans[0] = root
return foundval
ans = [None]
findlca(root, n1, n2, ans)
return ans[0] | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR RETURN VAR ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Node:
def __init__(self, value):
self.left = None
self.data = value
self.right = None
class Solution:
def lca(self, root, n1, n2):
if root == None:
return Node(-1)
if root.data == n1 or root.data == n2:
return root
c1 = self.lca(root.left, n1, n2)
c2 = self.lca(root.right, n1, n2)
if c1.data > 0 and c2.data > 0:
return root
elif c1.data > 0:
return c1
elif c2.data > 0:
return c2
else:
return c1 | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR NONE RETURN FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
if not root or root.data == n1 or root.data == n2:
return root
left = self.lca(root.left, n1, n2)
right = self.lca(root.right, n1, n2)
if not left:
return right
elif not right:
return left
else:
return root | CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN VAR IF VAR RETURN VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def findNode(self, root, n1, n2):
if not root:
return None
if root.data == n1 or root.data == n2:
return root
leftAns = self.findNode(root.left, n1, n2)
rightAns = self.findNode(root.right, n1, n2)
if leftAns != None and rightAns != None:
return root
if leftAns == None and rightAns != None:
return rightAns
if leftAns != None and rightAns == None:
return leftAns
return None
def lca(self, root, n1, n2):
ans = self.findNode(root, n1, n2)
return ans | CLASS_DEF FUNC_DEF IF VAR RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR RETURN NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
def LCAPath(root, path, tar):
if root == None:
return
if root.data == tar:
self.ans.append(path + [root.data])
LCAPath(root.left, path + [root.data], tar)
LCAPath(root.right, path + [root.data], tar)
self.ans = []
LCAPath(root, [], n1)
ans1 = self.ans[0]
self.ans = []
LCAPath(root, [], n2)
ans2 = self.ans[0]
for i in range(min(len(ans1), len(ans2))):
if ans1[i] == ans2[i]:
val = ans1[i]
else:
return Node(val)
return Node(val) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | def find_ancestors(root, n, original_ancestors):
ancestors = original_ancestors.copy()
if root == None:
return None
elif root.data == n:
ancestors.append(root)
return ancestors
ancestors.append(root)
leftfound = find_ancestors(root.left, n, ancestors)
if leftfound != None:
return leftfound
else:
rightfound = find_ancestors(root.right, n, ancestors)
if rightfound != None:
return rightfound
return None
class Solution:
def lca(self, root, n1, n2):
n1p = []
n2p = []
node = root
n1p = find_ancestors(root, n1, [])
n2p = find_ancestors(root, n2, [])
matched = None
for n1 in n1p:
for n2 in n2p:
if n1 == n2:
matched = n1
return matched | FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR NONE RETURN NONE IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE RETURN VAR RETURN NONE CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR LIST ASSIGN VAR NONE FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
def pathLCA(root, path, tar):
if root == None:
return
if root.data == tar:
self.ans.append(path + [root.data])
return
pathLCA(root.left, path + [root.data], tar)
pathLCA(root.right, path + [root.data], tar)
self.ans = []
pathLCA(root, [], n1)
ans1 = self.ans[0]
self.ans = []
pathLCA(root, [], n2)
ans2 = self.ans[0]
while ans1 and ans2:
if ans1[-1] != ans2[-1]:
if len(ans1) > len(ans2):
ans1.pop()
else:
ans2.pop()
else:
return Node(ans1[-1])
return Node(-1) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def rec_sol(self, root, n1, n2, ans):
if root == None:
return -1
left_ans = self.rec_sol(root.left, n1, n2, ans)
right_ans = self.rec_sol(root.right, n1, n2, ans)
if root.data == n1 or root.data == n2:
if left_ans == 1 or right_ans == 1:
ans.append(root)
return 1
if left_ans == 1 and right_ans == 1:
ans.append(root)
return 1
elif left_ans == 1 or right_ans == 1:
return 1
return -1
def lca(self, root, n1, n2):
ans = []
self.rec_sol(root, n1, n2, ans)
if len(ans) == 0:
return -1
return ans[0] | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN VAR NUMBER |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | def find_lca_using_recursion(value_1, value_2, node):
if node is None:
return False, False, None
left = find_lca_using_recursion(value_1, value_2, node.left)
right = find_lca_using_recursion(value_1, value_2, node.right)
found_1 = value_1 == node.data or left[0] or right[0]
found_2 = value_2 == node.data or left[1] or right[1]
if found_1 and found_2:
ans = left[2] or right[2] or node
else:
ans = None
return found_1, found_2, ans
class Solution:
def lca(self, root, n1, n2):
lca = find_lca_using_recursion(n1, n2, root)
return lca[2] | FUNC_DEF IF VAR NONE RETURN NUMBER NUMBER NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR NONE RETURN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
if root is None:
return 0
def fun(root, n1, n2):
if root is None:
return None
if root.data == n1 or root.data == n2:
return root
l = fun(root.left, n1, n2)
r = fun(root.right, n1, n2)
if l and r:
return root
elif l:
return l
else:
return r
return fun(root, n1, n2) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR RETURN VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
data = [n1, n2]
Node1 = Node(0)
ans = [Node1]
def F(root):
if root == None:
return False
left = F(root.left)
right = F(root.right)
if left == True and right == True:
ans.append(root)
return True
if root.data in data:
data.remove(root.data)
if len(data) == 0:
ans.append(root)
return True
return left or right
F(root)
return ans[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def store(self, root, data=[]):
if root is None:
return
self.dct[root.data] = data + [root.data]
data = data + [root.data]
self.store(root.left, data)
self.store(root.right, data)
def lca(self, root, n1, n2):
self.dct = {}
self.store(root)
n1_list = self.dct[n1][::-1]
n2_list = self.dct[n2][::-1]
for itr in n1_list:
if itr in n2_list:
return Node(itr) | CLASS_DEF FUNC_DEF LIST IF VAR NONE RETURN ASSIGN VAR VAR BIN_OP VAR LIST VAR ASSIGN VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def findpath(self, root, path, x):
if root is None:
return False
path.append(root)
if root.data == x:
return True
if self.findpath(root.left, path, x) or self.findpath(root.right, path, x):
return True
path.pop()
return False
def lca(self, root, n1, n2):
p1, p2 = [], []
if not self.findpath(root, p1, n1) or not self.findpath(root, p2, n2):
return None
i = 0
while i < len(p1) and i < len(p2):
if p1[i] != p2[i]:
break
i += 1
return p1[i - 1] | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR LIST LIST IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NONE ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of tree.
Example 1:
Input:
n1 = 2 , n2 = 3
1
/ \
2 3
Output: 1
Explanation:
LCA of 2 and 3 is 1.
Example 2:
Input:
n1 = 3 , n2 = 4
5
/
2
/ \
3 4
Output: 2
Explanation:
LCA of 3 and 4 is 2.
Your Task:
You don't have to read, input, or print anything. Your task is to complete the function lca() that takes nodes, n1, and n2 as parameters and returns the LCA node as output.
Expected Time Complexity:O(N).
Expected Auxiliary Space:O(Height of Tree).
Constraints:
1 β€ Number of nodes β€ 10^{5}
1 β€ Data of a node β€ 10^{5} | class Solution:
def lca(self, root, n1, n2):
v = [False, False]
lca = self.findLCA(root, n1, n2, v)
if v[0] and v[1] or v[0] and self.find(lca, n2) or v[1] and self.find(lca, n1):
return lca
return None
def find(self, root, k):
if root is None:
return False
return root.data == k or self.find(root.left, k) or self.find(root.right, k)
def findLCA(self, root, n1, n2, v):
if not root:
return None
if root.data == n1:
v[0] = True
return root
if root.data == n2:
v[1] = True
return root
leftLCA = self.findLCA(root.left, n1, n2, v)
rightLCA = self.findLCA(root.right, n1, n2, v)
if leftLCA and rightLCA:
return root
return leftLCA if leftLCA else rightLCA | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN VAR RETURN NONE FUNC_DEF IF VAR NONE RETURN NUMBER RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN NONE IF VAR VAR ASSIGN VAR NUMBER NUMBER RETURN VAR IF VAR VAR ASSIGN VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN VAR RETURN VAR VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if not len(level):
return None
rootVal = level[0]
root = Node(rootVal)
ind = 0
while ino[ind] != rootVal:
ind += 1
leftIno = ino[:ind]
rightIno = ino[ind + 1 :]
leftLevel = []
rightLevel = []
for val in level[1:]:
if val in leftIno:
leftLevel.append(val)
else:
rightLevel.append(val)
root.left = buildTree(leftLevel, leftIno)
root.right = buildTree(rightLevel, rightIno)
return root | FUNC_DEF IF FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if len(ino) == 1:
return Node(ino[0])
mp = {ino[i]: i for i in range(len(ino))}
for i in range(len(level)):
val = level[i]
if val in mp:
root = Node(val)
root_idx = mp[val]
break
if root_idx > 0:
root.left = buildTree(level, ino[:root_idx])
if root_idx + 1 < n:
root.right = buildTree(level, ino[root_idx + 1 :])
return root | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def find(level, ino):
lev = []
for i in range(len(level)):
for j in range(len(ino)):
if ino[j] == level[i]:
lev.append(level[i])
return lev
def cons(level, ino, n):
dict = {}
for i in range(n):
dict[ino[i]] = i
rootIndex = dict[level[0]]
root = Node(level[0])
if rootIndex != 0:
root.left = cons(find(level, ino[0:rootIndex]), ino[0:rootIndex], rootIndex)
if rootIndex != n - 1:
root.right = cons(
find(level, ino[rootIndex + 1 : n]),
ino[rootIndex + 1 : n],
n - rootIndex - 1,
)
return root
def buildTree(level, ino):
n = len(level)
head = cons(level, ino, n)
return head | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
def construct(start, end):
if start > end:
return
index = l[ino[start]]
ans = start
for i in range(start, end + 1):
if l[ino[i]] < index:
index = l[ino[i]]
ans = i
root = Node(ino[ans])
if start == end:
return root
root.left = construct(start, ans - 1)
root.right = construct(ans + 1, end)
return root
l = {}
for i in range(len(level)):
l[level[i]] = i
return construct(0, len(level) - 1) | FUNC_DEF FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
root = Node(level[0])
m = ino.index(root.data)
leftin = ino[:m]
rightin = ino[m + 1 :]
leftlevel = level[1:]
rightlevel = level
rightlevel.remove(root.data)
for i in leftin:
try:
rightlevel.remove(i)
except:
continue
for i in rightin:
try:
leftlevel.remove(i)
except:
continue
if leftin:
root.left = buildTree(leftlevel, leftin)
if rightin:
root.right = buildTree(rightlevel, rightin)
return root | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if level is None and ino is None:
return None
elif level is None and ino is not None or level is not None and ino is None:
return None
else:
root = Node(level[0])
ino_root_idx = ino.index(level[0])
ino_left, ino_right = [], []
for i in range(ino_root_idx):
ino_left.append(ino[i])
for i in range(ino_root_idx + 1, len(ino)):
ino_right.append(ino[i])
N = len(level)
level_left, level_right = [], []
for i in range(1, N):
if level[i] in ino_left:
level_left.append(level[i])
else:
level_right.append(level[i])
if level_left:
root.left = buildTree(level_left, ino_left)
if level_right:
root.right = buildTree(level_right, ino_right)
return root | FUNC_DEF IF VAR NONE VAR NONE RETURN NONE IF VAR NONE VAR NONE VAR NONE VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buil(level, ino):
if ino:
for i in range(len(level)):
if level[i] in ino:
node = Node(level[i])
index = ino.index(level[i])
break
node.left = buil(level, ino[0:index])
node.right = buil(level, ino[index + 1 : len(ino)])
return node
else:
return None
def buildTree(level, ino):
n = len(level)
a = buil(level, ino)
return a | FUNC_DEF IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR RETURN NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if ino:
for i in range(len(level)):
if level[i] in ino:
curr = Node(level[i])
x = ino.index(level[i])
break
curr.left = buildTree(level, ino[:x])
curr.right = buildTree(level, ino[x + 1 :])
return curr | FUNC_DEF IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | class Node:
def __init__(self, key):
self.data = key
self.left = None
self.right = None
def buildTree(level, ino):
if ino:
for i in range(0, len(level)):
if level[i] in ino:
node = Node(level[i])
io_index = ino.index(level[i])
break
node.left = buildTree(level, ino[0:io_index])
node.right = buildTree(level, ino[io_index + 1 : len(ino)])
return node
else:
return None | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR RETURN NONE |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def f(level: list, inorder: list, indices: dict):
if len(level) == 0:
return None
cur = Node(level[0])
idx = indices[level[0]]
left_level, right_level = [], []
for i in range(1, len(level)):
if indices[level[i]] < idx:
left_level.append(level[i])
else:
right_level.append(level[i])
cur.left = f(left_level, inorder, indices)
cur.right = f(right_level, inorder, indices)
return cur
def buildTree(level, ino):
indices = dict((x, i) for i, x in enumerate(ino))
return f(level, ino, indices) | FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
curr = 0
n = len(ino)
root = Node(level[0])
curr += 1
q = [root]
while curr != n:
node = q.pop(0)
node.left = Node(level[curr])
curr += 1
q.append(node.left)
if curr != n:
node.right = Node(level[curr])
curr += 1
q.append(node.right)
return root | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def construct_tree(levelorder, inorder):
if len(levelorder) == 0:
return None
cur_node = Node(levelorder[0])
for i in range(0, len(inorder)):
if cur_node.data == inorder[i]:
cur_index = i
left_inorder = inorder[0:cur_index]
right_inorder = inorder[cur_index + 1 :]
left_level = []
right_level = []
for node in levelorder:
if node in left_inorder:
left_level.append(node)
for node in levelorder:
if node in right_inorder:
right_level.append(node)
cur_node.left = construct_tree(left_level, left_inorder)
cur_node.right = construct_tree(right_level, right_inorder)
return cur_node
def buildTree(level, ino):
return construct_tree(level, ino) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
start = 0
end = len(ino) - 1
if start > end:
return -1
Hash = search(ino)
root = tobinarytree(level, ino, start, end, Hash)
return root
def tobinarytree(level, ino, start, end, Hash):
if start > end:
return None
data = ino[(start + end) // 2]
root = Node(data)
index = Hash[data]
if start == None:
return root
root.left = tobinarytree(level, ino, start, index - 1, Hash)
root.right = tobinarytree(level, ino, index + 1, end, Hash)
return root
def search(inorder):
Hash = {}
for i in range(0, len(inorder)):
Hash[inorder[i]] = i
return Hash | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def func(level, ino, mp):
if len(level) < 1:
return
ele = level[0]
root = Node(ele)
if len(level) == 1:
return root
root_ix = mp[ele]
l_level = []
r_level = []
for i in range(len(level)):
if mp[level[i]] < root_ix:
l_level.append(level[i])
elif mp[level[i]] > root_ix:
r_level.append(level[i])
if root_ix != 0:
root.left = func(l_level, ino, mp)
if root_ix != len(ino) - 1:
root.right = func(r_level, ino, mp)
return root
def buildTree(level, ino):
mp = {ino[i]: i for i in range(len(ino))}
return func(level, ino, mp) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | class makenode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def buildTree1(inorder, levelorder, iStart, iEnd, n):
if n <= 0:
return None
root = makenode(levelorder[0])
index = -1
for i in range(iStart, iEnd + 1):
if levelorder[0] == inorder[i]:
index = i
break
s = set()
for i in range(iStart, index):
s.add(inorder[i])
lLevel = [] * len(s)
rLevel = [] * (iEnd - iStart - len(s))
for i in range(1, n):
if levelorder[i] in s:
lLevel.append(levelorder[i])
else:
rLevel.append(levelorder[i])
root.left = buildTree1(inorder, lLevel, iStart, index - 1, index - iStart)
root.right = buildTree1(inorder, rLevel, index + 1, iEnd, iEnd - index)
return root
def buildTree(level, ino):
return buildTree1(ino, level, 0, n - 1, n) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, inorder):
if not level or not inorder:
return None
root = Node(level[0])
root_inorder_index = inorder.index(level[0])
left_inorder = set(inorder[:root_inorder_index])
right_inorder = set(inorder[root_inorder_index + 1 :])
level_order_left = [i for i in level if i in left_inorder]
level_order_right = [i for i in level if i in right_inorder]
root.left = buildTree(level_order_left, inorder[:root_inorder_index])
root.right = buildTree(level_order_right, inorder[root_inorder_index + 1 :])
return root | FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if not level:
return None
if not ino:
return None
curr = level[0]
root = Node(curr)
i = ino.index(curr)
li = set(ino[:i])
lef = []
for ele in level:
if ele in li:
lef.append(ele)
li = set(ino[i + 1 :])
ler = []
for ele in level:
if ele in li:
ler.append(ele)
root.left = buildTree(lef, ino[:i])
root.right = buildTree(ler, ino[i + 1 :])
return root | FUNC_DEF IF VAR RETURN NONE IF VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if not ino or not level:
return None
root = Node(level[0])
index = ino.index(root.data)
leftTree = ino[:index]
rightTree = ino[index + 1 :]
levelLeft, levelRight = [], []
for value in level[1:]:
if value in leftTree:
levelLeft.append(value)
elif value in rightTree:
levelRight.append(value)
root.left = buildTree(levelLeft, leftTree)
root.right = buildTree(levelRight, rightTree)
return root | FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def construct(in_order, lvl_order):
if len(in_order) == 0 or len(lvl_order) == 0:
return None
res = Node(lvl_order.pop(0))
q = [res]
in_ = [in_order]
while len(q) > 0:
root = q.pop(0)
in_order = in_.pop(0)
if root.data in in_order:
root_index = in_order.index(root.data)
left_tree_in_order = in_order[:root_index]
right_tree_in_order = in_order[root_index + 1 :]
if len(left_tree_in_order) > 0:
root.left = Node(lvl_order.pop(0))
q.append(root.left)
in_.append(left_tree_in_order)
if len(right_tree_in_order) > 0:
root.right = Node(lvl_order.pop(0))
q.append(root.right)
in_.append(right_tree_in_order)
return res
def buildTree(level, ino):
root = construct(ino, level)
return root | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def helper(level, ino, in_left, in_right, mapping):
if in_left > in_right:
return None
root = Node(level[0])
in_idx = mapping[level[0]]
left_level = []
right_level = []
for ele in level:
idx = mapping[ele]
if idx >= in_left and idx < in_idx:
left_level.append(ele)
elif idx > in_idx and idx <= in_right:
right_level.append(ele)
root.left = helper(left_level, ino, in_left, in_idx - 1, mapping)
root.right = helper(right_level, ino, in_idx + 1, in_right, mapping)
return root
def buildTree(level, ino):
mapping = {}
for ele in level:
for i in range(len(ino)):
if ino[i] == ele:
mapping[ele] = i
break
return helper(level, ino, 0, len(ino) - 1, mapping) | FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def f(l, r, inorder: list, indices: dict):
if l > r:
return None
min_idx = l
for i in range(l + 1, r + 1):
if indices[inorder[i]] < indices[inorder[min_idx]]:
min_idx = i
cur = Node(inorder[min_idx])
cur.left = f(l, min_idx - 1, inorder, indices)
cur.right = f(min_idx + 1, r, inorder, indices)
return cur
def buildTree(level, ino):
indices = dict((x, i) for i, x in enumerate(level))
return f(0, len(ino) - 1, ino, indices) | FUNC_DEF VAR VAR IF VAR VAR RETURN NONE ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
LevelOrder = level
InOrder = ino
LevelOrderMap = {}
for i in LevelOrder:
LevelOrderMap[i] = 0
def MakeTree(LevelOrder, InOrder, LevelOrderMap):
if LevelOrder == []:
return None
root = Node(LevelOrder[0])
for i in range(0, len(InOrder)):
if InOrder[i] == LevelOrder[0]:
break
LSTINO = InOrder[:i]
LSTLVL = []
for keys in LevelOrderMap:
if keys in LSTINO:
LSTLVL.append(keys)
RSTINO = InOrder[i + 1 :]
RSTLVL = []
for keys in LevelOrderMap:
if keys in RSTINO:
RSTLVL.append(keys)
root.left = MakeTree(LSTLVL, LSTINO, LevelOrderMap)
root.right = MakeTree(RSTLVL, RSTINO, LevelOrderMap)
return root
ans = []
def PreOrder(Node):
nonlocal ans
if Node == None:
return
ans.append(Node.data)
PreOrder(Node.left)
PreOrder(Node.right)
root = MakeTree(LevelOrder, InOrder, LevelOrderMap)
PreOrder(root)
for i in ans:
print(i, end=" ")
return | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR LIST RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING RETURN |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class ConstructTree:
def __init__(self):
self.dict_ = dict()
def ConstructMap(self, level):
for i in range(0, len(level)):
key = level[i]
self.dict_[key] = i
def construct(self, in_, level, start, end):
if start > end:
return None
min_index = start
for i in range(start + 1, end + 1):
temp = in_[i]
if self.dict_.get(in_[min_index]) > self.dict_.get(temp):
min_index = i
root = Node(in_[min_index])
if start == end:
return root
root.left = self.construct(in_, level, start, min_index - 1)
root.right = self.construct(in_, level, min_index + 1, end)
return root
def buildTree(level, ino):
obj = ConstructTree()
obj.ConstructMap(level)
root = obj.construct(ino, level, 0, len(ino) - 1)
return root | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def ff(l, i):
if not l:
return
y = i.index(l[0])
r = Node(l[0])
c = []
d = []
for j in l:
if j in i[:y]:
c.append(j)
if j in i[y + 1 :]:
d.append(j)
r.left = ff(c, i[:y])
r.right = ff(d, i[y + 1 :])
return r
def buildTree(l, i):
return ff(l, i)
class Node:
def __init__(self, key):
self.data = key
self.left = None
self.right = None
def preOrd(root):
if not root:
return
print(root.data, end=" ")
preOrd(root.left)
preOrd(root.right)
if __name__ == "__main__":
tcs = int(input())
for _ in range(tcs):
n = int(input())
InOrd = [int(x) for x in input().split()]
LvlOrd = [int(x) for x in input().split()]
root = buildTree(LvlOrd, InOrd)
preOrd(root)
print() | FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def find(ino, lev):
for i in range(len(ino)):
if ino[i] == lev:
return i
def buildTree(levo, ino):
if not ino:
return
count = find(ino, levo[0])
node = Node(levo.pop(0))
new_level = []
for i in range(len(levo) - 1, -1, -1):
if levo[i] in ino[:count]:
new_level.insert(0, levo.pop(i))
node.left = buildTree(new_level, ino[:count])
node.right = buildTree(levo, ino[count + 1 :])
return node | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if not level or not ino:
return
new_node = Node(level[0])
idx = ino.index(level[0])
left_ino = ino[:idx]
left_level = [x for x in level if x in left_ino]
new_node.left = buildTree(left_level, left_ino)
right_ino = ino[idx + 1 :]
right_level = [x for x in level if x in right_ino]
new_node.right = buildTree(right_level, right_ino)
return new_node | FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def f(ino, i, j, d):
if i > j:
return
mn = i, ino[i]
for k in range(i, j + 1):
if d[ino[k]] < d[mn[1]]:
mn = k, ino[k]
root = Node(mn[1])
root.left = f(ino, i, mn[0] - 1, d)
root.right = f(ino, mn[0] + 1, j, d)
return root
def buildTree(level, ino):
d = dict()
for i in range(len(level)):
d[level[i]] = i
return f(ino, 0, len(ino) - 1, d) | FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
def build(ino):
if not ino:
return None
else:
for ele in level:
if ele in ino:
idx = ino.index(ele)
root = Node(ele)
break
root.left = build(ino[0:idx])
root.right = build(ino[idx + 1 : len(ino)])
return root
return build(ino) | FUNC_DEF FUNC_DEF IF VAR RETURN NONE FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
root = None
n = len(level)
if n <= 0:
return None
nodeList = []
for i in level:
nodeList.append(Node(i))
for i in range(n // 2):
node = nodeList[i]
next_left_node = 2 * i + 1
if next_left_node < n:
node.left = nodeList[next_left_node]
next_right_node = next_left_node + 1
if next_right_node < n:
node.right = nodeList[next_right_node]
return nodeList[0] | FUNC_DEF ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NONE ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(levelorder, inorder):
n = len(inorder)
map = dict()
for i in range(n):
map[inorder[i]] = i + 1
def tempfunc(li, ri, levelorder):
nonlocal map, inorder
if li > ri:
return None
if li == ri:
return Node(inorder[li - 1])
k = map[levelorder[0]]
tempMap = dict()
for i in range(li, k):
tempMap[inorder[i - 1]] = 1
for i in range(k + 1, ri + 1):
tempMap[inorder[i - 1]] = 2
leftLevelList = []
rightLevelList = []
for i in levelorder:
if tempMap.get(i) == 1:
leftLevelList.append(i)
elif tempMap.get(i) == 2:
rightLevelList.append(i)
left = tempfunc(li, k - 1, leftLevelList)
right = tempfunc(k + 1, ri, rightLevelList)
new = Node(levelorder[0])
new.left = left
new.right = right
return new
return tempfunc(1, n, levelorder) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NONE IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
return constructTree_levelorder_inorder(level, ino)
def constructTree_levelorder_inorder(levelorder, inorder):
if len(inorder) == 0:
return None
rootData = -1
rootIndex = -1
for i in levelorder:
if i in inorder:
rootData = i
rootIndex = inorder.index(i)
break
if rootIndex == -1:
return None
root = Node(rootData)
root.left = constructTree_levelorder_inorder(levelorder, inorder[:rootIndex])
root.right = constructTree_levelorder_inorder(levelorder, inorder[rootIndex + 1 :])
return root | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if not level:
return
root = Node(level[0])
index = 1
queue = [root]
while index < len(level):
newLevel = []
for node in queue:
counter = 2
left = True
while counter > 0 and index < len(level):
newNode = Node(level[index])
if left:
node.left = newNode
else:
node.right = newNode
index += 1
counter -= 1
left = not left
newLevel.append(newNode)
queue.clear()
if newLevel:
queue.extend(newLevel)
return root | FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def f(level, inorder):
if len(inorder) == 0:
return None
cur = Node(level[0])
idx = inorder.index(level[0])
left_level, right_level = [], []
for i in range(1, len(level)):
if inorder.index(level[i]) < idx:
left_level.append(level[i])
else:
right_level.append(level[i])
cur.left = f(left_level, inorder[:idx])
cur.right = f(right_level, inorder[idx + 1 :])
return cur
def buildTree(level, ino):
return f(level, ino) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
return solve(level, 0)
def solve(level1, i):
root = None
if i < len(level1):
root = Node(level1[i])
root.left = solve(level1, 2 * i + 1)
root.right = solve(level1, 2 * i + 2)
return root | FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NONE IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if len(ino) == 0:
return None
node = Node(level[0])
hasset = set()
left_level = []
right_level = []
i = 0
idx = ino.index(level[0])
while i < idx:
hasset.add(ino[i])
i += 1
for ele in level[1:]:
if ele in hasset:
left_level.append(ele)
else:
right_level.append(ele)
node.left = buildTree(left_level, ino[:idx])
node.right = buildTree(right_level, ino[idx + 1 :])
return node | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if len(level) != len(ino):
return None
d = dict()
for i in range(len(ino)):
d[ino[i]] = i
def vfx(ino, isi, iei, level, d):
if isi > iei:
return None
root = Node(level[0])
if len(level) == 1:
return root
index = d[level[0]]
lofls = []
lofrs = []
s = dict()
for i in range(isi, index):
s[ino[i]] = i
for i in range(1, len(level)):
ele = level[i]
if len(s) != 0 and s.get(ele) != None:
lofls.append(ele)
s.pop(ele)
else:
lofrs.append(ele)
root.left = vfx(ino, isi, index - 1, lofls, d)
root.right = vfx(ino, index + 1, iei, lofrs, d)
return root
return vfx(ino, 0, len(ino) - 1, level, d) | FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def constructTree(level, inorder):
if len(level) == 0:
return
nodeVal = level[0]
node = Node(nodeVal)
for index, val in enumerate(inorder):
if val == nodeVal:
break
left_inorder = inorder[:index]
right_inorder = inorder[index + 1 :]
left_level = list()
right_level = list()
for nodeVal in level:
if nodeVal in left_inorder:
left_level.append(nodeVal)
elif nodeVal in right_inorder:
right_level.append(nodeVal)
node.left = constructTree(left_level, left_inorder)
node.right = constructTree(right_level, right_inorder)
return node
def buildTree(level, inorder):
return constructTree(level, inorder) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def find(ino, level, m, n):
arr = []
for i in range(len(level)):
if level[i] in ino[m:n]:
arr.append(level[i])
return arr
def buildTree(level, ino):
if len(level) == 0:
return None
root = Node(level[0])
root.left = None
root.right = None
ele_to_search = level[0]
for i in range(len(level)):
if ino[i] == ele_to_search:
break
leftList = find(ino, level, 0, i)
rightList = find(ino, level, i + 1, len(level))
v1 = ino[:i]
v11 = ino[i + 1 :]
root.left = buildTree(leftList, v1)
root.right = buildTree(rightList, v11)
return root | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
if not level and not ino:
return None
else:
root = level[0]
r = Node(root)
idx = ino.index(root)
il = ino[0:idx]
ir = ino[idx + 1 :]
ll = []
lr = []
for i in range(1, len(level)):
if level[i] in il:
ll.append(level[i])
else:
lr.append(level[i])
r.left = buildTree(ll, il)
r.right = buildTree(lr, ir)
return r | FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def preorder_traversal(root, res):
if root == None:
return
res.append(root.data)
preorder_traversal(root.left, res)
preorder_traversal(root.right, res)
def aux_func(level, ino, ino_idx_dict, il, ir):
if not level:
return None
val = level[0]
root = Node(val)
idx = ino_idx_dict[val]
lsize = idx - il
rsize = ir - il
left_level = []
right_level = []
for l in level:
ino_idx = ino_idx_dict[l]
if ino_idx >= il and ino_idx < idx:
left_level.append(l)
if ino_idx > idx and ino_idx <= ir:
right_level.append(l)
if lsize > 0:
root.left = aux_func(left_level, ino, ino_idx_dict, il, il + lsize - 1)
if rsize > 0:
root.right = aux_func(right_level, ino, ino_idx_dict, idx + 1, ir)
return root
def buildTree(level, ino):
N = len(ino)
ino_idx_dict = {ino[i]: i for i in range(N)}
root = aux_func(level, ino, ino_idx_dict, 0, N - 1)
res = []
preorder_traversal(root, res)
for r in res:
print(r, end=" ") | FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
n = len(ino)
if n == 0:
return None
data = level[0]
index = -1
for i in range(n):
if ino[i] == data:
index = i
break
if index == -1:
return None
root = Node(data)
li = ino[:index]
ri = ino[index + 1 :]
ll = []
rl = []
for i in range(1, len(level)):
if level[i] in li:
ll.append(level[i])
else:
rl.append(level[i])
root.left = buildTree(ll, li)
root.right = buildTree(rl, ri)
return root | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def buildTree(level, ino):
n = len(level)
return buildTreeUtil(level, ino, 0, n - 1, n)
def buildTreeUtil(level, ino, start, end, n):
if n <= 0 or start > end:
return None
root = level[0]
root_index = get_index(ino, start, end, root)
in_left_set = set(ino[start:root_index])
left_level, right_level = [], []
for i in range(1, n, 1):
if level[i] in in_left_set:
left_level.append(level[i])
else:
right_level.append(level[i])
root_node = Node(root)
root_node.left = buildTreeUtil(
left_level, ino, start, root_index - 1, root_index - start
)
root_node.right = buildTreeUtil(
right_level, ino, root_index + 1, end, end - root_index
)
return root_node
def get_index(ino, start, end, root):
for i in range(start, end + 1, 1):
if ino[i] == root:
return i | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR RETURN VAR |
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree and return the root Node.
Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements is respective arrays. Second and third line consists of arrays containing Inorder and Level-order traversal respectively.
Output:
Single line output, print the preOrder traversal of array.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
3
1 0 2
0 1 2
7
3 1 4 0 5 2 6
0 1 2 3 4 5 6
Output:
0 1 2
0 1 3 4 2 5 6 | def build(ino, start, end, d):
if start > end:
return None
if start == end:
return Node(ino[start])
idx = start
for i in range(start + 1, end + 1):
if d[ino[i]] < d[ino[idx]]:
idx = i
root = Node(ino[idx])
root.left = build(ino, start, idx - 1, d)
root.right = build(ino, idx + 1, end, d)
return root
def buildTree(level, ino):
n = len(ino)
d = {}
for i in range(n):
d[level[i]] = i
return build(ino, 0, n - 1, d) | FUNC_DEF IF VAR VAR RETURN NONE IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.