description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): dp = [([0] * n) for i in range(n)] for i in range(n): r, c = 0, i while c < n: sm = sum(freq[r : c + 1]) sub = float("INF") for curr in range(r, c + 1): left = 0 right = 0 if curr - 1 >= r: left = dp[r][curr - 1] if curr + 1 <= c: right = dp[curr + 1][c] sub = min(sub, left + right) if sub == float("INF"): sub = 0 dp[r][c] = sm + sub r += 1 c += 1 return dp[0][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def sumFreq(self, i, j, freq): Sum = 0 for k in range(i, j): Sum += freq[k] return Sum def cost(self, i, j, freq, dp): if i >= j: dp[i][j] = 0 return dp[i][j] if j - i == 1: dp[i][j] = freq[i] return dp[i][j] if dp[i][j] != -1: return dp[i][j] weight = self.sumFreq(i, j, freq) ans = float("inf") for k in range(i, j): temp = self.cost(i, k, freq, dp) + self.cost(k + 1, j, freq, dp) ans = min(ans, temp) dp[i][j] = ans + weight return dp[i][j] def optimalSearchTree(self, keys, freq, n): dp = [[(-1) for j in range(n + 1)] for i in range(n + 1)] return self.cost(0, n, freq, dp)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP 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 RETURN FUNC_CALL VAR NUMBER VAR VAR VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): dp = [[(0) for j in range(n)] for i in range(n)] psa = [0] * n psa[0] = freq[0] for i in range(1, n): psa[i] = psa[i - 1] + freq[i] for g in range(n): i = 0 j = g while j < len(dp): if g == 0: dp[i][j] = freq[i] elif g == 1: f1 = freq[i] f2 = freq[j] dp[i][j] = min(f1 + 2 * f2, 2 * f1 + f2) else: mini = float("inf") if i == 0: val = 0 else: val = psa[i - 1] fs = psa[j] - val for k in range(i, j + 1): if k == i: left = 0 else: left = dp[i][k - 1] if k == j: right = 0 else: right = dp[k + 1][j] if left + right + fs < mini: mini = left + right + fs dp[i][j] = mini i += 1 j += 1 return dp[0][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
import sys class Solution: def optimalSearchTree(self, keys, freq, n): dp = [[(0) for _ in range(n)] for _ in range(n)] for g in range(n): i = 0 j = g while j < n: if g == 0: dp[i][j] = freq[i] elif g == 1: dp[i][j] = min(1 * freq[i] + 2 * freq[j], 1 * freq[j] + 2 * freq[i]) else: freq_sum = 0 min_cost = float("inf") for k in range(i, j + 1): left = dp[i][k - 1] if k > 0 else 0 right = dp[k + 1][j] if k < j else 0 freq_sum += freq[k] min_cost = min(min_cost, left + right) dp[i][j] = min_cost + freq_sum i += 1 j += 1 return dp[0][n - 1]
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): def f(i, j, freq, keys, dp): if i > j: return 0 if i == j: return freq[i] if dp[i][j] != -1: return dp[i][j] fsum = sum(freq[i : j + 1]) mn = 1000000000.0 for k in range(i, j + 1): mn = min( mn, f(i, k - 1, freq, keys, dp) + f(k + 1, j, freq, keys, dp) + fsum ) dp[i][j] = mn return mn dp = [([-1] * n) for p in range(n)] return f(0, n - 1, freq, keys, dp)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR 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 NUMBER BIN_OP VAR NUMBER VAR VAR VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): dp = [[(0) for j in range(n)] for i in range(n)] for i in range(n): for j in range(n): if i == j: dp[i][j] = freq[i] x = -1 k = 1 for i in range(n - 1): j = 0 x += 1 while j < n - 1 - x: if i == 0: dp[j][k] = min( dp[j][k - 1] * 1 + dp[j + 1][k] * 2, dp[j][k - 1] * 2 + dp[j + 1][k] * 1, ) else: extra = sum(freq[j : k + 1]) min_c = float("inf") for y in range(j, k + 1): if y == 0: min_c = min(min_c, extra + 0 + dp[y + 1][k]) elif y == n - 1: min_c = min(min_c, extra + dp[j][y - 1] + 0) else: min_c = min(min_c, extra + dp[j][y - 1] + dp[y + 1][k]) dp[j][k] = min_c j += 1 k += 1 k = x + 2 return dp[0][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): dp = [([None] * n) for _ in range(n)] def hi(l1, l2): if l2 < l1: return 0 if l1 == l2: dp[l1][l1] = freq[l1] return freq[l1] else: if dp[l1][l2] != None: return dp[l1][l2] best = [] s = sum(freq[l1 : l2 + 1]) for a in range(l1, l2 + 1): l = hi(l1, a - 1) m = hi(a, a) r = hi(a + 1, l2) best.append(l + r + s) dp[l1][l2] = min(best) return dp[l1][l2] return hi(0, n - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR NONE RETURN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): dp = [[(0) for i in range(n)] for j in range(n)] keys, freq = zip(*sorted(zip(keys, freq))) for g in range(n): for i in range(n - g): j = i + g if g == 0: dp[i][j] = freq[i] elif g == 1: f1 = freq[i] f2 = freq[j] dp[i][j] = min(2 * f1 + f2, 2 * f2 + f1) else: freq_sum = 0 for x in range(i, j + 1): freq_sum += freq[x] min_v = float("inf") for k in range(i, j + 1): left = dp[i][k - 1] if k != i else 0 right = dp[k + 1][j] if k != j else 0 min_v = min(min_v, left + right) dp[i][j] = min_v + freq_sum return dp[0][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, a, p, n): n += 1 a.insert(0, 0) p.insert(0, 0) l = [[[0, 0] for j in range(n)] for i in range(n)] for i in range(n): for j in range(n - i): k = j + i if i == 0: pass elif i == 1: l[j][k][0] = p[k] l[j][k][1] = l[j][k - 1][1] + l[k][k][1] + l[j][k][0] else: l[j][k][0] = l[j][k - 1][0] + p[k] m = 999999 for x in range(j + 1, k + 1): y = l[j][x - 1][1] + l[x][k][1] if y < m: m = y l[j][k][1] = m + l[j][k][0] return l[0][n - 1][1]
CLASS_DEF FUNC_DEF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def compute_cost(self, arr): n = len(arr) result = [[(0) for col in range(n)] for row in range(n)] for row in range(n): for col in range(row, n): if row == col: result[row][col] = arr[col] else: result[row][col] = arr[col] + result[row][col - 1] return result def helper(self, arr, i, j, cost, mem): if i > j: return 0 if mem[i][j] != -1: return mem[i][j] ans = float("inf") for k in range(i, j + 1): temp_ans = ( cost[i][j] + self.helper(arr, i, k - 1, cost, mem) + self.helper(arr, k + 1, j, cost, mem) ) ans = min(ans, temp_ans) mem[i][j] = ans return ans def optimalSearchTree(self, keys, freq, n): cost = self.compute_cost(freq) mem = [[(-1) for col in range(n + 1)] for row in range(n + 1)] return self.helper(freq, 0, n - 1, cost, mem)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR 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 BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def cost(self, arr, i, j): count = 0 while i <= j: count += arr[i] i += 1 return count def helper(self, i, j, freq, mem): if i > j: return 0 key = str(i) + ":" + str(j) if key in mem: return mem[key] cost = self.cost(freq, i, j) ans = float("inf") for k in range(i, j + 1): temp_ans = ( cost + self.helper(i, k - 1, freq, mem) + self.helper(k + 1, j, freq, mem) ) ans = min(ans, temp_ans) mem[key] = ans return ans def optimalSearchTree(self, keys, freq, n): mem = {} return self.helper(0, n - 1, freq, mem)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): memo = {} def optCost(freq, i, j): if j < i: return 0 if j == i: return freq[i] if (i, j) in memo: return memo[i, j] fsum = sum(freq[i : j + 1]) minval = float("inf") for r in range(i, j + 1): cost = optCost(freq, i, r - 1) + optCost(freq, r + 1, j) if cost < minval: minval = cost memo[i, j] = minval + fsum return memo[i, j] return optCost(freq, 0, n - 1) if __name__ == "__main__": t = int(input()) for _ in range(t): n = int(input()) keys = input().split() for itr in range(n): keys[itr] = int(keys[itr]) freq = input().split() for itr in range(n): freq[itr] = int(freq[itr]) ob = Solution() print(ob.optimalSearchTree(keys, freq, n))
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): t = [[(-1) for i in range(n + 1)] for j in range(n + 1)] fsum = [0] * n fsum[0] = freq[0] for i in range(1, n): fsum[i] = freq[i] + fsum[i - 1] for gap in range(0, n): i = 0 for j in range(gap, n): if gap == 0: t[i][j] = freq[i] elif gap == 1: v1 = freq[i] v2 = freq[j] t[i][j] = min(v1 + 2 * v2, v2 + v1 * 2) else: mn = 2147483647 fs = fsum[j] - (0 if i == 0 else fsum[i - 1]) for k in range(i, j + 1): left = 0 if k == i else t[i][k - 1] right = 0 if k == j else t[k + 1][j] if left + right + fs < mn: mn = left + right + fs t[i][j] = mn i += 1 return t[0][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 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 VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): pref = [0] for i in range(0, n): pref.append(pref[-1] + freq[i]) dp = [[(0) for i in range(n + 1)] for j in range(n + 1)] for i in range(1, n + 1): c = 0 for j in range(i, n + 1): m = 999999999 for k in range(c, j): m = min(m, dp[c][k] + dp[k + 1][j] + pref[j] - pref[c]) dp[c][j] = m c += 1 return dp[0][n]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR NUMBER VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
INT_MAX = 1000000 def sum1(freq, i, j): s = 0 for k in range(i, j + 1): s += freq[k] return s class Solution: def optimalSearchTree(self, keys, freq, n): cost = [[INT_MAX for x in range(n)] for y in range(n)] for g in range(0, n): i = 0 for j in range(g, n): if g == 0: cost[i][j] = freq[i] elif g == 1: cost[i][j] = min(freq[i] + 2 * freq[j], 2 * freq[i] + freq[j]) else: min1 = INT_MAX sum2 = sum1(freq, i, j) for k in range(i, j + 1): left = 0 if k > i: left = cost[i][k - 1] right = 0 if k < j: right = cost[k + 1][j] if left + right + sum2 < min1: min1 = left + right + sum2 cost[i][j] = min1 i += 1 return cost[0][n - 1]
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
INF = float("inf") class Solution: def optimalSearchTree(self, keys, freq, n): N = len(keys) dp = [([INF] * (N + 2)) for _ in range(N + 2)] keys = [0] + keys freq = [0] + freq for i in range(1, N + 1): dp[i][i] = freq[i] dp[i][i - 1] = 0 dp[N + 1][N] = 0 for r in range(1, N): for i in range(1, N - r + 1): j = r + i freq_sum = 0 for index in range(i, j + 1): freq_sum += freq[index] for k in range(i, j + 1): dp[i][j] = min(dp[i][j], dp[i][k - 1] + dp[k + 1][j] + freq_sum) return dp[1][N]
ASSIGN VAR FUNC_CALL VAR STRING CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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 VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
import sys INT_MAX = sys.maxsize class Solution: def optimalSearchTree(self, keys, freq, n): dp = [[(-1) for _ in range(n)] for _ in range(n)] pre_sum = [(-1) for _ in range(n)] pre_sum[0] = freq[0] for i in range(1, n): pre_sum[i] = pre_sum[i - 1] + freq[i] for l in range(1, n + 1): for i in range(n - l + 1): j = i + l - 1 if l == 1: dp[i][j] = freq[i] elif l == 2: dp[i][j] = min(freq[i] + 2 * freq[j], 2 * freq[i] + freq[j]) else: fs = pre_sum[j] if i == 0 else pre_sum[j] - pre_sum[i - 1] min_val = INT_MAX for k in range(i, j + 1): left = 0 if k == i else dp[i][k - 1] right = 0 if k == j else dp[k + 1][j] min_val = min(min_val, left + right + fs) dp[i][j] = min_val return dp[0][n - 1]
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
import sys from sys import maxsize class Solution: def optimalSearchTree(self, keys, freq, n): freqSum = [(0) for i in range(n)] freqSum[0] = freq[0] for i in range(1, n): freqSum[i] = freq[i] + freqSum[i - 1] cost = [[maxsize for i in range(n + 1)] for j in range(n + 1)] for i in range(n): cost[i][i] = freq[i] for size in range(2, n + 1): for startIdx in range(n - size + 2): endIdx = min(n - 1, startIdx + size - 1) for curr in range(startIdx, endIdx + 1): currTotalCost = 0 if startIdx > 0: currTotalCost = freqSum[endIdx] - freqSum[startIdx - 1] else: currTotalCost = freqSum[endIdx] if curr != startIdx: currTotalCost += cost[startIdx][curr - 1] if curr != endIdx: currTotalCost += cost[curr + 1][endIdx] if currTotalCost < cost[startIdx][endIdx]: cost[startIdx][endIdx] = currTotalCost return cost[0][n - 1]
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): dp = [[(0) for i in range(n)] for j in range(n)] pref = [i for i in freq] for i in range(1, n): pref[i] += pref[i - 1] for g in range(n): i = 0 j = i + g while j < n: if g == 0: dp[i][j] = freq[i] else: m = float("inf") for k in range(i, j + 1): l = dp[i][k - 1] if k - 1 >= 0 else 0 r = dp[k + 1][j] if k + 1 < n else 0 m = min(m, l + r) dp[i][j] = m + pref[j] if i != 0: dp[i][j] -= pref[i - 1] i += 1 j += 1 return dp[0][-1] if __name__ == "__main__": t = int(input()) for _ in range(t): n = int(input()) keys = input().split() for itr in range(n): keys[itr] = int(keys[itr]) freq = input().split() for itr in range(n): freq[itr] = int(freq[itr]) ob = Solution() print(ob.optimalSearchTree(keys, freq, n))
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): dp = [[(0) for i in range(n)] for j in range(n)] for g in range(n): for i, j in zip(range(n - g), range(g, n)): if g == 0: dp[i][j] = freq[i] if g == 1: dp[i][j] = min(2 * freq[i] + freq[j], 2 * freq[j] + freq[i]) else: mini = 99999999999999 sumi = 0 for x in range(i, j + 1): sumi += freq[x] for k in range(i, j + 1): left = 0 if k == i else dp[i][k - 1] right = 0 if k == j else dp[k + 1][j] if left + right + sumi < mini: mini = left + right + sumi dp[i][j] = mini return dp[0][n - 1] if __name__ == "__main__": t = int(input()) for _ in range(t): n = int(input()) keys = input().split() for itr in range(n): keys[itr] = int(keys[itr]) freq = input().split() for itr in range(n): freq[itr] = int(freq[itr]) ob = Solution() print(ob.optimalSearchTree(keys, freq, n))
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
INT_MAX = 2147483647 class Solution: def optimalSearchTree(self, keys, freq, n): cost = [[(0) for x in range(n)] for y in range(n)] for i in range(0, n): cost[i][i] = freq[i] for length in range(2, n + 1): for i in range(0, n - length + 1): j = i + length - 1 if length == 2: cost[i][j] = sum(freq[i : j + 1]) + min(cost[i][i], cost[j][j]) else: minCost = INT_MAX currCost = 0 for k in range(i, j + 1): if k - 1 >= 0 and k + 1 < n: currCost = cost[i][k - 1] + cost[k + 1][j] else: if k - 1 < 0: currCost = cost[k + 1][j] if k + 1 >= n: currCost = cost[i][k - 1] minCost = min(minCost, currCost) cost[i][j] = sum(freq[i : j + 1]) + minCost return cost[0][n - 1]
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
def sum(freq, i, j): s = 0 for r in range(i, j + 1): s += freq[r] return s def optCost(freq, i, j, costs): if i > j: return 0 if costs[i][j] > 0: return costs[i][j] if i == j: return freq[i] min = 999999999 fsum = sum(freq, i, j) for r in range(i, j + 1): cost = optCost(freq, i, r - 1, costs) + optCost(freq, r + 1, j, costs) if min > cost: min = cost costs[i][j] = fsum + min return costs[i][j] class Solution: def optimalSearchTree(self, keys, freq, n): n = len(freq) costs = [([-1] * n) for i in range(n)] return optCost(freq, 0, n - 1, costs) if __name__ == "__main__": t = int(input()) for _ in range(t): n = int(input()) keys = input().split() for itr in range(n): keys[itr] = int(keys[itr]) freq = input().split() for itr in range(n): freq[itr] = int(freq[itr]) ob = Solution() print(ob.optimalSearchTree(keys, freq, n))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def sumFreq(self, i, j, freq): su = 0 for k in range(i, j + 1): su += freq[k] return su def solve(self, left, right, freq, dp): if left > right: return 0 if dp[left][right] != -1: return dp[left][right] weight = self.sumFreq(left, right, freq) ans = 1000000000.0 for i in range(left, right + 1): ans = min( ans, self.solve(left, i - 1, freq, dp) + self.solve(i + 1, right, freq, dp), ) dp[left][right] = ans + weight return dp[left][right] def optimalSearchTree(self, keys, freq, n): dp = [[(-1) for i in range(n + 1)] for j in range(n + 1)] return self.solve(0, n - 1, freq, dp)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP 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 RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
from sys import maxsize class Solution: def solve(self, i, j, dp, freq): if i > j: return 0 if i == j: dp[i][j] = freq[i] return dp[i][j] if dp[i][j] != -1: return dp[i][j] ans = maxsize wt = sum(freq[i : j + 1]) for k in range(i, j + 1): temp = self.solve(i, k - 1, dp, freq) + self.solve(k + 1, j, dp, freq) ans = min(ans, temp) dp[i][j] = ans + wt return dp[i][j] def optimalSearchTree(self, keys, freq, n): dp = [[(-1) for i in range(n)] for j in range(n)] return self.solve(0, n - 1, dp, freq)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): def sum(i, j, freq): sum1 = 0 for k in range(i, j + 1): sum1 += freq[k] return sum1 dp = [([-1] * (n + 1)) for j in range(n + 1)] def cost(i, j, freq): if i > j: return 0 if i == j: dp[i][j] = freq[i] if dp[i][j] != -1: return dp[i][j] min1 = float("inf") weight = sum(i, j, freq) for k in range(i, j + 1): ans = cost(i, k - 1, freq) + cost(k + 1, j, freq) min1 = min(min1, ans) dp[i][j] = min1 + weight return dp[i][j] return cost(0, n - 1, freq)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def optimalSearchTree(self, keys, freq, n): dp = [(["."] * n) for i in range(n)] for g in range(n): for i, j in zip(range(n - g), range(g, n)): if g == 0: dp[i][j] = freq[i] elif g == 1: f1 = freq[i] f2 = freq[j] dp[i][j] = min(f1 + 2 * f2, 2 * f1 + f2) else: Min = 10000000000.0 + 7 freqSum = 0 for x in range(i, j + 1): freqSum += freq[x] for k in range(i, j + 1): if k == i: left = 0 else: left = dp[i][k - 1] if k == j: right = 0 else: right = dp[k + 1][j] if left + right + freqSum < Min: Min = left + right + freqSum dp[i][j] = Min return dp[0][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
class Solution: def compute(self, arr, start, end): ans = 0 while start < end: ans += arr[start] ans += arr[end] start += 1 end -= 1 if start == end: ans += arr[start] return ans def optimal_helper(self, keys, freq, left, right, mem): if left > right: return 0 if left == right: return freq[left] if mem[left][right] != -1: return mem[left][right] intermediate_cost = self.compute(freq, left, right) min_cost = float("inf") for curr in range(left, right + 1): curr_cost = ( intermediate_cost + self.optimal_helper(keys, freq, left, curr - 1, mem) + self.optimal_helper(keys, freq, curr + 1, right, mem) ) min_cost = min(min_cost, curr_cost) mem[left][right] = min_cost return min_cost def optimalSearchTree(self, keys, freq, n): mem = [[(-1) for i in range(n)] for j in range(n)] return self.optimal_helper(keys, freq, 0, n - 1, mem)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
MAX = 10**9 + 7 class Solution: def solve(self, keys, freq, i, j): if i > j: return 0 elif self.dp[i][j] != -1: return self.dp[i][j] total = sum(freq[i : j + 1]) cost = MAX for k in range(i, j + 1): if self.dp[i][k - 1] != -1: if self.dp[k + 1][j] != -1: temp = self.dp[i][k - 1] + self.dp[k + 1][j] + total else: temp = self.dp[i][k - 1] + self.solve(keys, freq, k + 1, j) + total elif self.dp[k + 1][j] != -1: temp = self.solve(keys, freq, i, k - 1) + self.dp[k + 1][j] + total else: temp = ( self.solve(keys, freq, i, k - 1) + self.solve(keys, freq, k + 1, j) + total ) cost = min(cost, temp) self.dp[i][j] = cost return cost def optimalSearchTree(self, keys, freq, n): self.dp = [[(-1) for i in range(110)] for j in range(110)] return self.solve(keys, freq, 0, n - 1)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible. Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1. Example 1: Input: n = 2 keys = {10, 12} freq = {34, 50} Output: 118 Explaination: There can be following two possible BSTs 10 12 \ / 12 10 The cost of tree I is 34*1 + 50*2 = 134 The cost of tree II is 50*1 + 34*2 = 118 Example 2: Input: N = 3 keys = {10, 12, 20} freq = {34, 8, 50} Output: 142 Explaination: There can be many possible BSTs 20 / 10 \ 12 Among all possible BSTs, cost of this BST is minimum. Cost of this BST is 1*50 + 2*34 + 3*8 = 142 Your Task: You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible. Expected Time Complexity: O(n^{3}) Expected Auxiliary Space: O(n^{2}) Constraints: 1 ≤ N ≤ 100
def f(freq, i, j, dp): if i > j: return 0 if dp[i][j] != -1: return dp[i][j] cost = sum(freq[i : j + 1]) ans = 1000000000.0 for k in range(i, j + 1): c = cost + f(freq, i, k - 1, dp) + f(freq, k + 1, j, dp) ans = min(ans, c) dp[i][j] = ans return ans class Solution: def optimalSearchTree(self, keys, freq, n): dp = [[(-1) for i in range(n)] for i in range(n)] return f(freq, 0, n - 1, dp)
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: mod = 10**9 + 7 for i in range(len(arr)): arr[i] %= 2 n = len(arr) dp = [] ones = 0 zeroes = 0 for i in arr: if not dp: dp.append(i) else: dp.append(dp[-1] + i) dp[-1] %= 2 if dp[-1] == 0: zeroes += 1 else: ones += 1 total = n * (n + 1) // 2 total -= ones * (ones - 1) // 2 total -= zeroes + zeroes * (zeroes - 1) // 2 return total % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: dp = [[0, 0] for _ in range(len(arr) + 1)] for i, num in enumerate(arr): if num % 2 == 0: dp[i + 1][0] = dp[i][0] dp[i + 1][1] = dp[i][1] + 1 else: dp[i + 1][0] = dp[i][1] + 1 dp[i + 1][1] = dp[i][0] return sum(dp[i][0] for i in range(len(dp))) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: MOD = 10**9 + 7 count = [1, 0] cur = answer = 0 for n in arr: cur ^= n & 1 answer = (answer + count[1 ^ cur]) % MOD count[cur] += 1 return answer
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: mod = 10**9 + 7 result = 0 sumOdd = [0] * len(arr) sumEven = [0] * len(arr) if arr[0] % 2 == 1: sumOdd[0] = 1 sumEven[0] = 0 else: sumOdd[0] = 0 sumEven[0] = 1 for i in range(1, len(sumOdd)): if arr[i] % 2 == 0: sumOdd[i] = sumOdd[i - 1] sumEven[i] = sumEven[i - 1] + 1 else: sumOdd[i] = sumEven[i - 1] + 1 sumEven[i] = sumOdd[i - 1] return sum(sumOdd) % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: sums = [0] sum = 0 for n in arr: sum += n sums.append(sum) odd_sum_count = [] even_sum_count = [] odd_sum = 0 even_sum = 0 for ss in sums: odd_sum += 1 if ss % 2 == 1 else 0 even_sum += 0 if ss % 2 == 1 else 1 odd_sum_count.append(odd_sum) even_sum_count.append(even_sum) ans = 0 for i in range(len(arr)): if sums[i + 1] % 2 == 0: ans += odd_sum_count[i] else: ans += even_sum_count[i] ans = ans % (10**9 + 7) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: ans, odd, even = 0, 0, 0 for num in arr: if num % 2 != 0: odd, even = even + 1, odd else: odd, even = odd, even + 1 ans += odd return ans % int(1000000000.0 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: if not arr: return 0 n_even = 0 n_odd = 0 res = 0 for x in arr: if x % 2 == 0: n_even, n_odd = n_even + 1, n_odd else: n_even, n_odd = n_odd, n_even + 1 res += n_odd return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: mod = 10**9 + 7 n = len(arr) dp = [([0] * 2) for _ in range(n)] if arr[n - 1] & 1: dp[n - 1][1] = 1 else: dp[n - 1][0] = 1 for i in range(n - 2, -1, -1): if arr[i] & 1: dp[i][1] = (dp[i + 1][0] + 1) % mod dp[i][0] = dp[i + 1][1] else: dp[i][1] = dp[i + 1][1] dp[i][0] = (dp[i + 1][0] + 1) % mod ans = 0 for i in range(n): ans += dp[i][1] return ans % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: kMod = int(1000000000.0 + 7) n = len(arr) dp = [([0] * 2) for _ in range(n)] ans = 0 if arr[0] % 2 == 0: dp[0][0] = 1 else: dp[0][1] = 1 ans += dp[0][1] for i in range(1, n): if arr[i] % 2 != 0: dp[i][0] = dp[i - 1][1] dp[i][1] = dp[i - 1][0] + 1 else: dp[i][0] = dp[i - 1][0] + 1 dp[i][1] = dp[i - 1][1] ans += dp[i][1] return ans % kMod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: MOD = 10**9 + 7 memo = {(0): 0, (1): 0} cumsum = 0 res = 0 for v in arr: cumsum += v if cumsum % 2 == 0: memo[0] += 1 res = (res + memo[1]) % MOD else: memo[1] += 1 res = (1 + res + memo[0]) % MOD return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
from itertools import accumulate class Solution: def numOfSubarrays(self, arr: List[int]) -> int: def num_subarrays(k): return (k * k + k) // 2 counts = [0, 0] for prefix in accumulate(arr): counts[prefix % 2] += 1 evens, odds = counts return ( num_subarrays(len(arr)) - (num_subarrays(evens) + (num_subarrays(odds) - odds)) ) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: n = len(arr) ret = 0 odds, evens = 0, 1 cur = 0 for num in arr: cur ^= num & 1 if cur: ret += evens odds += 1 else: ret += odds evens += 1 return ret % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: c, e, o, a = 0, 1, 0, 0 for i in arr: c += i if c % 2 == 0: a += o a %= 1000000007 e += 1 else: a += e a %= 1000000007 o += 1 return a % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: odds, counts = 0, [1, 0] for x in arr: odds += 1 if x % 2 else 0 counts[odds % 2] += 1 return counts[0] * counts[1] % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: mod = 10**9 + 7 odd_even_count = [[0, 0] for _ in range(len(arr) + 1)] prefix_odd_sum = [(-1) for _ in range(len(arr))] cur = 0 odd_count = 0 even_count = 0 for idx in range(len(arr)): cur += arr[idx] prefix_odd_sum[idx] = cur % 2 if cur % 2 == 1: odd_count += 1 else: even_count += 1 odd_even_count[idx + 1] = odd_count, even_count ans = 0 for idx in range(len(arr)): is_odd = prefix_odd_sum[idx] if is_odd: ans += 1 + odd_even_count[idx][1] else: ans += odd_even_count[idx][0] return ans % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: prefix_sum = 0 number_odd = 0 number_even = 0 total = 0 for i in arr: prefix_sum += i if prefix_sum % 2 == 1: number_odd += 1 total += 1 total += number_even else: total += number_odd number_even += 1 return int(total % (1000000000.0 + 7))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOddEvenSubarrays(self, arr: List[int], start: int) -> (int, int, int): if len(arr) == 0: return 0, 0, 0 if len(arr) == 1 or start == len(arr) - 1: return (0, 1, 0) if arr[start] % 2 == 0 else (1, 0, 0) odd, even, oldOdd = self.numOddEvenSubarrays(arr, start + 1) indOdd = (odd + oldOdd) % (10**9 + 7) return ( (odd, even + 1, indOdd) if arr[start] % 2 == 0 else (even + 1, odd, indOdd) ) def numOfSubarrays(self, arr: List[int]) -> int: odd, even, oldOdd = self.numOddEvenSubarrays(arr, 0) total = (odd + oldOdd) % (10**9 + 7) return total
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: N = len(arr) if N == 0: return 0 elif N == 1: return abs(arr[0]) % 2 l1 = 0 l2 = 0 if arr[0] % 2: l1 += 1 else: l2 += 1 tot = 0 tot += l1 for i in range(1, N): l3 = 0 l4 = 0 if arr[i] % 2: l3 = 1 + l2 l4 = l1 else: l3 = l1 l4 = 1 + l2 l2 = l4 l1 = l3 tot += l1 return tot % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: n = len(arr) odd, even = [0] * n, [0] * n for i, v in enumerate(arr): if not i: odd[i] += v % 2 == 1 even[i] += v % 2 != 1 elif v % 2: odd[i] += 1 + even[i - 1] even[i] += odd[i - 1] else: odd[i] += odd[i - 1] even[i] += 1 + even[i - 1] return sum(odd) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: res, s, prev_odd, prev_even = 0, 0, 0, 1 for v in arr: s = (s + v) % 2 if s == 1: res += prev_even prev_odd += 1 else: res += prev_odd prev_even += 1 return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: odd = even = ans = 0 mod = 10**9 + 7 for num in arr: if num % 2 == 1: ans += even + 1 curr_even = even even = odd odd = curr_even + 1 else: ans += odd even += 1 return ans % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: L = len(arr) works = [0] * L not_works = [0] * L if arr[0] % 2 == 1: works[0] += 1 else: not_works[0] += 1 for i in range(1, L): if arr[i] % 2 == 0: works[i] += works[i - 1] not_works[i] += not_works[i - 1] else: works[i] += not_works[i - 1] not_works[i] += works[i - 1] if arr[i] % 2 == 1: works[i] += 1 else: not_works[i] += 1 return sum(works) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: n = len(arr) odd, even, re, s = 0, 1, 0, 0 for v in arr: s += v if s % 2 == 0: re += odd even += 1 else: re += even odd += 1 return re % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: odds = 0 evens = 1 ans = 0 runningsum = 0 MOD = 10**9 + 7 for a in arr: runningsum += a if runningsum % 2: ans = (ans + evens) % MOD odds += 1 else: ans = (ans + odds) % MOD evens += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
def isEven(n): return n % 2 == 0 def isOdd(n): return not isEven(n) class Solution: def numOfSubarrays(self, arr: List[int]) -> int: od = list(range(len(arr))) ev = list(range(len(arr))) if isEven(arr[0]): od[0] = 0 ev[0] = 1 else: od[0] = 1 ev[0] = 0 for i, num in enumerate(arr): if i == 0: continue if isOdd(num): od[i] = ev[i - 1] + 1 ev[i] = od[i - 1] else: od[i] = od[i - 1] ev[i] = ev[i - 1] + 1 ans = 0 for num in od: ans += num return ans % (10**9 + 7)
FUNC_DEF RETURN BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: n = len(arr) mod = 10**9 + 7 ans = 0 p = [0] * (n + 1) counter = Counter([0]) for i in range(n): p[i] = p[i - 1] + arr[i] if p[i] % 2: ans += counter[0] else: ans += counter[1] counter[p[i] % 2] += 1 return ans % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: n = len(arr) d = {} d[0] = 1 sm = 0 even = 0 for i in range(n): sm += arr[i] sm %= 2 if sm < 0: sm += 2 if sm in d: even += d[sm] if sm not in d: d[sm] = 0 d[sm] += 1 return (n * (n + 1) // 2 - even) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, coll: List[int]) -> int: n = len(coll) m = 10**9 + 7 acc = 0 evens = odds = 0 for i, x in enumerate(coll): if x & 1: evens, odds = odds, evens + 1 else: evens += 1 acc += odds acc %= m return acc
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: res = 0 odds = 0 even = 0 for i, c in enumerate(arr): if c & 1: odds, even = even + 1, odds else: even += 1 res = (res + odds) % 1000000007 return res % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: le = len(arr) o = 0 s = 0 for i in arr: s += i if s % 2: o += 1 return o * (le - o + 1) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: res = odd = even = 0 for x in arr: even += 1 if x % 2: odd, even = even, odd res = (res + odd) % 1000000007 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: odd = 0 even = 1 res = 0 sum = 0 for i in arr: sum += i if sum % 2 == 0: even += 1 res = (res + odd) % 1000000007 else: odd += 1 res = (res + even) % 1000000007 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: mod = 10**9 + 7 odd_presum_cnt = 0 par = 0 for a in arr: par ^= a & 1 if par: odd_presum_cnt += 1 return odd_presum_cnt * (len(arr) + 1 - odd_presum_cnt) % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: o = [] score = 0 tmp = 0 pa, no = 0, 0 check = [] for el in arr: tmp += el if tmp % 2 == 0: pa += 1 check.append(0) else: no += 1 check.append(1) o.append((pa, no)) score = 0 for i in range(len(arr)): if arr[i] % 2 == 1: score += 1 if check[i - 1] == 0 or i == 0: score += o[-1][1] - o[i][1] else: score += o[-1][0] - o[i][0] mod = 10**9 + 7 return score % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
MOD = 10**9 + 7 class Solution: def numOfSubarrays(self, arr: List[int]) -> int: odd_cum_sums = 0 even_cum_sums = 0 total_odd_sums = 0 for i in range(len(arr) - 1): arr[i + 1] += arr[i] for sum_ in arr: if sum_ % 2 == 0: total_odd_sums += odd_cum_sums even_cum_sums += 1 else: total_odd_sums += 1 + even_cum_sums odd_cum_sums += 1 return total_odd_sums % MOD
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: r = 0 e = 0 o = 0 for i in arr: if i % 2 == 1: r += e + 1 o, e = e + 1, o else: r += o o, e = o, e + 1 a = 10**9 + 7 return r % a
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: mod = 10**9 + 7 dic = collections.Counter() dic[0] += 1 pre = ans = 0 for i, x in enumerate(arr): pre += x pre %= 2 ans = (ans + dic[pre ^ 1]) % mod dic[pre] += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: s = [0, 0] res = 0 cur = 0 for x in arr: cur = (cur + x) % 2 res += s[1 - cur] res += int(cur == 1) s[cur] += 1 return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: count = len(arr) isodd = [(item % 2) for item in arr] odd_end_total = [0] * count even_end_total = [0] * count odd_end_total[0] = isodd[0] even_end_total[0] = 1 - odd_end_total[0] for index in range(1, count): if isodd[index]: odd_end_total[index] = 1 + even_end_total[index - 1] even_end_total[index] = odd_end_total[index - 1] else: odd_end_total[index] = odd_end_total[index - 1] even_end_total[index] = 1 + even_end_total[index - 1] result = 0 for index in range(count): result += odd_end_total[index] return result % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: odd = 0 even = 0 n = len(arr) sumlist = [0] * n output = 0 for i in range(n - 1, -1, -1): if i != n - 1: sumlist[i] += arr[i] + sumlist[i + 1] else: sumlist[i] += arr[i] if sumlist[i] % 2 == 0: output += odd even += 1 else: output += 1 output += even odd += 1 output %= 10**9 + 7 return output
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: s = 0 evens, odds = 0, 0 cnt = 0 for num in arr: s += num if s % 2 == 1: cnt += evens + 1 odds += 1 else: cnt += odds evens += 1 cnt = cnt % (10**9 + 7) return cnt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, A): count = [1, 0] curr = res = 0 for a in A: curr ^= a & 1 res += count[1 - curr] count[curr] += 1 return res % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: if not arr or len(arr) == 0: return 0 even = 1 odd = 0 cur = 0 ret = 0 for a in arr: cur = a + cur if cur % 2 == 0: even += 1 ret += odd else: odd += 1 ret += even ret = ret % (10**9 + 7) return ret
CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: ans = 0 evenCount = 0 oddCount = 0 for i in arr: if i % 2 == 0: ans += oddCount oddCount, evenCount = oddCount, evenCount + 1 else: ans += evenCount + 1 oddCount, evenCount = evenCount + 1, oddCount return int(ans % (math.pow(10, 9) + 7))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: N = len(arr) if N == 0: return 0 elif N == 1: return abs(arr[0]) % 2 s = 0 tot = 0 ct_odd = 0 ct_even = 0 for i in range(N): s += arr[i] if s % 2 == 1: tot += 1 + ct_even ct_odd += 1 else: tot += ct_odd ct_even += 1 return tot % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: ans = 0 even = 0 odd = 0 for v in arr: if v % 2 == 0: even, odd = (even + 1) % 10000000007, odd else: even, odd = odd, (even + 1) % 10000000007 ans = (ans + odd) % 10000000007 return ans % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, A: List[int]) -> int: n = len(A) mod = 10**9 + 7 ans = 0 p, ctr = [0] * (n + 1), Counter([0]) for i in range(n): p[i] = p[i - 1] + A[i] s = p[i] % 2 ans = ans + ctr[1 - s] ans = ans % mod ctr[s] += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: even = [0] * len(arr) odd = [0] * len(arr) for i in range(len(arr)): if i == 0 and arr[i] % 2 == 0: even[i] = 1 elif i == 0: odd[i] = 1 elif arr[i] % 2 == 0: even[i] = even[i - 1] + 1 odd[i] = odd[i - 1] else: even[i] = odd[i - 1] odd[i] = even[i - 1] + 1 mod = 10**9 + 7 ans = 0 for i in range(len(arr)): ans = (ans + odd[i]) % mod return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: dp = [] for i in range(len(arr) + 1): dp.append([0, 0]) ans = 0 for i in range(1, len(arr) + 1): if arr[i - 1] % 2 == 0: dp[i][0] = dp[i - 1][0] + 1 dp[i][1] = dp[i - 1][1] else: dp[i][0] = dp[i - 1][1] dp[i][1] = dp[i - 1][0] + 1 ans += dp[i][1] return ans % (pow(10, 9) + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: s, d, ans = 0, 0, 0 for n in arr: if n % 2: s, d = d, s s += 1 else: d += 1 ans += s return ans % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: MOD = 10**9 + 7 res = 0 curr_sum = 0 even_count = 1 odd_count = 0 for num in arr: curr_sum += num curr_sum %= 2 if curr_sum == 1: res += even_count odd_count += 1 else: res += odd_count even_count += 1 return res % MOD
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: odd = [(0) for x in arr] even = [(0) for x in arr] odd[0] = 1 if arr[0] % 2 == 1 else 0 even[0] = 1 if arr[0] % 2 == 0 else 0 for i in range(1, len(arr)): if arr[i] % 2 == 0: odd[i] = odd[i - 1] even[i] = even[i - 1] + 1 else: odd[i] = even[i - 1] + 1 even[i] = odd[i - 1] return sum(odd) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: N = len(arr) dp = [0] * N dp[0] = arr[0] & 1 for i in range(1, N): if arr[i] & 1: dp[i] = i - dp[i - 1] + 1 else: dp[i] = dp[i - 1] return sum(dp) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: run, prev = [], 0 count = 0 odd, even = 0, 0 for ele in arr: run.append(prev + ele) prev = run[-1] if run[-1] % 2 != 0: count += even + 1 odd += 1 else: count += odd even += 1 return count % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: n = len(arr) MOD = int(1000000000.0 + 7) res = even = odd = 0 for a in arr: even += 1 if a % 2: even, odd = odd, even res = (res + odd) % MOD return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: dp = [(-1) for i in range(len(arr))] dp[0] = [(arr[0] + 1) % 2, arr[0] % 2] total = dp[0][1] for k in range(1, len(arr)): if arr[k] % 2 == 0: dp[k] = [(dp[k - 1][0] + 1) % (10**9 + 7), dp[k - 1][1] % (10**9 + 7)] total += dp[k][1] total = total % (10**9 + 7) else: dp[k] = [dp[k - 1][1] % (10**9 + 7), (dp[k - 1][0] + 1) % (10**9 + 7)] total += dp[k][1] total = total % (10**9 + 7) return total % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER LIST BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
MOD = 1000000007 class Solution: def numOfSubarrays(self, arr): n = len(arr) pre_sum = [1, 0] now_sum = 0 res = 0 for i in range(n): now_sum += arr[i] if now_sum % 2 == 1: res += pre_sum[0] pre_sum[1] += 1 else: res += pre_sum[1] pre_sum[0] += 1 return res % MOD
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: c = collections.defaultdict(int) s = 0 c[0] += 1 res = 0 mod = 10**9 + 7 for x in arr: s ^= x % 2 res += c[1 - s] res %= mod c[s] += 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, A) -> int: n = len(A) MOD = pow(10, 9) + 7 dp_even, dp_odd = [0], [0] if A[0] % 2: dp_odd[0] += 1 else: dp_even[0] += 1 ans = dp_odd[-1] for i in range(1, n): if A[i] % 2: dp_odd.append((dp_even[i - 1] + 1) % MOD) dp_even.append(dp_odd[i - 1]) else: dp_odd.append(dp_odd[i - 1]) dp_even.append((dp_even[i - 1] + 1) % MOD) ans += dp_odd[i] ans %= MOD return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER LIST NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: sum_even = 0 sum_odd = 0 out = 0 for i in range(len(arr)): if arr[i] % 2 == 0: sum_even, sum_odd = sum_even + 1, sum_odd else: sum_even, sum_odd = sum_odd, sum_even + 1 out = out + sum_odd return out % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: acc = [] temp = 0 ones = 0 for u in arr: temp += u % 2 if temp % 2 == 1: ones += 1 L = len(arr) return ones * (L - ones + 1) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, A): MOD = 10**9 + 7 ans = 0 even = odd = 0 for x in A: if x % 2: odd, even = 1 + even, odd else: even += 1 ans = (ans + odd) % MOD return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: ans = 0 seen_odd, seen_even = set(), set() odd_count, even_count = 0, 1 psum = 0 mod = 10**9 + 7 for a in arr: psum += a if psum % 2 == 0: ans = (ans + odd_count) % mod seen_even.add(psum) even_count += 1 else: ans = (ans + even_count) % mod seen_odd.add(psum) odd_count += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: limit = 10**9 + 7 final = 0 dp_even = [0] * (len(arr) + 1) dp_odd = [0] * (len(arr) + 1) for i in range(len(arr)): num = arr[i] if num % 2 == 0: odd_cur = dp_odd[i] even_cur = 1 + dp_even[i] final += odd_cur dp_even[i + 1] = even_cur dp_odd[i + 1] = odd_cur else: odd_cur = dp_even[i] + 1 even_cur = dp_odd[i] final += odd_cur dp_odd[i + 1] = odd_cur dp_even[i + 1] = even_cur return final % limit
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: dp = [0, 0] ans = 0 for i, num in enumerate(arr): dp[0], dp[1] = dp[num % 2] + num % 2, dp[(num - 1) % 2] + (num - 1) % 2 ans += dp[0] return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: o, e = 0, 0 res = 0 for n in arr: if n % 2 == 0: e += 1 else: o, e = e, o o += 1 res += o res = res % (10**9 + 7) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: memoOdd = arr[0] % 2 memoEven = -(arr[0] % 2 - 1) memoOddSum = memoOdd for i in range(1, len(arr)): memoOdd_temp = memoOdd memoEven_temp = memoEven memoOdd = ( memoOdd_temp * -(arr[i] % 2 - 1) + memoEven_temp * (arr[i] % 2) + arr[i] % 2 ) memoEven = ( memoOdd_temp * (arr[i] % 2) + memoEven_temp * -(arr[i] % 2 - 1) - (arr[i] % 2 - 1) ) memoOddSum += memoOdd return memoOddSum % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: odd = [0] even = [0] for i in range(len(arr)): if arr[i] % 2 == 0: even.append(even[-1] + 1) odd.append(odd[-1]) else: even.append(odd[-1]) odd.append(even[-2] + 1) return sum(odd) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: A = [(i % 2) for i in arr] n = len(A) lst = [[0, 0]] lst[0][A[0] & 1] = 1 for i in range(1, n): if A[i]: lst.append([lst[-1][1], 1 + lst[-1][0]]) else: lst.append([1 + lst[-1][0], lst[-1][1]]) return sum([x[1] for x in lst]) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: np = [[(0) for i in range(len(arr))] for j in range(2)] np[0][0] = 1 if arr[0] % 2 == 0 else 0 np[1][0] = 1 if arr[0] % 2 == 1 else 0 res = np[1][0] for i in range(1, len(arr)): if arr[i] % 2 == 0: np[0][i] = (1 + np[0][i - 1]) % 1000000007 np[1][i] = np[1][i - 1] % 1000000007 else: np[0][i] = np[1][i - 1] % 1000000007 np[1][i] = (1 + np[0][i - 1]) % 1000000007 res += np[1][i] return res % 1000000007
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR RETURN BIN_OP VAR NUMBER VAR
Given an array of integers arr. Return the number of sub-arrays with odd sum. As the answer may grow large, the answer must be computed modulo 10^9 + 7.   Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All sub-arrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All sub-arrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have even sum and the answer is 0. Example 3: Input: arr = [1,2,3,4,5,6,7] Output: 16 Example 4: Input: arr = [100,100,99,99] Output: 4 Example 5: Input: arr = [7] Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 100
def accumulate(arr): acc = 0 for elem in arr: acc = (acc + elem) % 2 yield acc class Solution: def numOfSubarrays(self, arr: List[int]) -> int: parity_array = (x % 2 for x in arr) cumulative_array = accumulate(parity_array) prev = [1, 0] ans = 0 for elem in cumulative_array: ans += [prev[1], prev[0]][elem] prev[elem] += 1 return ans % (10**9 + 7)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR LIST VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR