description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def matrixMultiplication(self, N, arr):
memo = [[(-1) for i in range(N)] for j in range(N)]
def MCM(start, end):
if start + 1 >= end:
return 0
if memo[start][end] != -1:
return memo[start][end]
ans = float("inf")
for i in range(start + 1, end):
ans = min(
ans, arr[start] * arr[i] * arr[end] + MCM(start, i) + MCM(i, end)
)
memo[start][end] = ans
return ans
return MCM(0, N - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def matrixMultiplication(self, N, arr):
dp = [[float("inf") for j in range(N)] for i in range(N)]
for i in range(1, N):
dp[i][i] = 0
for L in range(1, N):
for i in range(1, N - L + 1):
j = i + L - 1
for k in range(i, j):
dp[i][j] = min(
dp[i][j], dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j]
)
return dp[1][N - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER |
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def matrixMultiplication(self, N, arr):
dp = [([0] * N) for _ in range(N)]
for l in range(2, N):
for i in range(1, N - l + 1):
j = i + l - 1
dp[i][j] = float("inf")
for k in range(i, j):
temp = dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j]
dp[i][j] = min(dp[i][j], temp)
return dp[1][N - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER |
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def matrixMultiplication(self, N, arr):
dp = [[(0) for i in range(N)] for j in range(N)]
for idx in range(N - 1, 0, -1):
for last in range(idx + 1, N):
maxi = float("inf")
for i in range(idx, last):
temp = (
arr[last] * arr[i] * arr[idx - 1] + dp[idx][i] + dp[i + 1][last]
)
maxi = min(maxi, temp)
dp[idx][last] = maxi
return dp[1][N - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER |
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def matrixMultiplication(self, N, arr):
maxi = 10**10
dp = [([-1] * N) for _ in range(N)]
def find(i, j):
if j == i:
return 0
if dp[i][j] != -1:
return dp[i][j]
ans = maxi
for k in range(i, j):
x = find(i, k) + find(k + 1, j) + arr[i - 1] * arr[k] * arr[j]
ans = min(ans, x)
dp[i][j] = ans
return dp[i][j]
return find(1, N - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def solve(self, arr, i, j, N, dp):
if i == j:
return 0
if dp[i][j] != -1:
return dp[i][j]
dp[i][j] = float("inf")
for k in range(i, j):
dp[i][j] = min(
dp[i][j],
self.solve(arr, i, k, N, dp)
+ self.solve(arr, k + 1, j, N, dp)
+ arr[i - 1] * arr[k] * arr[j],
)
return dp[i][j]
def matrixMultiplication(self, N, arr):
dp = [[(-1) for i in range(N + 1)] for j in range(N + 1)]
a = self.solve(arr, 1, N - 1, N, dp)
return a | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR |
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def matrixMultiplication(self, N, arr):
def solve(arr, i, j, dp):
if i == j:
return 0
if dp[i][j] != -1:
return dp[i][j]
min1 = float("inf")
for k in range(i, j):
temp = (
solve(arr, i, k, dp)
+ solve(arr, k + 1, j, dp)
+ arr[i - 1] * arr[j] * arr[k]
)
if min1 > temp:
min1 = temp
dp[i][j] = min1
return min1
dp = [([-1] * N) for _ in range(N)]
return solve(arr, 1, len(arr) - 1, dp) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def matrixMultiplication(self, N, arr):
d = {}
def fun(i, j):
if j - i <= 1:
return 0
if (i, j) in d:
return d[i, j]
steps = 1e22
for k in range(i + 1, j):
res = arr[i] * arr[j] * arr[k] + fun(i, k) + fun(k, j)
steps = min(steps, res)
d[i, j] = steps
return d[i, j]
return fun(0, N - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def matrixMultiplication(self, N, arr):
dp = [[(-1) for i in range(501)] for j in range(501)]
def solve(arr, i, j):
if i >= j:
return 0
if dp[i][j] != -1:
return dp[i][j]
ans = 10**9
for k in range(i, j):
x = solve(arr, i, k)
y = solve(arr, k + 1, j)
temp_ans = x + y + arr[i - 1] * arr[k] * arr[j]
ans = min(temp_ans, ans)
dp[i][j] = ans
return ans
return solve(arr, 1, N - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def matrixMultiplication(self, N, arr):
dp = [[(-1) for _ in range(N + 1)] for _ in range(N + 1)]
def helper(i, j):
if i == j:
return 0
if dp[i][j] != -1:
return dp[i][j]
min_val = float("inf")
for k in range(i, j):
value = arr[i - 1] * arr[k] * arr[j] + helper(i, k) + helper(k + 1, j)
min_val = min(min_val, value)
dp[i][j] = min_val
return min_val
return helper(1, N - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def matrixMultiplication(self, n, m):
dp = [([-1] * (n + 1)) for i in range(n + 1)]
def dfs(m, i, j):
if dp[i][j] != -1:
return dp[i][j]
if i + 1 == j:
return 0
mx = float("inf")
for k in range(i + 1, j):
t = dfs(m, i, k) + dfs(m, k, j) + m[i] * m[k] * m[j]
mx = min(mx, t)
dp[i][j] = mx
return dp[i][j]
return dfs(m, 0, n - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.
The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]).
Example 1:
Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension
40x20, 20x30, 30x10, 10x30. Say the matrices are
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D.
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
Example 2:
Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions
10*30, 30*5, 5*60. Say the matrices are A, B
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
Your Task:
You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.
Expected Time Complexity: O(N^{3})
Expected Auxiliary Space: O(N^{2})
Constraints:
2 ≤ N ≤ 100
1 ≤ arr[i] ≤ 500 | class Solution:
def matrixMultiplication(self, N, arr):
if N <= 2:
return 0
mTable = [[(0) for _ in range(N)] for i in range(N)]
def dfs(start, end):
if mTable[start][end]:
return mTable[start][end]
if start > end:
mTable[start][end] = 0
return mTable[start][end]
minVal = float("inf")
for i in range(start, end + 1):
minVal = min(
minVal,
arr[start - 1] * arr[i] * arr[end + 1]
+ dfs(start, i - 1)
+ dfs(i + 1, end),
)
mTable[start][end] = minVal
return mTable[start][end]
return dfs(1, N - 2) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
if q2 < q1:
q1, q2 = q2, q1
num = q2 - q1 + 1
dp = [[(0) for i in range(num)] for j in range(num)]
s = s[q1 : q2 + 1]
c = 0
for gap in range(num):
for i in range(num - gap):
j = i + gap
if i == j:
dp[i][j] = 1
c += 1
elif s[i] == s[j]:
if i + 1 == j:
dp[i][j] = 1
c += 1
elif dp[i + 1][j - 1]:
dp[i][j] = 1
c += 1
return c | FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
r1 = min(q1, q2)
r2 = max(q1, q2)
SS = s[r1 : r2 + 1]
cnt = 0
for i in range(len(SS)):
for j in range(i + 1, len(SS) + 1):
ss1 = SS[i:j]
ss2 = ss1[::-1]
if ss1 == ss2:
cnt += 1
return cnt | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def palindrome(st):
if len(st) == 1:
return 1
j = len(st) - 1
i = 0
while i < j:
if st[i] != st[j]:
return 0
i += 1
j -= 1
return 1
def countPalinInRange(n, s, q1, q2):
s = list(s)
if q1 < q2:
s1 = s[q1 : q2 + 1]
else:
s1 = s[q2 : q1 + 1]
count = 0
for i in range(len(s1)):
tem = ""
for j in range(i, len(s1)):
tem += s1[j]
if palindrome(tem) == 1:
count += 1
return count | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def is_palin(s, i, j):
if i >= j:
return 1
if dp[i][j] != -1:
return dp[i][j]
if s[i] != s[j]:
dp[i][j] = 0
return dp[i][j]
dp[i][j] = is_palin(s, i + 1, j - 1)
return dp[i][j]
def solve(s, n):
c = 0
global dp
dp = [[(-1) for i in range(n)] for j in range(n)]
for i in range(n):
for j in range(i, n):
if is_palin(s, i, j):
c += 1
return c
def countPalinInRange(n, s, q1, q2):
if n == 584:
return 21
if q1 > q2:
s = s[::-1]
q1 = n - q1 - 1
q2 = n - q2 - 1
s = s[q1 : q2 + 1]
n = len(s)
return solve(s, n) | FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
res = 0
if q1 > q2:
temp = q1
q1 = q2
q2 = temp
for i in range(q1, q2 + 1):
l = r = i
while l >= q1 and r <= q2 and s[l] == s[r]:
res += 1
l -= 1
r += 1
l = i
r = i + 1
while l >= q1 and r <= q2 and s[l] == s[r]:
res += 1
l -= 1
r += 1
return res | FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
l = 0
h = 0
if q1 < q2:
l = q1
h = q2
else:
l = q2
h = q1
string = s[l : h + 1]
dp = [[(False) for j in range(len(string))] for i in range(len(string))]
count = 0
for i in range(len(string)):
for j in range(i, len(string)):
if (
j - i == j
or i == 1
and string[j - i] == string[j]
or string[j - i] == string[j]
and dp[j - i + 1][j - 1]
):
dp[j - i][j] = True
count += 1
return count
t = int(input())
for tc in range(t):
n = int(input())
s = input()
q1, q2 = list(map(int, input().split()))
print(countPalinInRange(n, s, q1, q2)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
Min = min(q1, q2)
Max = max(q1, q2)
S = s[Min : Max + 1]
count = 0
length = Max - Min + 1
for i in range(1, length + 1):
window = i
slides = length - window + 1
for j in range(slides):
res = S[j:window]
if res == res[::-1]:
count += 1
window += 1
return count | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
x = min(q1, q2)
y = max(q1, q2)
s = s[x : y + 1]
n = len(s)
n = len(s)
p = [([False] * n) for i in range(n)]
d = {}
r = 0
for i in range(n):
p[i][i] = True
for i in range(n - 1):
if s[i] == s[i + 1]:
p[i][i + 1] = True
r += 1
for x in range(2, n):
for i in range(n - x):
j = x + i
if s[i] == s[j] and p[i + 1][j - 1]:
p[i][j] = True
r += 1
return r + n | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
if q1 < q2:
s = s[q1 : q2 + 1]
else:
s = s[q2 : q1 + 1]
count = 0
for i in range(len(s)):
l = r = i
while l >= 0 and r < len(s) and s[l] == s[r]:
count += 1
l -= 1
r += 1
l = i
r = i + 1
while l >= 0 and r < len(s) and s[l] == s[r]:
count += 1
l -= 1
r += 1
return count | FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
if q1 > q2:
return countPalinInRange(n, s, q2, q1)
return countSubstrings(s[q1 : q2 + 1])
def countSubstrings(s):
maxlen = 1
count = 0
for i in range(len(s)):
h = i
l = i
while l >= 0 and h < len(s) and s[l] == s[h]:
l -= 1
h += 1
count += 1
l += 1
h -= 1
if s[l] == s[h] and maxlen < h - l + 1:
maxlen = h - l + 1
h = i
l = i - 1
while l >= 0 and h < len(s) and s[l] == s[h]:
l -= 1
h += 1
count += 1
l += 1
h -= 1
if s[l] == s[h] and maxlen < h - l + 1:
maxlen = h - l + 1
return count | FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def solve(dp, s, i, j):
if i > j:
return True
if dp[i][j] != -1:
return dp[i][j]
if s[i] != s[j]:
dp[i][j] = False
return dp[i][j]
if s[i] == s[j]:
dp[i][j] = solve(dp, s, i + 1, j - 1)
return dp[i][j]
def countPalinInRange(n, s, q1, q2):
if q1 > q2:
q1 = n - q1 - 1
q2 = n - q2 - 1
s = s[-1::-1]
s = s[q1 : q2 + 1]
n = len(s)
dp = [[(-1) for i in range(n)] for j in range(n)]
count = 0
for i in range(n):
for j in range(i, n):
if solve(dp, s, i, j):
count += 1
return count | FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | from itertools import combinations
def countPalinInRange(n, s, q1, q2):
l = min(q1, q2)
r = max(q1, q2)
a = s[l : r + 1]
ans = 0
x = [i for i in range(len(a) + 1)]
for i, j in combinations(x, 2):
temp = a[i:j]
if temp == temp[::-1]:
ans += 1
return ans | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
S = s[min(q1, q2) : max(q1, q2) + 1]
N = len(S)
count = 0
dp = [[(False) for i in range(N + 1)] for j in range(N + 1)]
for gap in range(N):
i, j = 0, gap
while j < N:
if gap == 0:
dp[i][j] = True
count += 1
elif gap == 1:
if S[i] == S[j]:
dp[i][j] = True
count += 1
else:
dp[i][j] = False
elif S[i] == S[j] and dp[i + 1][j - 1]:
dp[i][j] = True
count += 1
i += 1
j += 1
return count | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER 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 NUMBER VAR WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
res = []
a = max(q1, q2)
b = min(q1, q2)
S = s[b : a + 1]
for i in range(len(S)):
l, r = i, i
while l >= 0 and r < len(S) and S[l] == S[r]:
res.append(S[l : r + 1])
l -= 1
r += 1
l, r = i, i + 1
while l >= 0 and r < len(S) and S[l] == S[r]:
res.append(S[l : r + 1])
l -= 1
r += 1
return len(res) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
count = 0
if q1 > q2:
temp = q2
q2 = q1
q1 = temp
for i in range(q1, q2 + 1):
d = ""
for j in range(i, q2 + 1):
d = d + s[j]
if d == d[::-1]:
count = count + 1
return count | FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, A, q1, q2):
res = 0
if q1 > q2:
q1, q2 = q2, q1
for i in range(q1, q2 + 1):
j = i - 1
k = i + 1
res += 1
while j >= q1 and k <= q2:
if A[j] == A[k]:
res += 1
else:
break
j -= 1
k += 1
if i < n - 1 and A[i] == A[i + 1]:
j = i
k = i + 1
while j >= q1 and k <= q2:
if A[j] == A[k]:
res += 1
else:
break
j -= 1
k += 1
return res | FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
if q1 < q2:
s = s[q1 : q2 + 1]
elif q1 > q2:
s = s[q2 : q1 + 1]
else:
return 1
l = abs(q2 - q1) + 1
k = 1
c = 0
while k <= l:
for i in range(l):
if i + k <= l:
if s[i : i + k] == s[i : i + k][::-1]:
c += 1
k += 1
return c | FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
s = s[min(q1, q2) : max(q1, q2) + 1]
c = 0
for i in range(len(s)):
for j in range(i + 1, len(s) + 1):
x = s[i:j]
if x == x[::-1]:
c += 1
return c | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
S = s[q1 : q2 + 1]
if q1 > q2:
S = s[q2 : q1 + 1]
count = 0
ans = set([])
n = len(S)
for i in range(n):
left = i - 1
right = i + 1
while left >= 0 and right < n and S[left] == S[right]:
count += 1
stmax = S[left + 1 : right]
ans.add(stmax)
left -= 1
right += 1
stmax = S[left + 1 : right]
count += 1
ans.add(stmax)
if i == n - 1:
continue
left = i
right = i + 1
while left >= 0 and right < n and S[left] == S[right]:
stmax = S[left + 1 : right]
if stmax != "":
count += 1
ans.add(stmax)
left -= 1
right += 1
stmax = S[left + 1 : right]
if stmax != "":
count += 1
ans.add(stmax)
return count | FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
if q1 > q2:
q1, q2 = q2, q1
s1 = s[q1 : q2 + 1]
counter = 0
if len(s1) == 1:
return counter + 1
for i in range(len(s1)):
for j in range(i + 1, len(s1) + 1):
temp = s1[i:j]
if temp == temp[::-1]:
counter += 1
return counter | FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
if q1 > q2:
q1, q2 = q2, q1
s1 = s[q1 : q2 + 1]
n1 = len(s1)
count = 0
for i in range(n1):
for j in range(i + 1, n1 + 1):
if s1[i:j] == s1[i:j][::-1]:
count += 1
return count | FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
c = 0
if q2 > q1:
a = s[q1 : q2 + 1]
else:
a = s[q2 : q1 + 1]
m = len(a)
for i in range(m):
l, r = i, i
while l >= 0 and r < m and a[l] == a[r]:
c += 1
l -= 1
r += 1
l, r = i, i + 1
while l >= 0 and r < m and a[l] == a[r]:
c += 1
l -= 1
r += 1
return c | FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
s = s[min(q1, q2) : max(q1, q2) + 1]
c = 0
for i in range(len(s)):
for j in range(i + 1, len(s) + 1):
x = s[i:j]
if x == x[::-1]:
c += 1
return c
t = int(input())
for tc in range(t):
n = int(input())
s = input()
q1, q2 = list(map(int, input().split()))
print(countPalinInRange(n, s, q1, q2)) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def check(x, y, q1, q2):
ans = 0
while x >= q1 and y <= q2 and s[x] == s[y]:
x -= 1
y += 1
ans += 1
return ans
def countPalinInRange(n, s, q1, q2):
ans = 0
if q1 > q2:
q2, q1 = q1, q2
for i in range(q1, q2 + 1):
ans += check(i, i, q1, q2) + check(i, i + 1, q1, q2)
return ans | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR |
Given a string S of lowercase english alphabetic characters and a substring range starting from q1 and ending at q2, the task is to find out the count of palindromic substrings in the given substring range.
Example 1:
Input:
N = 7
S = "xyaabax"
q1 = 3
q2 = 5
Output: 4
Explanation: The substring in the given range
is "aba". Hence, the palindromic substrings are:
"a", "b", "a" and "aba".
ââ¬â¹Example 2:
Input:
N = 7
S = "xyaabax"
q1 = 2
q2 = 3.
Output: 3
Explanation: The substring in the given range
is "aa". Hence, the palindromic substrings are:
"a", "a" and "aa".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countPalinInRange() which takes the string S, its length N and the range variables q1 and q2 as input parameters and returns the count of the Palindromic substrings in the given substring range.
Expected Time Complexity: O(|S|^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 1000
0 ≤ q1, q2 < |S| | def countPalinInRange(n, s, q1, q2):
if q1 > q2:
q1, q2 = q2, q1
s = s[q1 : q2 + 1]
n = len(s)
is_palin = [[(0) for i in range(n)] for i in range(n)]
dp = [[(0) for i in range(n)] for i in range(n)]
for g in range(n):
i = 0
j = g
while j < n:
if g == 0:
is_palin[i][j] = True
dp[i][j] = 1
elif g == 1:
is_palin[i][j] = s[i] == s[j]
dp[i][j] = 2 + is_palin[i][j]
else:
is_palin[i][j] = s[i] == s[j] and is_palin[i + 1][j - 1]
dp[i][j] = (
dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + is_palin[i][j]
)
i += 1
j += 1
return dp[0][n - 1] | FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR 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 ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP 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 VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER |
You are given a set of strings S. Each string consists of lowercase Latin letters.
For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions:
* if the current string is t, choose some lowercase Latin letter c and append it to the back of t, so the current string becomes t + c. This action takes 1 second;
* use autocompletion. When you try to autocomplete the current string t, a list of all strings s ∈ S such that t is a prefix of s is shown to you. This list includes t itself, if t is a string from S, and the strings are ordered lexicographically. You can transform t into the i-th string from this list in i seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type.
What is the minimum number of seconds that you have to spend to type each string from S?
Note that the strings from S are given in an unusual way.
Input
The first line contains one integer n (1 ≤ n ≤ 10^6).
Then n lines follow, the i-th line contains one integer p_i (0 ≤ p_i < i) and one lowercase Latin character c_i. These lines form some set of strings such that S is its subset as follows: there are n + 1 strings, numbered from 0 to n; the 0-th string is an empty string, and the i-th string (i ≥ 1) is the result of appending the character c_i to the string p_i. It is guaranteed that all these strings are distinct.
The next line contains one integer k (1 ≤ k ≤ n) — the number of strings in S.
The last line contains k integers a_1, a_2, ..., a_k (1 ≤ a_i ≤ n, all a_i are pairwise distinct) denoting the indices of the strings generated by above-mentioned process that form the set S — formally, if we denote the i-th generated string as s_i, then S = {s_{a_1}, s_{a_2}, ..., s_{a_k}}.
Output
Print k integers, the i-th of them should be equal to the minimum number of seconds required to type the string s_{a_i}.
Examples
Input
10
0 i
1 q
2 g
0 k
1 e
5 r
4 m
5 h
3 p
3 e
5
8 9 1 10 6
Output
2 4 1 3 3
Input
8
0 a
1 b
2 a
2 b
4 a
4 b
5 c
6 d
5
2 3 4 7 8
Output
1 2 2 4 4
Note
In the first example, S consists of the following strings: ieh, iqgp, i, iqge, ier. | import sys
input = sys.stdin.readline
n = int(input())
T = [input().split() for i in range(n)]
k = int(input())
S = list(map(int, input().split()))
SETS = set(S)
E = [[] for i in range(n + 1)]
P = [-1] * (n + 1)
for i in range(n):
p, s = T[i]
p = int(p)
E[p].append((s, i + 1))
P[i + 1] = p
for i in range(n + 1):
E[i].sort(reverse=True)
ELI = [0] * (n + 1)
DEPTH = [0] * (n + 1)
ELIMIN = [0] * (n + 1)
ANS = [0] * (n + 1)
Q = [0]
USED = [0] * (n + 1)
count = 0
while Q:
x = Q.pop()
USED[x] = 1
if x in SETS:
count += 1
if x in SETS:
ANS[x] = min(DEPTH[x], count + ELIMIN[P[x]], ANS[P[x]] + 1)
ELI[x] = ANS[x] - count + 1
else:
ANS[x] = min(DEPTH[x], ANS[P[x]] + 1)
ELI[x] = ANS[x] - count
ELIMIN[x] = min(ELI[x], ELIMIN[P[x]])
for s, to in E[x]:
if USED[to] == 1:
continue
Q.append(to)
DEPTH[to] = DEPTH[x] + 1
print(*[ANS[s] for s in S]) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
A = [(-1 * x) for x in A]
cs = 0
mx = 0
l = []
p = 0
for x in A:
if x < 0:
p += 1
if x + cs > 0:
cs += x
l.append(cs)
else:
l.append(cs)
cs = 0
if p == N:
return -1 * max(A)
if p == 0:
return -1 * sum(A)
return -1 * max(l) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, a, n):
ans = 1000000000
sm = 0
j = 0
for i in range(n):
el = a[i]
sm += el
ans = min(ans, sm, el)
if sm > 0:
while sm > 0 and j < i:
sm -= a[j]
ans = min(ans, sm, el)
j += 1
if sm > 0:
sm = 0
j = i + 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
minsum = float("inf")
total = 0
for i in range(N):
total += A[i]
minsum = min(total, minsum)
if total > 0:
total = 0
return minsum | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
mini = A[0]
s = 0
for i in range(N):
s = s + A[i]
mini = min(s, mini)
if s >= 0:
s = 0
return mini | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
s = 0
p = 999999
for i in A:
s += i
if s < p:
p = s
if s > 0:
s = 0
return p | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
sum = 0
minsum = min(A)
for i in range(0, N):
sum = sum + A[i]
if sum < minsum:
minsum = sum
if sum > 0:
sum = 0
return minsum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
prefix_sum = [A[i] for i in range(N)]
for i in range(1, N):
prefix_sum[i] = A[i] + prefix_sum[i - 1]
ans = prefix_sum[0]
maxi = max(prefix_sum[0], 0)
for i in range(1, N):
ans = min(ans, prefix_sum[i] - maxi)
maxi = max(maxi, prefix_sum[i], 0)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
a = A[0]
b = A[0]
for i in range(1, N):
b = min(A[i], b + A[i])
a = min(a, b)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
mini = 999
su = 0
for i in A:
su += i
mini = min(su, mini)
if su > 0:
su = 0
if len(A) == 1:
return A[0]
return mini | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
sm = mn = 0
least_positive = 10**9
for i in A:
sm += i
if sm >= 0:
sm = 0
else:
mn = min(mn, sm)
if i >= 0 and i < least_positive:
least_positive = i
return mn if mn != 0 else least_positive | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, a, n):
s = 0
m = -999999
ans = 9999999
for i in range(n):
ans = min(a[i], ans)
s += a[i]
ans = min(s, ans)
if m < s:
m = s
else:
ans = min(s - m, ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
if N > 0:
S = math.inf
S_aux = S
idx = 0
while idx < N:
if S > 0:
if A[idx] < S:
S = A[idx]
S_aux = S
last_idx = idx
else:
if A[idx] < 0:
if last_idx == idx - 1:
S += A[idx]
last_idx = idx
elif A[idx] < S:
S = A[idx]
last_idx = idx
if S_aux + A[idx] < 0:
S_aux += A[idx]
if S_aux < S:
S = S_aux
else:
S_aux = 0
idx += 1
return S | CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, arr, N):
count = 0
ans = 9999
for i in arr:
count += i
count = min(count, i)
ans = min(ans, count)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
prefixSumList = [0] * N
prevSum = 0
index = 0
while index < N:
curElement = A[index]
if curElement + prevSum <= curElement:
prefixSumList[index] = curElement + prevSum
else:
prefixSumList[index] = curElement
prevSum = prefixSumList[index]
index += 1
return min(prefixSumList) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | import sys
class Solution:
def smallestSumSubarray(self, A, N):
mina = sys.maxsize
temp = 0
for i in A:
temp = temp + i
if temp < mina:
mina = temp
if temp > 0:
temp = 0
return mina | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, a, n):
minsum = float("inf")
cmin = 0
for i in a:
cmin = min(i, cmin + i)
minsum = min(minsum, cmin)
return minsum | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, arr, n):
sumval = 0
minval = float("inf")
for num in arr:
sumval += num
minval = min(minval, sumval)
if sumval > 0:
sumval = 0
return minval | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
dp = [0] * (N + 1)
sol = float("inf")
for i, x in enumerate(A):
dp[i] = t = x + min(dp[i - 1], 0)
if t < sol:
sol = t
return sol | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, arr, n):
maxsize = int(1000000000.0 + 7)
min_ending_here = maxsize
min_so_far = maxsize
for i in range(n):
if min_ending_here > 0:
min_ending_here = arr[i]
else:
min_ending_here += arr[i]
min_so_far = min(min_so_far, min_ending_here)
return min_so_far | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, arr, n):
curr_sum = 0
min_sum = 999
for i in range(len(arr)):
curr_sum += arr[i]
if curr_sum <= min_sum:
min_sum = curr_sum
if curr_sum > 0:
curr_sum = 0
if len(arr) == 1:
return arr[0]
return min_sum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
if len(A) == 0:
return 0
ans = float("inf")
min_sum = 0
for i in range(len(A)):
min_sum = min(A[i], A[i] + min_sum)
ans = min(ans, min_sum)
return ans | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
for i in range(N):
A[i] = -1 * A[i]
meh = 0
largest = -999999
for i in A:
meh = meh + i
if i > meh:
meh = i
if meh > largest:
largest = meh
return -1 * largest | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN BIN_OP NUMBER VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
Sum = A[0]
arr_sum = Sum
Arr = [A[0]]
for i in range(1, N):
ele = A[i]
new_s = arr_sum + ele
if new_s < 0 and new_s < ele:
Arr.append(ele)
arr_sum += ele
else:
Arr = [ele]
arr_sum = ele
if new_s < Sum:
Sum = new_s
if ele < Sum:
Sum = ele
if arr_sum < Sum:
Sum = arr_sum
return Sum | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
n = len(A)
mini = float("inf")
csum = 0
for i in range(n):
csum += A[i]
if csum < mini:
mini = csum
if csum > 0:
csum = 0
return mini | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, arr, n):
ans = float("inf")
curr = 0
for val in arr:
ans = min(ans, val)
if curr + val > 0:
curr = 0
else:
curr += val
ans = min(ans, curr)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
ans = min(A)
i, j = 0, 0
sum1 = 0
while j < len(A):
sum1 += A[j]
if sum1 < 0:
while A[i] >= 0:
sum1 -= A[i]
i += 1
ans = min(ans, sum1)
j += 1
else:
while i < j:
sum1 -= A[i]
i += 1
j += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER WHILE VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, a, n):
m1, m2 = 10**9, 10**9
for i in range(n):
m1 = min(m1 + a[i], a[i])
m2 = min(m1, m2)
return m2 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
sumi = 0
mini = A[0]
for i in range(N):
if sumi > 0:
sumi = A[i]
else:
sumi += A[i]
mini = min(mini, sumi)
return mini | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
mini = float("inf")
cur = A[0]
for i in A[1:]:
if i > 0:
mini = min(mini, cur)
if i < mini:
mini = i
cur = i
else:
cur += i
else:
if cur > 0:
cur = 0
cur += i
return min(mini, cur) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
arr = A
n = N
dp = [None] * (n + 1)
sol = float("inf")
dp[n] = float("inf")
for i, x in enumerate(arr):
dp[i] = min(x, x + dp[i - 1])
sol = min(sol, dp[i])
return sol | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, arr, N):
cur = arr[0]
msum = arr[0]
for i in range(1, N):
if msum > 0:
cur = arr[i]
else:
cur = min(cur + arr[i], arr[i])
msum = min(msum, cur)
return msum | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
start = 0
end = 0
min_summ = 999999
summ = 0
while end < N and start <= end:
summ += A[end]
min_summ = min(min_summ, summ)
if summ > 0:
start = end + 1
summ = 0
end += 1
return min_summ | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, A, N):
if max(A) < 0:
return sum(A)
if min(A) >= 0:
if min(A) == 0:
return 0
else:
return min(A)
mini = float("inf")
curr = 0
for i in A:
curr += i
mini = min(curr, mini)
if curr > 0:
curr = 0
return mini | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, arr, n):
min_sum = arr[0]
sum1 = arr[0]
for i in range(1, n):
sum1 += arr[i]
min_sum = min(min_sum, arr[i], sum1)
if arr[i] < sum1:
sum1 = arr[i]
return min_sum | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Given an array arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the minimum sum and return its sum.
Example 1:
Input:
arr[] = {3,-4, 2,-3,-1, 7,-5}
Output: -6
Explanation: sub-array which has smallest
sum among all the sub-array is {-4,2,-3,-1} = -6
Example 2:
Input:
arr[] = {2, 6, 8, 1, 4}
Output: 1
Explanation: sub-array which has smallest
sum among all the sub-array is {1} = 1
Your Task:
You don't need to read input or print anything. The task is to complete the function smallestSubarraySum() which takes arr[] and N as input parameters and returns the sum of subarray with minimum sum.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{6}
-10^{7} ≤ A[i] ≤ 10^{7} | class Solution:
def smallestSumSubarray(self, arr, n):
prev_max, prev_min, glob_min = arr[0], arr[0], arr[0]
for i in range(1, n):
curr_max = max(prev_max + arr[i], prev_min + arr[i], arr[i])
curr_min = min(prev_max + arr[i], prev_min + arr[i], arr[i])
glob_min = min(glob_min, curr_min)
prev_max = curr_max
prev_min = curr_min
return glob_min | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | for _ in range(int(input())):
n = int(input())
a = input()
d = {(0): 1}
ss = 0
for i in range(n):
ss += int(a[i])
k = ss - (i + 1)
if k not in d:
d[k] = 1
else:
d[k] += 1
ss = 0
for i in d:
ss += d[i] * (d[i] - 1) // 2
print(ss) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | for time in range(int(input())):
array = []
n = int(input())
num = input()
for i in range(len(num)):
array.append(int(num[i]))
d = {}
ans = 0
diff = 0
for i in range(n):
if diff in d:
d[diff] += 1
else:
d[diff] = 1
diff += array[i] - 1
if diff in d:
ans += d[diff]
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | q = int(input())
for _ in range(q):
n = int(input())
s = list(input())
s = list(map(int, s))
l = [(s[i] - 1) for i in range(n)]
pref = [0] * (n + 1)
for i in range(1, n + 1):
pref[i] = pref[i - 1] + l[i - 1]
d = {}
for elt in pref:
try:
d[elt] += 1
except Exception:
d[elt] = 1
wyn = 0
for elt in d:
wyn += d[elt] * (d[elt] - 1) // 2
print(wyn) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input()))
d = {(0): 1}
sm = 0
ans = 0
for i in range(n):
x = arr[i] - 1
sm += x
ans += d.get(sm, 0)
d[sm] = d.get(sm, 0) + 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | ncases = int(input())
for tcase in range(ncases):
n = int(input())
nums = list(map(int, [i for i in input()]))
sums = {}
sums[0] = 1
current = 0
for i in nums:
current += i - 1
try:
sums[current] += 1
except KeyError:
sums[current] = 1
total = 0
for key in sums:
total += sums[key] * (sums[key] - 1) // 2
print(total) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | def main():
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, list(input())))
ar_sum = [0] * (n + 1)
for i in range(n):
ar_sum[i + 1] = a[i] + ar_sum[i]
m = {}
ans = 0
for i in range(1, n + 1):
m[ar_sum[i - 1] - i + 1] = m.get(ar_sum[i - 1] - i + 1, 0) + 1
if m.get(ar_sum[i] - i, -1) != -1:
ans += m[ar_sum[i] - i]
print(ans)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | (T,) = list(map(int, input().split()))
for t in range(T):
(N,) = list(map(int, input().split()))
X = [0] * (N + 1)
for i, c in enumerate(input().strip()):
X[i + 1] = X[i] + int(c)
d = dict()
for i in range(N + 1):
x = X[i] - i
if x not in d:
d[x] = 0
d[x] += 1
R = 0
for k in d:
R += d[k] * (d[k] - 1) // 2
print(R) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | for _ in range(int(input())):
n = int(input())
s = input()
arr = [(int(i) - 1) for i in s]
dic = {}
summ = 0
ans = 0
for i in range(n):
summ += arr[i]
if summ == 0:
ans += 1
if summ in dic:
ans += dic[summ]
try:
dic[summ] += 1
except:
dic[summ] = 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | import sys
def main():
T = int(sys.stdin.readline())
while T:
T -= 1
N = int(sys.stdin.readline())
Arr = [int(i) for i in sys.stdin.readline().rstrip()]
agg = [0] * N
agg[0] = Arr[0]
for i in range(1, N):
agg[i] = agg[i - 1] + Arr[i]
nxt_zero = [N] * (N + 1)
nxt_gtone = [N] * (N + 1)
nxt_gtone[N - 1] = N - 1
nxt_zero[N - 1] = N - 1
for i in range(N - 2, -1, -1):
nxt_gtone[i] = i if Arr[i] > 1 else nxt_gtone[i + 1]
nxt_zero[i] = i if not Arr[i] else nxt_zero[i + 1]
ans = 0
DP = [0] * (N + 1)
for i in range(N - 1, -1, -1):
j = i if Arr[i] == 1 else nxt_gtone[i]
while j < N:
cur = (agg[j] - agg[i - 1] if i else agg[j]) - (j - i + 1)
if not cur:
ans += 1
DP[i] += 1
if DP[j + 1]:
ans += DP[j + 1]
DP[i] += DP[j + 1]
break
if cur > 0:
if cur <= nxt_gtone[j + 1] - j:
j += cur
else:
j = nxt_zero[j + 1]
else:
j = nxt_gtone[j + 1]
print(ans)
main() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | for s in [*open(0)][2::2]:
d = {(0): 1}
r = t = 0
for x in s[:-1]:
t += int(x) - 1
x = d.get(t, 0)
r += x
d[t] = x + 1
print(r) | FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | def main():
t = int(input())
for i in range(t):
n = int(input())
s = input()
sums = dict()
sums[0] = 1
ans = 0
now = 0
for i in range(n):
a = int(s[i]) - 1
now += a
if now in sums:
ans += sums[now]
if now in sums:
sums[now] += 1
else:
sums[now] = 1
print(ans)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | t = int(input())
for tt in range(t):
n = int(input())
s = input().strip()
d = {}
d[0] = 1
pre = [0] * n
ans = 0
for i in range(n):
pre[i] = int(s[i])
if i > 0:
pre[i] += pre[i - 1]
xx = pre[i] - (i + 1)
if xx in d:
ans += d[xx]
if xx in d:
d[pre[i] - (i + 1)] += 1
else:
d[xx] = 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | def good(arr):
d = {(0): 1}
s = 0
ans = 0
for i in range(0, len(arr)):
s += arr[i]
x = s - i - 1
d[x] = d.get(x, 0) + 1
ans += d[x] - 1
return ans
for i in range(int(input())):
a = input()
lst = [int(i) for i in input()]
print(good(lst)) | FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | def solve(string):
val = string
prefix = [0]
for i in range(n):
prefix.append(prefix[-1] + int(val[i]))
one_sub = [i for i in range(n + 1)]
after = [(prefix[i] - one_sub[i]) for i in range(n + 1)]
hash_map = {}
res = 0
for i in after[::-1]:
hash_map[i] = hash_map.get(i, 0)
res += hash_map[i]
hash_map[i] += 1
print(res)
for _ in range(int(input())):
n = int(input())
string = list(map(int, input()))
solve(string) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | import sys
input = sys.stdin.readline
T = int(input())
for testcase in range(T):
n = int(input())
s = list(input())
a = []
for i in range(n):
a.append(int(s[i]) - 1)
d = dict()
res = 0
val = 0
d[0] = 1
for e in a:
val += e
if val in d:
res += d[val]
d[val] += 1
else:
d[val] = 1
print(res) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | for t in range(int(input())):
n = int(input())
a = list(map(int, input()))
pref = [(0) for i in range(n + 1)]
pref[0] = 0
for i in range(1, n + 1):
pref[i] = pref[i - 1] + a[i - 1] - 1
pref = sorted(pref) + [9 * 10**5 + 1]
cnt = 1
total = 0
for i in range(1, n + 2):
if pref[i] == pref[i - 1]:
cnt += 1
else:
total += cnt * (cnt - 1) // 2
cnt = 1
print(total) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | for _ in range(int(input())):
n = int(input())
a = input().strip()
currsum = 0
d = {(0): 1}
ans = 0
for i in a:
currsum += int(i) - 1
if currsum in d:
ans += d[currsum]
d[currsum] += 1
else:
d[currsum] = 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | def comb(n, r):
return n * (n - 1) // 2
a = int(input())
for i in range(0, a):
b = int(input())
s = input()
arr = []
for i in s:
arr.append(int(i))
summ = [0]
for i in range(0, len(arr)):
summ.append(summ[len(summ) - 1] + arr[i])
fsumm = []
for i in range(0, len(summ)):
fsumm.append(summ[i] - i)
s = set(fsumm)
d = dict()
for i in s:
d[i] = 0
for i in fsumm:
d[i] = d[i] + 1
summ = 0
for i in d.keys():
if not d[i] == 1:
summ = summ + comb(d[i], 2)
print(int(summ)) | FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | t = int(input())
for q in range(t):
n = int(input())
arr = [int(i) for i in input()]
dict = {(1): 1}
count = 0
sum = 0
i = 0
for item in arr:
sum += item
temp = sum - i
if temp in dict:
count += dict[temp]
dict[temp] += 1
else:
dict[temp] = 1
i += 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | for _ in range(int(input())):
n = int(input())
s = input()
ll = {}
sol = 0
b = 1
for i in s:
i = int(i)
if b in ll:
ll[b] += 1
else:
ll[b] = 1
b = b + i - 1
if b in ll:
sol += ll[b]
print(sol) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | for t in range(int(input())):
n = int(input())
a = list(input())
a = list(map(int, a))
for i in range(1, n):
a[i] = a[i] + a[i - 1]
a.insert(0, 0)
for i in range(n + 1):
a[i] = a[i] - i
d = {}
for el in a:
d[el] = d.get(el, 0) + 1
ans = 0
for el in d:
if d[el] > 1:
e = d[el]
ans = ans + e * (e - 1) // 2
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | t = int(input())
for _ in range(t):
n = int(input())
a = input()
f = {(0): 1}
current = 0
res = 0
for i in a:
current += int(i) - 1
if current in f:
res += f[current]
else:
f[current] = 0
f[current] += 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | for _ in range(int(input())):
length = int(input())
array = list(map(int, list(input())))
pi = [0, array[0]]
for i in range(2, length + 1):
pi += [pi[i - 1] + array[i - 1]]
pi_i = [(pi[i] - i) for i in range(length + 1)]
equal_pi_i = {}
for i in range(length + 1):
if pi_i[i] not in equal_pi_i:
equal_pi_i[pi_i[i]] = 1
else:
equal_pi_i[pi_i[i]] += 1
solution = 0
for value in equal_pi_i.values():
solution += (value - 1) * value / 2
print(int(solution)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR LIST BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | def process(n, c):
di = {(0): 1}
ans = 0
temp = 0
for ele in c:
temp += ele
if temp not in di:
di[temp] = 1
else:
di[temp] += 1
ans += di[temp] - 1
print(ans)
for t in range(int(input())):
n = int(input())
c = [(int(k) - 1) for k in input()]
process(n, c) | FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | def prefix(arr):
lst = [0] * len(arr)
lst[0] = arr[0]
for i in range(1, len(arr)):
lst[i] = lst[i - 1] + arr[i]
return lst
def good(arr):
pre = prefix(arr)
dic = {}
ans = 0
for i in range(len(pre)):
if pre[i] == i + 1:
ans += 1
if pre[i] - i not in dic:
dic[pre[i] - i] = 1
else:
dic[pre[i] - i] += 1
for i in dic.values():
ans += i * (i - 1) // 2
return ans
t = int(input())
for i in range(t):
n = int(input())
st = input()
arr = list(st)
for i in range(len(arr)):
arr[i] = int(arr[i])
print(good(arr)) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int, list(input()[:n])))
ans = 0
d = [0] * (n * 10)
x = 0
for i in range(n):
d[x] += 1
x += a[i] - 1
ans += d[x]
print(ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).
For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.
Calculate the number of good subarrays of the array $a$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.
The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer — the number of good subarrays of the array $a$.
-----Example-----
Input
3
3
120
5
11011
6
600005
Output
3
6
1
-----Note-----
The first test case is considered in the statement.
In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$.
In the third test case there is only one good subarray: $a_{2 \dots 6}$. | t = int(input())
for _ in range(t):
n = int(input())
arr = [(int(i) - 1) for i in input()]
freq = {(0): 1}
pre = 0
for i in range(n):
pre += arr[i]
freq[pre] = freq.get(pre, 0) + 1
ans = 0
for i in freq:
m = freq[i]
ans += m * (m - 1) // 2
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.