description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
n, m = map(int, input().split()) l = [list(map(int, input())) for _ in range(n)] pre = [([0] * m) for _ in range(n)] for _ in range(int(input())): x1, y1, x2, y2 = map(int, input().split()) x1 -= 1 y1 -= 1 x2 -= 1 y2 -= 1 pre[x1][y1] += 1 if x2 + 1 < n: pre[x2 + 1][y1] -= 1 if y2 + 1 < m: pre[x1][y2 + 1] -= 1 if x2 + 1 < n and y2 + 1 < m: pre[x2 + 1][y2 + 1] += 1 for i in range(1, n): for j in range(m): pre[i][j] += pre[i - 1][j] for i in range(n): for j in range(1, m): pre[i][j] += pre[i][j - 1] for i in range(n): for j in range(m): print((pre[i][j] + l[i][j]) % 2, end="") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
n, m = map(int, input().split()) matrix = [list(input()) for _ in range(n)] q = int(input()) prefix_sum = [([0] * (m + 2)) for _ in range(n + 2)] for _ in range(q): x1, y1, x2, y2 = map(int, input().split()) prefix_sum[x1][y1] += 1 prefix_sum[x2 + 1][y2 + 1] += 1 prefix_sum[x1][y2 + 1] -= 1 prefix_sum[x2 + 1][y1] -= 1 flip = {"0": "1", "1": "0"} for col in range(1, m + 1): for row in range(1, n + 1): prefix_sum[row][col] += prefix_sum[row - 1][col] for row in range(1, n + 1): for col in range(1, m + 1): prefix_sum[row][col] += prefix_sum[row][col - 1] if prefix_sum[row][col] & 1: matrix[row - 1][col - 1] = flip[matrix[row - 1][col - 1]] for row in range(n): print("".join(matrix[row]))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
import sys n, m = map(int, sys.stdin.readline().strip().split()) arr = [] for i in range(n): arr.append(list(input())) temp = [[(0) for i in range(m + 1)] for j in range(n + 1)] q = int(sys.stdin.readline()) while q: q -= 1 x1, y1, x2, y2 = map(int, sys.stdin.readline().strip().split()) x1 -= 1 x2 -= 1 y1 -= 1 y2 -= 1 temp[x1][y1] += 1 temp[x1][y2 + 1] -= 1 temp[x2 + 1][y1] -= 1 temp[x2 + 1][y2 + 1] += 1 for i in range(n + 1): for j in range(m + 1): if j == 0: temp[i][j] = temp[i][j] else: temp[i][j] += temp[i][j - 1] for j in range(m + 1): for i in range(n + 1): if i == 0: temp[i][j] = temp[i][j] else: temp[i][j] += temp[i - 1][j] for i in range(n): for j in range(m): if temp[i][j] % 2 == 1: if arr[i][j] == "0": print("1", end="") else: print("0", end="") else: print(arr[i][j], end="") print()
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
n, m = map(int, input().split()) arr = [] for _ in range(n): s = input() a = [int(x) for x in s] arr.append(a) Q = int(input()) ans = [[(0) for i in range(m + 1)] for j in range(n + 1)] for i in range(Q): x1, y1, x2, y2 = map(int, input().split()) x1 -= 1 y1 -= 1 x2 -= 1 y2 -= 1 ans[x1][y1] += 1 ans[x2 + 1][y2 + 1] += 1 ans[x2 + 1][y1] -= 1 ans[x1][y2 + 1] -= 1 for j in range(m + 1): for i in range(1, n + 1): ans[i][j] = ans[i - 1][j] + ans[i][j] for i in range(n + 1): for j in range(1, m + 1): ans[i][j] = ans[i][j - 1] + ans[i][j] for i in range(n): for j in range(m): arr[i][j] = (ans[i][j] + arr[i][j]) % 2 for i in range(n): for j in range(m): print(arr[i][j], end="") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
n, m = map(int, input().split()) mat = [] for _ in range(n): s = input() l = list(map(int, s)) mat.append(l) pref = [[(0) for _ in range(m + 1)] for _ in range(n + 1)] p = [[(0) for _ in range(m)] for _ in range(n)] add = [[(0) for _ in range(m)] for _ in range(n)] for _ in range(int(input())): x1, y1, x2, y2 = map(int, input().split()) pref[x1 - 1][y1 - 1] += 1 pref[x2][y1 - 1] -= 1 pref[x1 - 1][y2] -= 1 pref[x2][y2] += 1 for i in range(n): for j in range(m): if i == 0: p[i][j] = pref[i][j] else: p[i][j] = p[i - 1][j] + pref[i][j] for i in range(n): for j in range(m): if j == 0: add[i][j] = p[i][j] else: add[i][j] = add[i][j - 1] + p[i][j] for i in range(n): for j in range(m): mat[i][j] += add[i][j] mat[i][j] %= 2 for i in range(n): for j in range(m): print(mat[i][j], end="") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
def issafe(a, b, n, m): if 0 <= a < n and 0 <= b < m: return True return False def main(): n, m = map(int, input().split()) mat = [list(map(int, list(input()))) for i in range(n)] qmat = [[(0) for i in range(m)] for j in range(n)] q = int(input()) query = [list(map(int, input().split())) for i in range(q)] for k in query: i1, j1, i2, j2 = map(lambda x: x - 1, k) qmat[i1][j1] += 1 if issafe(i2 + 1, j2 + 1, n, m): qmat[i2 + 1][j2 + 1] += 1 if issafe(i1, j2 + 1, n, m): qmat[i1][j2 + 1] += -1 if issafe(i2 + 1, j1, n, m): qmat[i2 + 1][j1] += -1 for i in range(n - 1): for j in range(m): qmat[i + 1][j] += qmat[i][j] for i in range(n): for j in range(m - 1): qmat[i][j + 1] += qmat[i][j] for i in range(n): for j in range(m): qmat[i][j] = (qmat[i][j] + mat[i][j]) % 2 for i in qmat: print(*i, sep="") main()
FUNC_DEF IF NUMBER VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
n, m = map(int, input().split()) arr = [] for _ in range(n): arr.append(list(map(int, list(input())))) q = int(input()) flip = [([0] * m) for _ in range(n)] for _ in range(q): x1, y1, x2, y2 = map(int, input().split()) flip[x1 - 1][y1 - 1] += 1 if x2 - 1 < n - 1: flip[x2 - 1 + 1][y1 - 1] -= 1 if y2 - 1 < m - 1: flip[x1 - 1][y2 - 1 + 1] -= 1 if x2 - 1 < n - 1 and y2 - 1 < m - 1: flip[x2 - 1 + 1][y2 - 1 + 1] -= 1 for i in range(1, n): for j in range(m): flip[i][j] += flip[i - 1][j] for i in range(n): for j in range(1, m): flip[i][j] += flip[i][j - 1] for i in range(n): for j in range(m): if flip[i][j] % 2: print(arr[i][j] ^ 1, end="") else: print(arr[i][j], end="") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
n, m = map(int, input().split()) MX = list() for i in range(n): YYY = str(input()) yy = list(YYY) a = [] for j in range(m): a.append(yy[j]) MX.append(a) q = int(input()) dp = [[(0) for i in range(m)] for i in range(n)] for _ in range(q): x, y, x2, y2 = map(int, input().split()) dp[x - 1][y - 1] += 1 if x2 < n: dp[x2][y - 1] -= 1 if y2 < m: dp[x - 1][y2] -= 1 if x2 < n and y2 < m: dp[x2][y2] -= 1 for i in range(n): for j in range(1, m): dp[i][j] += dp[i][j - 1] for i in range(m): for j in range(1, n): dp[j][i] += dp[j - 1][i] for i in range(n): for j in range(m): if dp[i][j] % 2 == 1: if MX[i][j] == "0": MX[i][j] = "1" else: MX[i][j] = "0" for i in range(n): for j in range(m): print(MX[i][j], end="") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
n, m = map(int, input().split()) mat = [] for i in range(n): me = [int(i) for i in list(input())] mat.append(me) dummy = [[(0) for i in range(m + 1)] for j in range(n + 1)] q = int(input()) for i in range(q): x1, y1, x2, y2 = map(int, input().split()) dummy[x1 - 1][y1 - 1] += 1 dummy[x2][y1 - 1] -= 1 dummy[x2][y2] += 1 dummy[x1 - 1][y2] -= 1 for i in range(n + 1): for j in range(m + 1): if j == 0: continue else: dummy[i][j] += dummy[i][j - 1] for j in range(m + 1): for i in range(n + 1): if i == 0: continue else: dummy[i][j] += dummy[i - 1][j] for i in range(n): for j in range(m): print((mat[i][j] + dummy[i][j] + 2) % 2, end="") print() print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
n, m = map(int, input().split()) matrix = [] for _ in range(n): matrix.append(list(map(int, list(input())))) extra = [[(0) for _ in range(m + 1)] for _ in range(n + 1)] for _ in range(int(input())): x1, y1, x2, y2 = map(int, input().split()) extra[x1 - 1][y1 - 1] += 1 extra[x1 - 1][y2] -= 1 extra[x2][y2] += 1 extra[x2][y1 - 1] -= 1 for j in range(m): for i in range(1, n): extra[i][j] += extra[i - 1][j] for i in range(n): for j in range(1, m): extra[i][j] += extra[i][j - 1] for i in range(n): for j in range(m): matrix[i][j] += extra[i][j] matrix[i][j] %= 2 print(matrix[i][j], end="") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
def flip(dp, x1, y1, x2, y2, n, m): dp[x1][y1] += 1 if y2 + 1 < m: dp[x1][y2 + 1] -= 1 if x2 + 1 < n: dp[x2 + 1][y1] -= 1 if x2 + 1 < n and y2 + 1 < m: dp[x2 + 1][y2 + 1] += 1 def main(): n, m = map(int, input().split()) matrix = [] for i in range(n): matrix.append(list(map(int, input()))) dp = [[(0) for j in range(m)] for i in range(n)] q = int(input()) for i in range(q): x1, y1, x2, y2 = map(int, input().split()) x1 -= 1 y1 -= 1 x2 -= 1 y2 -= 1 flip(dp, x1, y1, x2, y2, n, m) for i in range(n): for j in range(1, m): dp[i][j] += dp[i][j - 1] for i in range(1, n): for j in range(m): dp[i][j] += dp[i - 1][j] for i in range(n): for j in range(m): if dp[i][j] % 2 == 0: print(matrix[i][j], end="") else: print(int(not matrix[i][j]), end="") print() main()
FUNC_DEF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
n, m = map(int, input().split()) mat = [] for x in range(n): row = input() mat.append(list(row)) q = int(input()) pref = [[(0) for _ in range(m)] for _ in range(n)] for _ in range(q): x1, y1, x2, y2 = map(int, input().split()) x1 -= 1 x2 -= 1 y1 -= 1 y2 -= 1 pref[x1][y1] += 1 if x2 + 1 < n and y2 + 1 < m: pref[x2 + 1][y2 + 1] += 1 if x2 + 1 < n: pref[x2 + 1][y1] -= 1 if y2 + 1 < m: pref[x1][y2 + 1] -= 1 for i in range(m): for j in range(1, n): pref[j][i] += pref[j - 1][i] for i in range(n): for j in range(1, m): pref[i][j] += pref[i][j - 1] for x in range(n): for y in range(m): if pref[x][y] % 2: mat[x][y] = str(int(mat[x][y]) ^ 1) for x in mat: print("".join(x))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
[n, m] = [int(x) for x in input().split()] arr = [] for i in range(n): arr.append([int(x) for x in list(input())]) prefix = [([0] * (m + 1)) for i in range(n + 1)] q = int(input()) for i in range(q): [x1, y1, x2, y2] = [int(x) for x in input().split()] x1 -= 1 y1 -= 1 x2 -= 1 y2 -= 1 prefix[x1][y1] += 1 prefix[x2 + 1][y1] -= 1 prefix[x1][y2 + 1] -= 1 prefix[x2 + 1][y2 + 1] += 1 for i in range(m + 1): for j in range(1, n + 1): prefix[j][i] += prefix[j - 1][i] for i in range(n + 1): for j in range(1, m + 1): prefix[i][j] += prefix[i][j - 1] for i in range(n): ans = "" for j in range(m): ans += str((arr[i][j] + prefix[i][j]) % 2) print(ans)
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
n, m = map(int, input().split()) a = [[int(i) for i in str(input())] for j in range(n)] b = [[(0) for i in range(m + 1)] for j in range(n + 1)] for q in range(int(input())): x1, y1, x2, y2 = map(int, input().split()) b[x1 - 1][y1 - 1] += 1 b[x1 - 1][y2] -= 1 b[x2][y1 - 1] -= 1 b[x2][y2] += 1 for i in range(n + 1): for j in range(1, m + 1): b[i][j] += b[i][j - 1] for i in range(1, n + 1): for j in range(m + 1): b[i][j] += b[i - 1][j] for i in range(n): for j in range(m): a[i][j] += b[i][j] a[i][j] %= 2 print("".join(str(k) for k in a[i]))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
import sys try: def main(): t = 1 while t: n, m = [int(x) for x in sys.stdin.readline().split()] a = [] update = [] for i in range(n): tmp = sys.stdin.readline() a.append(tmp) update.append([(0) for j in range(m + 1)]) update.append([(0) for j in range(m + 1)]) q = int(sys.stdin.readline()) while q: x1, y1, x2, y2 = [int(z) for z in sys.stdin.readline().split()] update[x1][y1] += 1 if x2 < n: update[x2 + 1][y1] += -1 if y2 < m: update[x1][y2 + 1] += -1 if x2 < n and y2 < m: update[x2 + 1][y2 + 1] += 1 q -= 1 for i in range(1, n + 1): for j in range(1, m + 1): update[i][j] += update[i][j - 1] for j in range(1, m + 1): for i in range(1, n + 1): update[i][j] += update[i - 1][j] for i in range(1, n + 1): for j in range(1, m + 1): if update[i][j] % 2 == 0: sys.stdout.write(a[i - 1][j - 1]) else: sys.stdout.write(str(1 - (ord(a[i - 1][j - 1]) - 48))) sys.stdout.write("\n") t -= 1 main() except Exception as e: sys.stdout.write("ErrOR : " + str(e))
IMPORT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
n, m = map(int, input().split()) a = [] b = [0] * (m + 1) for i in range(n + 1): a.append(b[:]) p = [] for i in range(n): r = input() q = [] for j in range(len(r)): q.append(int(r[j])) p.append(q[:]) q = int(input()) while q: i, j, k, l = map(int, input().split()) a[i - 1][j - 1] += 1 a[i - 1][l] -= 1 a[k][j - 1] -= 1 a[k][l] += 1 q -= 1 c = 0 for j in range(m + 1): for i in range(n + 1): c += a[i][j] a[i][j] = c for i in range(n): for j in range(m): c += a[i][j] if c % 2 == 1: if p[i][j] == 1: p[i][j] = 0 else: p[i][j] = 1 c += a[i][m] for i in range(n): for j in range(m): print(p[i][j], end="") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≀ $i$ ≀ $N$) and column $j$ ($1$ ≀ $j$ ≀ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ β€” the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ β€” the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β€” coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
r, c = list(map(int, input().split())) A = [] dp = [[(0) for i in range(c + 1)] for j in range(r + 1)] for i in range(r): A.append([int(j) for j in list(input())]) q = int(input()) for i in range(q): x1, y1, x2, y2 = list(map(int, input().split())) x1, y1, x2, y2 = x1 - 1, y1 - 1, x2 - 1, y2 - 1 dp[x1][y1] += 1 dp[x2 + 1][y2 + 1] += 1 dp[x1][y2 + 1] -= 1 dp[x2 + 1][y1] -= 1 for i, v1 in enumerate(A): for j, v2 in enumerate(v1): if i == 0 and j == 0: pass elif i == 0: dp[i][j] += dp[i][j - 1] elif j == 0: dp[i][j] += dp[i - 1][j] else: dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] if dp[i][j] % 2 != 0: A[i][j] = 1 - A[i][j] for i in A: for j in i: print(j, end="") print()
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): newRoot = self.searchForNewRoot(root, k) if newRoot == None: return None self.trimTree(newRoot, k) return newRoot def searchForNewRoot(self, root, k): curr = root while curr.data >= k: if curr.left == None: return None curr = curr.left return curr def trimTree(self, root, k): curr = root parent = None while curr != None: if curr.data >= k: parent.right = curr.left curr = curr.left else: parent = curr curr = curr.right
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR IF VAR NONE RETURN NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def root_lefts(self, root): while root.right: root = root.right return root def util(self, root, x): if not root: return root if root.data == x: return root.left if root.data > x: return self.util(root.left, x) if root.data < x: root.right = self.util(root.right, x) return root def deleteNode(self, root, k): return self.util(root, k)
CLASS_DEF FUNC_DEF WHILE VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): while root and root.data >= k: root = root.left start = root while root: while root.right and root.right.data >= k: root.right = root.right.left root = root.right return start
CLASS_DEF FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def __init__(self): self.inorder = [] def InOrder(self, Node): if Node == None: return self.InOrder(Node.left) self.inorder.append(Node.data) self.InOrder(Node.right) return def MakeTree(self, inorder): if inorder == []: return None mid = len(inorder) // 2 root = Node(inorder[mid]) root.left = self.MakeTree(inorder[:mid]) root.right = self.MakeTree(inorder[mid + 1 :]) return root def deleteNode(self, root, k): self.InOrder(root) for i in range(0, len(self.inorder)): if self.inorder[i] < k: continue else: self.inorder = self.inorder[:i] break return self.MakeTree(self.inorder)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF IF VAR LIST RETURN NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
node = None class Solution: def solve(self, root, k): if root is None: return None global head, itr if k <= root.data: if itr: itr.right = root.left self.solve(root.left, k) else: if head is None: head = root itr = head else: itr.right = root itr = root self.solve(root.right, k) def deleteNode(self, root, k): global head, itr head = itr = None self.solve(root, k) return head
ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NONE EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): while root.data >= k: if root.left == None and root.right == None: return root = root.left temp = root prev = None while temp != None: if temp.data < k: prev = temp temp = temp.right else: prev.right = temp.left temp = temp.left return root
CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR NONE VAR NONE RETURN ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): head = None head = self.delete(root, k, head) return head def delete(self, root, k, head): if not root: return if root.data < k: head = root head.left = self.delete(root.left, k, head.left) head.right = self.delete(root.right, k, head.right) else: head = self.delete(root.left, k, head) return head
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): if root == None: return if root.data >= k: return self.deleteNode(root.left, k) if root.data < k: root.right = self.deleteNode(root.right, k) return root
CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
def deep(root): cur = root while cur.left != None: cur = cur.left return cur def delete(root, key): if root is None: return None if root.data > key: root.left = delete(root.left, key) elif root.data < key: root.right = delete(root.right, key) else: if root.left is None: temp = root.right root = None return temp if root.right is None: temp = root.left root = None return temp temp = deep(root.right) root.data = temp.data root.right = delete(root.right, temp.data) return root class Solution: def inorder(self, root): res = [] q = [] cur = root while True: if cur is not None: q.append(cur) cur = cur.left elif q: cur = q.pop() res.append(cur.data) cur = cur.right else: break return res def deleteNode(self, root, k): ino = self.inorder(root) for i in ino: if i >= k: root = delete(root, i) return root
FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR WHILE NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def solve(self, root, k): if root == None: return None elif root.data >= k: l = self.solve(root.left, k) return l else: root.right = self.solve(root.right, k) return root def deleteNode(self, root, k): res = self.solve(root, k) return res
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): a = [] b = [] x = self.pre(root, a) a.sort() for i in a: if i < k: b.append(i) for i in b: print(i, end=" ") def pre(self, root, a): if root == None: return a.append(root.data) self.pre(root.left, a) self.pre(root.right, a) return a
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): if root == None: return root if root.data == k: return root.left elif root.data > k: temp = root.left return self.deleteNode(temp, k) else: root.right = self.deleteNode(root.right, k) return root
CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
import sys sys.setrecursionlimit(10**6) class Solution: def ino(self, r, l, k): if r is not None: self.ino(r.left, l, k) if r.data < k: l.append(Node(r.data)) self.ino(r.right, l, k) def deleteNode(self, root, k): l = [] self.ino(root, l, k) n = len(l) for i in range(n - 1): l[i].right = l[i + 1] return l[0]
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): if not root: return root if root.data < k: root.right = self.deleteNode(root.right, k) return root elif root.data > k: return self.deleteNode(root.left, k) else: return root.left
CLASS_DEF FUNC_DEF IF VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Delete: def getNodeAndParent(self, root, key): parent = None tmp = root while tmp: if tmp.data == key: return [tmp, parent] parent = tmp if tmp.data > key: tmp = tmp.left else: tmp = tmp.right return [None, None] def isLeaf(self, node): return not node.left and not node.right def deleteLeaf(self, node, parent): if parent.left == node: parent.left = None else: parent.right = None return node def isSingleChildNode(self, node): return not node.left and node.right or node.left and not node.right def deleteSingleChildNode(self, node, parent): if node.left: if parent.left == node: parent.left = node.left else: parent.right = node.left node.left = None else: if parent.left == node: parent.left = node.right else: parent.right = node.right node.right = None return node def deleteDoubleChildNode(self, node, parent): re_parent = node re_node = node.left while re_node.right: re_parent = re_node re_node = re_node.right if self.isLeaf(re_node): removed_node = self.deleteLeaf(re_node, re_parent) else: removed_node = self.deleteSingleChildNode(re_node, re_parent) if parent: if parent.left == node: parent.left = removed_node else: parent.right = removed_node removed_node.left = node.left removed_node.right = node.right return node def deleteRoot(self, root): if self.isLeaf(root): new_root = None elif self.isSingleChildNode(root): if root.left: new_root = root.left else: new_root = root.right else: new_root = root.left while new_root.right: new_root = new_root.right root = self.deleteDoubleChildNode(root, None) del root return new_root def deleteNodeBST(self, root, key: int): node, parent = self.getNodeAndParent(root, key) if not node: return root if not parent: root = self.deleteRoot(root) else: if self.isLeaf(node): removed_node = self.deleteLeaf(node, parent) elif self.isSingleChildNode(node): removed_node = self.deleteSingleChildNode(node, parent) else: removed_node = self.deleteDoubleChildNode(node, parent) del removed_node return root class Solution: def __init__(self) -> None: self.values = [] def getVals(self, root, k): if not root: return if root.data >= k: self.values.append(root.data) self.getVals(root.left, k) self.getVals(root.right, k) def deleteNode(self, root, k): delete_node = Delete() self.getVals(root, k) for key in self.values: root = delete_node.deleteNodeBST(root, key) return root
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR IF VAR VAR RETURN LIST VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN LIST NONE NONE FUNC_DEF RETURN VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE RETURN VAR FUNC_DEF RETURN VAR VAR VAR VAR FUNC_DEF IF VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR ASSIGN VAR NONE IF FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NONE VAR RETURN VAR FUNC_DEF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR RETURN VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST NONE FUNC_DEF IF VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, node, k): if node is None: return node node.left = self.deleteNode(node.left, k) node.right = self.deleteNode(node.right, k) if node.data >= k: return node.left return node
CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): return self.helper(root, k) or root def deleteNodeHelper(self, root): if root is None: return if root.left is None and root.right is None: del root return self.deleteNodeHelper(root.left) self.deleteNodeHelper(root.right) def helper(self, root, k): if root is None: return None if root.data >= k: newRoot = root.left self.deleteNodeHelper(root.right) del root return self.helper(newRoot, k) else: rightChild = self.helper(root.right, k) root.right = rightChild return root
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): def f(root, k): if not root: return None if root.data < k: root.right = f(root.right, k) else: root = f(root.left, k) return root return f(root, k)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def Delete(self, root, k): if root == None: return None l = self.Delete(root.left, k) r = self.Delete(root.right, k) if root.data < k: root.left = l root.right = r return root else: return l def deleteNode(self, root, k): root = self.Delete(root, k) return root
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): if not root: return root.left = self.deleteNode(root.left, k) root.right = self.deleteNode(root.right, k) if root.data >= k: root = delete(root) return root def delete(root): if not root.left and not root.right: root = None elif not root.left: root = root.right elif not root.right: root = root.left else: temp = minn(root.left) root.data = temp.data temp = delete(temp) return root def minn(root): while root.right: root = root.right return root
CLASS_DEF FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): if root == None: return if root.data >= k: return self.deleteNode(root.left, k) root.left = self.deleteNode(root.left, k) root.right = self.deleteNode(root.right, k) return root def delete(self, root): if root == None: return if root.left: root.data = root.left.data root.left = self.delete(root.left) elif root.right: root.data = root.right.data root.right = self.delete(root.right) else: del root return
CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Node: def __init__(self, val): self.right = None self.data = val self.left = None class Solution: def deleteNode(self, root, k): if root is None: return root.left = self.deleteNode(root.left, k) root.right = self.deleteNode(root.right, k) if root.data >= k: temp = root.left root = None return temp return root
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): nod = Node(0) def fun2(root): if root == None: return if root.data >= k: return fun2(root.left) root.right = fun2(root.right) return root return fun2(root)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def __init__(self): self.root = None def deleteNode(self, root, k): self.root = root return self.__delete_k_greater(root, key=k) def __delete_k_greater(self, node, key): if node and node.data >= key: if node == self.root: node = self.root = self.root.left else: node = node.left if node: return self.__delete_k_greater(node, key) elif node and node.data < key and node.right: node.right = self.__delete_k_greater(node.right, key) return node else: return node
CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def deleteNode(self, root, k): a = [] arr = inorder(root, a) for i in arr: if i >= k: w = deleteS(root, i) else: w = root return w def deleteS(root, k): if root is None: return root if root.data > k: root.left = deleteS(root.left, k) elif root.data < k: root.right = deleteS(root.right, k) else: if root.left is None: temp = root.right root = None return temp if root.right is None: temp = root.left root = None return temp temp = minValueNode(root.right) root.data = temp.data root.right = deleteS(root.right, temp.data) return root def minValueNode(node): current = node while current.left is not None: current = current.left return current def inorder(root, a): if root is not None: inorder(root.left, a) a.append(root.data) inorder(root.right, a) return a
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a value k, the task is to delete the nodes having values greater than or equal to k. Example 1: Input: 4 / \ 1 9 k = 2 Output: 1 Your Task: The task is to complete the function deleteNode() which takes root, k as the argument, and returns the root of tree after deleting values greater than or equal to k. The driver code will print the inorder traversal of the updated tree in output itself. Expected Time Complexity: O(Size of tree) Expected Auxiliary Space: O(1). Constraints: 1 <= T <= 100 1 <= N <= 10^{3} 1 <= A[] <= 10^{3} 1 <= k <= N
class Solution: def inorder1(self, root): if root is None: return print(root.data) self.inorder1(root.left) print(root.data) self.inorder1(root.right) print(root.data) def inorder(self, root, k): if root is None: return if root.left is None or root.right is None: return self.inorder(root.left, k) if root.right.data > k: root.right = None print("right") if root.left.data > k: self.inorder(root, k) print("left") else: print("left1") root.left = None self.inorder(root.right, k) def deleteNode(self, root, k): if root is None: return if root.data >= k: return self.deleteNode(root.left, k) root.right = self.deleteNode(root.right, k) return root
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a AVL tree and N values to be inserted in the tree. Write a function to insert a given value into the tree. Example 1: N = 3 Values to be inserted = {5,1,4} Input: Value to be inserted = 5 Output: 5 Input : Value to be inserted = 1 Output: 5 / 1 Input : Value to be inserted = 4 Output: 5 4 / LR rotation / \ 1 -----------> 1 5 \ 4 Your Task: You dont need to read input or print anything. Complete the function insertToAVL() which takes the root of the tree and the value of the node to be inserted as input parameters and returns the root of the modified tree. Note: The tree will be checked after each insertion. If it violates the properties of balanced BST, an error message will be printed followed by the inorder traversal of the tree at that moment. If instead all insertions are successful, inorder traversal of tree will be printed. Expected Time Complexity: O(log N) Expected Auxiliary Space: O(height of tree) Constraints: 1 ≀ N ≀ 500
class Solution: def getHeight(self, root): if root == None: return 0 return root.height def getBalance(self, root): if root == None: return 0 return self.getHeight(root.left) - self.getHeight(root.right) def leftRotate(self, root): y = root.right t2 = y.left root.right = t2 y.left = root root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right)) y.height = 1 + max(self.getHeight(y.left), self.getHeight(y.right)) return y def rightRotate(self, root): y = root.left t2 = y.right root.left = t2 y.right = root root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right)) y.height = 1 + max(self.getHeight(y.left), self.getHeight(y.right)) return y def insertToAVL(self, root, key): if not root: root = Node(key) return root elif key == root.data: return root elif key < root.data: root.left = self.insertToAVL(root.left, key) else: root.right = self.insertToAVL(root.right, key) root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right)) b = self.getBalance(root) if b > 1 and key < root.left.data: return self.rightRotate(root) if b < -1 and key > root.right.data: return self.leftRotate(root) if b > 1 and key > root.left.data: root.left = self.leftRotate(root.left) return self.rightRotate(root) if b < -1 and key < root.right.data: root.right = self.rightRotate(root.right) return self.leftRotate(root) return root
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR
Given a AVL tree and N values to be inserted in the tree. Write a function to insert a given value into the tree. Example 1: N = 3 Values to be inserted = {5,1,4} Input: Value to be inserted = 5 Output: 5 Input : Value to be inserted = 1 Output: 5 / 1 Input : Value to be inserted = 4 Output: 5 4 / LR rotation / \ 1 -----------> 1 5 \ 4 Your Task: You dont need to read input or print anything. Complete the function insertToAVL() which takes the root of the tree and the value of the node to be inserted as input parameters and returns the root of the modified tree. Note: The tree will be checked after each insertion. If it violates the properties of balanced BST, an error message will be printed followed by the inorder traversal of the tree at that moment. If instead all insertions are successful, inorder traversal of tree will be printed. Expected Time Complexity: O(log N) Expected Auxiliary Space: O(height of tree) Constraints: 1 ≀ N ≀ 500
class Solution: def insertToAVL(self, node, key): if node == None: return Node(key) else: if key < node.data: node.left = self.insertToAVL(node.left, key) elif key > node.data: node.right = self.insertToAVL(node.right, key) else: return node lh = self.nodeHeight(node.left) rh = self.nodeHeight(node.right) if lh > rh: node.height = 1 + lh else: node.height = 1 + rh balance = self.nodeHeight(node.left) - self.nodeHeight(node.right) if balance > 1 and key < node.left.data: return self.rRight(node) if balance < -1 and key > node.right.data: return self.rLeft(node) if balance > 1 and key > node.left.data: node.left = self.rLeft(node.left) return self.rRight(node) if balance < -1 and key < node.right.data: node.right = self.rRight(node.right) return self.rLeft(node) return node def nodeHeight(self, node): if node == None: return 0 else: return node.height def rLeft(self, rnode): RN = rnode.right LN = RN.left RN.left = rnode rnode.right = LN rnode.height = 1 + max( self.nodeHeight(rnode.left), self.nodeHeight(rnode.right) ) RN.height = 1 + max(self.nodeHeight(RN.left), self.nodeHeight(RN.right)) return RN def rRight(self, rnode): LN = rnode.left RN = LN.right LN.right = rnode rnode.left = RN rnode.height = 1 + max( self.nodeHeight(rnode.left), self.nodeHeight(rnode.right) ) LN.height = 1 + max(self.nodeHeight(LN.left), self.nodeHeight(LN.right)) return LN
CLASS_DEF FUNC_DEF IF VAR NONE RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given a AVL tree and N values to be inserted in the tree. Write a function to insert a given value into the tree. Example 1: N = 3 Values to be inserted = {5,1,4} Input: Value to be inserted = 5 Output: 5 Input : Value to be inserted = 1 Output: 5 / 1 Input : Value to be inserted = 4 Output: 5 4 / LR rotation / \ 1 -----------> 1 5 \ 4 Your Task: You dont need to read input or print anything. Complete the function insertToAVL() which takes the root of the tree and the value of the node to be inserted as input parameters and returns the root of the modified tree. Note: The tree will be checked after each insertion. If it violates the properties of balanced BST, an error message will be printed followed by the inorder traversal of the tree at that moment. If instead all insertions are successful, inorder traversal of tree will be printed. Expected Time Complexity: O(log N) Expected Auxiliary Space: O(height of tree) Constraints: 1 ≀ N ≀ 500
class Solution: def getHeight(self, root): if not root: return 0 return root.height def leftrotate(self, root): node = root.right temp = node.left node.left = root root.right = temp root.height = 1 + max(self.getHeight(root.left), self.getHeight(temp)) node.height = 1 + max(self.getHeight(root), self.getHeight(node.right)) return node def rightrotate(self, root): node = root.left temp = node.right node.right = root root.left = temp root.height = 1 + max(self.getHeight(root.right), self.getHeight(temp)) node.height = 1 + max(self.getHeight(root), self.getHeight(node.left)) return node def insertToAVL(self, root, key): if not root: return Node(key) if key < root.data: root.left = self.insertToAVL(root.left, key) elif key > root.data: root.right = self.insertToAVL(root.right, key) root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right)) balance = self.getHeight(root.left) - self.getHeight(root.right) if balance > 1 and key < root.left.data: return self.rightrotate(root) if balance > 1 and key > root.left.data: root.left = self.leftrotate(root.left) return self.rightrotate(root) if balance < -1 and key > root.right.data: return self.leftrotate(root) if balance < -1 and key < root.right.data: root.right = self.rightrotate(root.right) return self.leftrotate(root) return root
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR
Given a AVL tree and N values to be inserted in the tree. Write a function to insert a given value into the tree. Example 1: N = 3 Values to be inserted = {5,1,4} Input: Value to be inserted = 5 Output: 5 Input : Value to be inserted = 1 Output: 5 / 1 Input : Value to be inserted = 4 Output: 5 4 / LR rotation / \ 1 -----------> 1 5 \ 4 Your Task: You dont need to read input or print anything. Complete the function insertToAVL() which takes the root of the tree and the value of the node to be inserted as input parameters and returns the root of the modified tree. Note: The tree will be checked after each insertion. If it violates the properties of balanced BST, an error message will be printed followed by the inorder traversal of the tree at that moment. If instead all insertions are successful, inorder traversal of tree will be printed. Expected Time Complexity: O(log N) Expected Auxiliary Space: O(height of tree) Constraints: 1 ≀ N ≀ 500
class Solution: def insertToAVL(self, root, key): if root == None: return Node(key) if key < root.data: root.left = self.insertToAVL(root.left, key) elif key > root.data: root.right = self.insertToAVL(root.right, key) root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right)) bal = self.getBalance(root) if bal > 1 and self.getBalance(root.left) >= 0: return self.rotateRight(root) elif bal > 1 and self.getBalance(root.left) < 0: root.left = self.rotateLeft(root.left) return self.rotateRight(root) elif bal < -1 and self.getBalance(root.right) <= 0: return self.rotateLeft(root) elif bal < -1 and self.getBalance(root.right) > 0: root.right = self.rotateRight(root.right) return self.rotateLeft(root) return root def getBalance(self, root): if root == None: return 0 return self.getHeight(root.left) - self.getHeight(root.right) def getHeight(self, root): if root == None: return 0 return root.height def rotateRight(self, root): x, y, z = root, root.left, root.left.left x.left = y.right y.right = x x.height = 1 + max(self.getHeight(x.left), self.getHeight(x.right)) y.height = 1 + max(self.getHeight(y.left), self.getHeight(y.right)) return y def rotateLeft(self, root): x, y, z = root, root.right, root.right.right x.right = y.left y.left = x x.height = 1 + max(self.getHeight(x.left), self.getHeight(x.right)) y.height = 1 + max(self.getHeight(y.left), self.getHeight(y.right)) return y
CLASS_DEF FUNC_DEF IF VAR NONE RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given a AVL tree and N values to be inserted in the tree. Write a function to insert a given value into the tree. Example 1: N = 3 Values to be inserted = {5,1,4} Input: Value to be inserted = 5 Output: 5 Input : Value to be inserted = 1 Output: 5 / 1 Input : Value to be inserted = 4 Output: 5 4 / LR rotation / \ 1 -----------> 1 5 \ 4 Your Task: You dont need to read input or print anything. Complete the function insertToAVL() which takes the root of the tree and the value of the node to be inserted as input parameters and returns the root of the modified tree. Note: The tree will be checked after each insertion. If it violates the properties of balanced BST, an error message will be printed followed by the inorder traversal of the tree at that moment. If instead all insertions are successful, inorder traversal of tree will be printed. Expected Time Complexity: O(log N) Expected Auxiliary Space: O(height of tree) Constraints: 1 ≀ N ≀ 500
class Solution: def height(self, h): if h == None: return 0 return h.height def getBalance(self, h): if not h: return 0 return self.height(h.left) - self.height(h.right) def rotateLeft(self, h): x = h.right h.right = x.left x.left = h h.height = max(self.height(h.left), self.height(h.right)) + 1 x.height = max(self.height(x.left), self.height(x.right)) + 1 return x def rotateRight(self, h): x = h.left h.left = x.right x.right = h h.height = max(self.height(h.left), self.height(h.right)) + 1 x.height = max(self.height(x.left), self.height(x.right)) + 1 return x def insertToAVL(self, root, key): if not root: return Node(key) if root.data < key: root.right = self.insertToAVL(root.right, key) elif root.data > key: root.left = self.insertToAVL(root.left, key) else: return root root.height = max(self.height(root.left), self.height(root.right)) + 1 balance = self.getBalance(root) if balance > 1: if key > root.left.data: root.left = self.rotateLeft(root.left) return self.rotateRight(root) elif balance < -1: if key < root.right.data: root.right = self.rotateRight(root.right) return self.rotateLeft(root) return root
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER RETURN VAR FUNC_DEF IF VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): res = [([0] * M) for _ in range(N)] for r in range(N): for c in range(M): prevcells = after[r - 1][c] if r > 0 else 0 tmp = after[r - 1][c - 1] if r > 0 and c > 0 else 0 prevcols = after[r][c - 1] - tmp if c > 0 else 0 res[r][c] = after[r][c] - prevcells - prevcols return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): n, m = len(after), len(after[0]) for i in range(n - 1, -1, -1): for j in range(m - 1, -1, -1): if i - 1 >= 0 and j - 1 >= 0: after[i][j] = after[i][j] - ( after[i - 1][j] + after[i][j - 1] - after[i - 1][j - 1] ) elif i - 1 >= 0: after[i][j] = after[i][j] - after[i - 1][j] elif j - 1 >= 0: after[i][j] = after[i][j] - after[i][j - 1] return after
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): sum = after[0][0] after.append([(0) for i in range(M)]) befor = [[(0) for i in range(M)] for j in range(N + 1)] befor[0][0] = after[0][0] for i in range(N + 1): for j in range(M): if i == 0 and j == 0: continue elif j == 0: befor[i][j] = after[i][j] - after[i - 1][j] else: befor[i][j] = ( after[i][j] - after[i - 1][j] - (after[i][j - 1] - after[i - 1][j - 1]) ) return befor[:-1][:]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR NUMBER
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def ans(self, x, y, after, before): res = after[x][y] for i in range(x + 1): for j in range(y + 1): if i == x and j == y: pass else: res -= before[i][j] return res def computeBeforeMatrix(self, N, M, after): before = [[(0) for i in range(M)] for j in range(N)] before[0][0] = after[0][0] for i in range(1, N): before[i][0] = after[i][0] - after[i - 1][0] for j in range(1, M): before[0][j] = after[0][j] - after[0][j - 1] for i in range(1, N): for j in range(1, M): before[i][j] = ( after[i][j] + after[i - 1][j - 1] - after[i - 1][j] - after[i][j - 1] ) return before
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): res = [[(0) for i in range(M)] for i in range(N)] res[0][0] = after[0][0] for i in range(1, N): res[i][0] = after[i][0] - after[i - 1][0] for i in range(1, M): res[0][i] = after[0][i] - after[0][i - 1] for i in range(1, N): for j in range(1, M): res[i][j] = ( after[i][j] + after[i - 1][j - 1] - after[i - 1][j] - after[i][j - 1] ) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): i = 0 j = 0 before = [] for _ in range(N): before.append([0] * M) while i < N: j = 0 while j < M: if i == 0 and j == 0: before[i][j] = after[i][j] elif i == 0 and j != 0: before[i][j] = after[i][j] - after[i][j - 1] elif i != 0 and j == 0: before[i][j] = after[i][j] - after[i - 1][j] else: before[i][j] = ( after[i][j] + after[i - 1][j - 1] - after[i - 1][j] - after[i][j - 1] ) j += 1 i += 1 return before
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): for i in range(N - 1, -1, -1): for j in range(M - 1, -1, -1): if i: after[i][j] -= after[i - 1][j] if j: after[i][j] -= after[i][j - 1] if i and j: after[i][j] += after[i - 1][j - 1] return after
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, n, m, after): for i in range(n - 1, -1, -1): for j in range(m - 1, -1, -1): if i == 0 and j == 0: continue elif i == 0 and j > 0: after[i][j] -= after[i][j - 1] elif j == 0 and i > 0: after[i][j] -= after[i - 1][j] else: after[i][j] -= ( after[i - 1][j] + after[i][j - 1] - after[i - 1][j - 1] ) return after
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): res = [] for i in range(N): col = [] for j in range(M): if i == 0: if j == 0: col.append(after[i][j]) else: col.append(after[i][j] - after[i][j - 1]) elif j == 0: col.append(after[i][j] - after[i - 1][j]) else: col.append( after[i][j] - (after[i][j - 1] + after[i - 1][j]) + after[i - 1][j - 1] ) res.append(col) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, n, m, after): aux = [[(0) for _ in range(m)] for _ in range(n)] before = [[(0) for _ in range(m)] for _ in range(n)] for i in range(n): for j in range(m): aux[i][j] += aux[i][j - 1] if j > 0 else 0 before[i][j] = after[i][j] - aux[i][j] - (aux[i - 1][j] if i > 0 else 0) aux[i][j] += before[i][j] if i > 0: aux[i] = [(aux[i][j] + aux[i - 1][j]) for j in range(m)] return before
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): res = [] for i in range(N): a = [0] * M res.append(a) res[0][0] = after[0][0] for i in range(N): for j in range(M): if i == 0 and j != 0: res[i][j] = after[i][j] - after[i][j - 1] if i != 0 and j == 0: res[i][j] = after[i][j] - after[i - 1][j] if i > 0 and j > 0: res[i][j] = ( after[i][j] - after[i - 1][j] - after[i][j - 1] + after[i - 1][j - 1] ) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): prefix = [([0] * M) for i in range(N)] before = [([0] * M) for i in range(N)] prefix[0][0] = after[0][0] before[0][0] = after[0][0] for i in range(1, N): before[i][0] = after[i][0] - prefix[i - 1][0] prefix[i][0] = before[i][0] + prefix[i - 1][0] for j in range(1, M): before[0][j] = after[0][j] - prefix[0][j - 1] prefix[0][j] = before[0][j] + prefix[0][j - 1] for i in range(1, N): for j in range(1, M): before[i][j] = after[i][j] - ( prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1] ) prefix[i][j] = before[i][j] + ( prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1] ) return before
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): arr = [] for i in range(N): arr.append([0] * M) for i in range(0, N): for j in range(0, M): if i == 0 and j == 0: arr[0][0] = after[0][0] elif j == 0: arr[i][j] = after[i][j] - after[i - 1][j] elif i == 0: arr[0][j] = after[0][j] - after[0][j - 1] else: arr[i][j] = ( after[i][j] - after[i - 1][j] - after[i][j - 1] + after[i - 1][j - 1] ) return arr
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, a): l = [[i for i in range(M)] for j in range(N)] for i in range(N): for j in range(M): if i == 0 and j == 0: l[i][j] = a[i][j] elif i == 0 and j != 0: l[i][j] = a[i][j] - a[i][j - 1] elif i != 0 and j == 0: l[i][j] = a[i][j] - a[i - 1][j] else: l[i][j] = a[i][j] - a[i - 1][j] - a[i][j - 1] + a[i - 1][j - 1] return l
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): for i in range(N): after[i].insert(0, 0) after.insert(0, [0] * (M + 1)) for i in range(N, 0, -1): for j in range(M, 0, -1): after[i][j] -= after[i - 1][j] + after[i][j - 1] - after[i - 1][j - 1] return [[after[i][j] for j in range(1, M + 1)] for i in range(1, N + 1)] if __name__ == "__main__": T = int(input()) while T > 0: N, M = [int(i) for i in input().split()] after = [] for j in range(N): after.append([int(i) for i in input().split()]) ob = Solution() before = ob.computeBeforeMatrix(N, M, after) for i in range(len(before)): for j in range(len(before[i])): print(before[i][j], end=" ") print() T -= 1
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): before = [[(0) for _ in range(M)] for _ in range(N)] for i in range(N): l = 0 for j in range(M): if i > 0: u = after[i - 1][j] else: u = 0 before[i][j] = after[i][j] - l - u l += before[i][j] return before
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): ans = [[(0) for _ in range(M)] for l in range(N)] res = 0 i = N - 1 j = M - 1 while i >= 0: j = M - 1 while j >= 0: res = 0 if i - 1 >= 0: res += after[i - 1][j] if j - 1 >= 0: res += after[i][j - 1] if i - 1 >= 0 and j - 1 >= 0: res -= after[i - 1][j - 1] ans[i][j] = after[i][j] - res j -= 1 i -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): pre = 0 upR = [0] * M for i in range(N): for j in range(M): if i == 0 and j == 0: pre = after[i][j] continue up = upR[j] if j == 0: pre = 0 x = after[i][j] after[i][j] = after[i][j] - pre - up pre = x if j == M - 1: pre = 0 upR = [(upR[k] + after[i][k]) for k in range(M)] return after
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): for j in range(M - 1, -1, -1): for i in range(N - 1, -1, -1): if i - 1 < 0: v1 = 0 else: v1 = after[i - 1][j] if j - 1 < 0: v2 = 0 else: v2 = after[i][j - 1] if i - 1 < 0 or j - 1 < 0: v3 = 0 else: v3 = after[i - 1][j - 1] after[i][j] = after[i][j] - v1 - v2 + v3 return after
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): for r in range(N - 1, 0, -1): for c in range(M): after[r][c] -= after[r - 1][c] for c in range(M - 1, 0, -1): for r in range(N): after[r][c] -= after[r][c - 1] return after
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): ans = [([0] * M) for _ in range(N)] for row in range(N): curr_row = 0 for col in range(M): if row == 0: if col > 0: ans[row][col] = after[row][col] - after[row][col - 1] else: ans[row][col] = after[row][col] else: if col == 0: ans[row][col] = after[row][col] - after[row - 1][col] else: ans[row][col] = after[row][col] - ( curr_row + after[row - 1][col] ) curr_row += ans[row][col] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): before = [[(0) for _ in range(M)] for _ in range(N)] for i in range(N): for j in range(M): if j == 0: prevCol = 0 else: prevCol = after[i][j - 1] if i == 0: prevRow = 0 else: prevRow = after[i - 1][j] if i == 0 or j == 0: before[i][j] = after[i][j] - prevCol - prevRow else: before[i][j] = after[i][j] - prevCol - prevRow + after[i - 1][j - 1] return before
CLASS_DEF FUNC_DEF 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): before = [[(0) for i in range(M)] for j in range(N)] res = after[0][0] pro = 0 before[0][0] = res h = {} for i in range(N): for j in range(M): if i == 0 and j == 0: continue elif i == 0 and j > 0: before[i][j] = after[i][j] - after[i][j - 1] elif j == 0 and i > 0: before[i][j] = after[i][j] - after[i - 1][j] else: if j not in h.keys(): h[j] = before[i - 1][j] else: h[j] += before[i - 1][j] before[i][j] = after[i][j] - after[i][j - 1] before[i][j] -= h[j] return before
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): before = [[(0) for i in range(M)] for j in range(N)] for i in range(N): for j in range(M): a = after[i][j] b1 = after[i - 1][j] if i - 1 >= 0 else 0 b2 = after[i][j - 1] if j - 1 >= 0 else 0 b3 = after[i - 1][j - 1] if i - 1 >= 0 and j - 1 >= 0 else 0 before[i][j] = a - b1 - b2 + b3 return before
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): arr = [] for i in range(N): arr.append([(0) for j in range(M)]) arr[0][0] = after[0][0] for x in range(1, M): arr[0][x] = after[0][x] - after[0][x - 1] for y in range(1, N): arr[y][0] = after[y][0] - after[y - 1][0] for j in range(1, N): for k in range(1, M): arr[j][k] = ( after[j][k] - after[j - 1][k] - after[j][k - 1] + after[j - 1][k - 1] ) return arr
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, a): b = [[(0) for i in range(M)] for j in range(N)] for i in range(N): um = 0 for j in range(M): if i == 0: b[i][j] = a[i][j] - um um = um + b[i][j] if i > 0: b[i][j] = a[i][j] - a[i - 1][j] - um um = um + b[i][j] return b
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, matrix): n = len(matrix) m = len(matrix[0]) before = [[(0) for i in range(m)] for j in range(n)] before[0][0] = matrix[0][0] for i in range(n): for j in range(m): if i == 0 and j > 0: before[i][j] = matrix[i][j] - matrix[i][j - 1] elif j == 0 and i > 0: before[i][j] = matrix[i][j] - matrix[i - 1][j] elif j > 0 and i > 0: before[i][j] = matrix[i][j] - ( matrix[i][j - 1] + matrix[i - 1][j] - matrix[i - 1][j - 1] ) return before if __name__ == "__main__": T = int(input()) while T > 0: N, M = [int(i) for i in input().split()] after = [] for j in range(N): after.append([int(i) for i in input().split()]) ob = Solution() before = ob.computeBeforeMatrix(N, M, after) for i in range(len(before)): for j in range(len(before[i])): print(before[i][j], end=" ") print() T -= 1
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 NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): a = [[(0) for i in range(M)] for i in range(N)] sum = 0 for i in range(M): a[0][i] = after[0][i] - sum sum += a[0][i] for i in range(1, N): sum = 0 for j in range(M): a[i][j] = after[i][j] - after[i - 1][j] - sum sum += a[i][j] return a
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): l = [] for i in range(len(after)): l1 = [] for j in range(len(after[0])): m, n, t = 0, 0, 0 if j - 1 >= 0: m = after[i][j - 1] if i - 1 >= 0: n = after[i - 1][j] if i - 1 >= 0 and j - 1 >= 0: t = after[i - 1][j - 1] l1.append(after[i][j] - m - n + t) l.append(l1) return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): new_lst = after new_lst.reverse() string = "" res = [] for i in range(N): temp = [] for j in range(M): try: temp.append(new_lst[i][j] - new_lst[i + 1][j]) except: temp.append(new_lst[i][j]) res.append(temp) temp.reverse() result = [] for item in res: res1 = [] for x in range(len(item)): try: res1.append(item[x] - item[x + 1]) except: res1.append(item[x]) res1.reverse() result.append(res1) result.reverse() return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
For a given 2D Matrix before, the corresponding cell (x, y) of the after matrix is calculated as follows: res = 0; for(i = 0; i <= x; i++){ for( j = 0; j <= y; j++){ res += before(i,j); } } after(x,y) = res; Given an N*M 2D-Matrix after, your task is to find the corresponding before matrix for the given matrix. Example 1: Input: N = 2, M = 3 after[][] = {{1, 3, 6}, {3, 7, 11}} Output: 1 2 3 2 2 1 Explanation: The before matrix for the given after matrix matrix is {{1, 2, 3}, {2, 2, 1}}. Reason: According to the code given in problem, after(0,0) = before(0,0) = 1 after(0,1) = before(0,0) + before(0,1) = 1 + 2 = 3. after(0, 2) = before(0,0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6. Similarly we can calculate values for every cell of the after matrix. Example 2: Input: N = 1, M = 3 after[][] = {{1, 3, 5}} Output: 1 2 2 Explanation: The before matrix for the given after matrix is {{1, 2, 2}}. Your Task: Complete the function computeBeforeMatrix() which takes the integers N, M, and the 2D Matrix after as the input parameters, and returns the before matrix of the given after matrix. Expected Time Complexity: O(N*M) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N, M, after[i][j] ≀ 10^{9}
class Solution: def computeBeforeMatrix(self, N, M, after): before = [[(0) for j in range(M)] for i in range(N)] for i in range(N): for j in range(M): if i == 0: if j > 0: before[i][j] = after[i][j] - after[i][j - 1] else: before[i][j] = after[i][j] elif i > 0: if j == 0: before[i][j] = after[i][j] - after[i - 1][j] else: before[i][j] = after[i][j] - ( after[i][j - 1] - after[i - 1][j - 1] + after[i - 1][j] ) return before
CLASS_DEF FUNC_DEF 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 NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def KthSmallestElement(self, root, k): s = [] c = 0 while s or root: while root: s.append(root) root = root.left node = s.pop() c += 1 if c == k: return node.data root = node.right return -1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR RETURN NUMBER
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def inorder(self, root, arr): if root is not None: self.inorder(root.left, arr) arr.append(root.data) self.inorder(root.right, arr) def KthSmallestElement(self, root, K): arr = [] self.inorder(root, arr) if len(arr) < K: return -1 return arr[K - 1]
CLASS_DEF FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def KthSmallestElement(self, root, K): count = [0] return self.helper(root, K, count) def helper(self, root, K, count): if root is None: return -1 result = -1 leftSubTree = self.helper(root.left, K, count) if leftSubTree > 0: result = leftSubTree count[0] += 1 if count[0] == K: result = root.data if result < 0: rightSubTree = self.helper(root.right, K, count) if rightSubTree > 0: result = rightSubTree return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def KthSmallestElement(self, root, K): temp = root while temp: if temp.left is None: K -= 1 if K == 0: return temp.data temp = temp.right else: pred = temp.left while pred.right and pred.right != temp: pred = pred.right if pred.right is None: pred.right = temp temp = temp.left else: pred.right = None K -= 1 if K == 0: return temp.data temp = temp.right return -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR WHILE VAR IF VAR NONE VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR RETURN NUMBER
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def util(self, root, k): if not root: return self.util(root.left, k) self.cnt += 1 if self.cnt == k: self.cnt += 1 self.ans = root.data return self.util(root.right, k) def KthSmallestElement(self, root, k): self.cnt = 0 self.ans = -1 self.util(root, k) return self.ans
CLASS_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def KthSmallestElement(self, root, K): li = self.inorder(root, []) if len(li) < K: return -1 else: return li[K - 1] def inorder(self, node, a): if node is None: return self.inorder(node.left, a) a.append(node.data) self.inorder(node.right, a) return a
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def KthSmallestElement(self, root, k): stack = [] while root is not None or len(stack) > 0: while root is not None: stack.append(root) root = root.left root = stack.pop() k -= 1 if k == 0: return root.data root = root.right return -1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR NONE FUNC_CALL VAR VAR NUMBER WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR RETURN NUMBER
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def KthSmallestElement(self, root, K): def inorder(root): nonlocal count, ans if not root: return inorder(root.left) count += 1 if count == K: ans = root.data inorder(root.right) count = 0 ans = -1 inorder(root) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def KthSmallestElement(self, root, K): i = [0, -1] def ino(root, K): if root: ino(root.left, K) i[0] = i[0] + 1 if i[0] == K: i[1] = root.data return return ino(root.right, K) ino(root, K) return i[1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR RETURN RETURN FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def inorder(self, root): if root == None: return [] return self.inorder(root.left) + [root.data] + self.inorder(root.right) def KthSmallestElement(self, root, K): li = self.inorder(root) if len(li) >= K: return li[K - 1] return -1
CLASS_DEF FUNC_DEF IF VAR NONE RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER RETURN NUMBER
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def KthSmallestElement(self, root, k): result = [] if root is None: return -1 if self.size(root) < k: return -1 self._kthSmallestElement(root, k, result) return result[k - 1] def _kthSmallestElement(self, root, k, result): if root is None: return if len(result) == k: return self._kthSmallestElement(root.left, k, result) result.append(root.data) self._kthSmallestElement(root.right, k, result) def size(self, root): if root is None: return 0 return 1 + self.size(root.left) + self.size(root.right)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR NONE RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: count = 0 ans = 0 def KthSmallestElement(self, root, K): if root == None: return self.KthSmallestElement(root.left, K) self.count += 1 if self.count == K: self.ans = root.data self.KthSmallestElement(root.right, K) if self.ans == 0: return -1 return self.ans
CLASS_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def inorder(self, root, l): if root is None: return self.inorder(root.left, l) l.append(root.data) self.inorder(root.right, l) def InOrder(self, root): l = [] self.inorder(root, l) return l def KthSmallestElement(self, root, K): p = self.InOrder(root) p.sort() if K > len(p): return -1 else: return p[K - 1]
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def __init__(self): self.counter = 0 def KthSmallestElement(self, root, k): return self.KthSmallestElementUtil(root, k) def KthSmallestElementUtil(self, root, k): if root is None: return -1 left = self.KthSmallestElementUtil(root.left, k) if left != -1: return left self.counter += 1 if self.counter == k: return root.data return self.KthSmallestElementUtil(root.right, k)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def inOrder(self, root): res = [] if root: res = self.inOrder(root.left) res.append(root.data) res = res + self.inOrder(root.right) return res def KthSmallestElement(self, root, K): li = self.inOrder(root) if len(li) >= K: return li[K - 1] return -1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER RETURN NUMBER
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
global index index = 0 global ans ans = -1 class Solution: def inorder(self, root, K): global index global ans if not root or ans != -1: return self.inorder(root.left, K) index += 1 if index == K: ans = root.data return self.inorder(root.right, K) def KthSmallestElement(self, root, K): global ans global index index = 0 ans = -1 self.inorder(root, K) return ans
ASSIGN VAR NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def util(self, root, k): global cnt, ans par = root while par != None: if par.left != None: temp = par.left while temp.right != None and temp.right != par: temp = temp.right if temp.right == None: temp.right = par par = par.left else: temp.right = None cnt += 1 if cnt == k: ans = par.data par = par.right else: cnt += 1 if cnt == k: ans = par.data par = par.right def KthSmallestElement(self, root, k): global cnt, ans cnt = 0 ans = -1 self.util(root, k) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE IF VAR NONE ASSIGN VAR VAR WHILE VAR NONE VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def inorder(self, root): if root: self.inorder(root.left) self.res.append(root.data) self.inorder(root.right) def KthSmallestElement(self, root, K): self.res = [] self.inorder(root) for i in range(len(self.res)): if K > len(self.res): return -1 return self.res[K - 1]
CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
global index index = 0 global ans ans = -1 global k class Solution: def inorder(self, root, K): global index global ans if not root or ans != -1: return self.inorder(root.left, K) index += 1 if index == K: ans = root.data return self.inorder(root.right, K) def KthSmallestElement(self, root, K): global k k = K node = self.doomdoom(root, K) if node: return node.data return -1 def doomdoom(self, root, K): global k if root == None: return None left = self.doomdoom(root.left, K) if left != None: return left k -= 1 if k == 0: return root return self.doomdoom(root.right, K)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN VAR RETURN NUMBER FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR VAR NUMBER IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. Example 1: Input: 2 / \ 1 3 K = 2 Output: 2 Explanation: 2 is the 2nd smallest element in the BST Example 2: Input: 2 / \ 1 3 K = 5 Output: -1 Explanation: There is no 5th smallest element in the BST as the size of BST is 3 Your Task: You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and returns the Kth smallest element in the BST, if no such element exists return -1. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1<=Number of nodes<=10^5
class Solution: def kthelement(self, root): if root is None: return -1 leftside = self.kthelement(root.left) if leftside != -1: return leftside self.K -= 1 if self.K == 0: return root.data return self.kthelement(root.right) def KthSmallestElement(self, root, K): self.K = K return self.kthelement(root)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR VAR NUMBER IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR
In a country Stone Land, there are $N$ stones arranged in a row from left to right. Each stone has some height represented by a positive number. The stones are numbered from $1$ to $N$ from left to right. You are given an array H of size $N$ consisting of the height of the $N$ stones from left to right. Stone at $j^{th}$ position is visible from the stone at $i^{th}$ position if:- 1. $j>i$ 2. $max(H[i],H[i+1],H[i+2],....,H[j-1])$ $<$ $H[j]$ You are asked $M$ queries. Each query consists of two different positions of stones $A$ and $B$. You have to Print the total number of stones that are visible from both the stones at the position $A$ and $B$. ------ 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 two space-separated integers $N$ and $M$, where $N$ is the number of stones and $M$ is the number of queries. The Second line consists of $N$ space-separated integers (array H) denoting the height of stones from left to the right separated by a space. Next, $M$ lines consist of two space-separated integers $A$ and $B$ separated by space where $A$ and $B$ are positions of stones. ------ Constraints ------ $1 ≀ T ≀ 5$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $1 ≀ H[i] ≀ 10^{9}$, where H[i] denotes the height of $i^{th}$ stone, and $1 ≀ i ≀ N$ $1 ≀ A, B ≀ N$ It is guaranteed that the sum of $N$ over all test cases does not exceed $10^{5}$. It is guaranteed that the sum of $M$ over all test cases does not exceed $10^{5}$. ------ Output ------ For each query, print the total number of stones that are visible from both the stones at the position $A$ and $B$ in a new line. ------ Example Input ------ 1 6 6 3 10 20 20 5 50 1 2 2 4 4 3 4 5 1 3 4 4 ------ Example Output ------ 2 1 1 1 1 1 ------ Explanation ------ Query $1$: Stone at position $1$ can see the stones at position $(2, 3, 6)$ whereas stone at position $2$ can see the stones at position $(3, 6)$. Thus, the total number of stones that are visible from both stones is $2$.
segment_tree = [] height = [] def build(ind, start, end): if start == end: segment_tree[ind] = height[start], start return segment_tree[ind] mid = (start + end) // 2 left = build(2 * ind + 1, start, mid) right = build(2 * ind + 2, mid + 1, end) if left[0] > right[0]: segment_tree[ind] = left else: segment_tree[ind] = right return segment_tree[ind] def query(ind, start, end, l, r): if start > r or end < l: return -float("inf"), -1 if l <= start and r >= end: return segment_tree[ind] mid = (start + end) // 2 left = query(2 * ind + 1, start, mid, l, r) right = query(2 * ind + 2, mid + 1, end, l, r) if left[0] > right[0]: return left else: return right t = int(input()) for _ in range(t): n_stones, m_queries = map(int, input().split()) segment_tree = [0] * (n_stones * 4) height = list(map(int, input().split())) build(0, 0, n_stones - 1) stack = [] for i in range(n_stones - 1, -1, -1): curr = height[i] while stack and stack[-1] <= curr: stack.pop(-1) height[i] = len(stack) stack.append(curr) for _ in range(m_queries): l, r = map(int, input().split()) if l > r: l, r = r, l l -= 1 r -= 1 _, ind = query(0, 0, n_stones - 1, l, r) print(height[ind])
ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR