description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): for i in range(n): arr1[i] -= arr2[i] m = dict() m[0] = -1 sum = 0 ans = 0 for i in range(n): sum += arr1[i] if sum in m: ans = max(ans, i - m[sum]) ...
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): temp = [] for i in range(len(arr1)): temp.append(arr1[i] - arr2[i]) pre_sum = 0 x = {} count = 0 for i in range(len(temp)): pre_sum += temp[i] if pre_sum == 0: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR V...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): for i in range(len(arr1)): arr1[i] = arr1[i] - arr2[i] ans, sum = 0, 0 d = {(0): -1} k = 0 for i in range(len(arr1)): sum += arr1[i] if sum - k in d: ans = max(ans,...
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VA...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): maxLen = 0 presum1 = presum2 = 0 diff = [-1] * (2 * n + 1) for i in range(n): presum1 += 1 if arr1[i] == True else 0 presum2 += 1 if arr2[i] == True else 0 curr_diff = presum1 - presum2 ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR N...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): diff1 = diff2 = 0 maxlen = 0 diff = {} for i in range(n): diff1 += arr1[i] diff2 += arr2[i] curr_diff = diff1 - diff2 if curr_diff == 0: maxlen = i + 1 ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): for i in range(n): arr1[i] = arr1[i] - arr2[i] p = 0 hash = {} ans = 0 for i, el in enumerate(arr1): p += el if p == 0: ans = i + 1 if p in hash: ...
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): maxlen = 0 total1 = 0 total2 = 0 d = {} for i in range(n): total1 += arr1[i] total2 += arr2[i] currdiff = total1 - total2 if currdiff == 0: maxlen = i +...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIS...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): d = {} psum1 = psum2 = 0 longest = 0 for i in range(n): psum1 += arr1[i] psum2 += arr2[i] diff = psum1 - psum2 if diff == 0: longest = i + 1 if diff...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): for i in range(n): if arr2[i] == 1: arr2[i] = -1 sum1 = 0 max1 = 0 d = {} for i in range(n): sum1 += arr1[i] + arr2[i] if sum1 == 0: if i + 1 > max1...
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): arr = [] for i in range(len(arr1)): arr.append(arr1[i] - arr2[i]) sm = 0 dct = {(0): -1} res = 0 for i in range(len(arr)): sm += arr[i] if sm in dct: res = ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN ...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): minidx = dict() minidx[0] = -1 ans = 0 psum = 0 for i in range(n): psum += arr1[i] - arr2[i] if psum in minidx: ans = max(ans, i - minidx[psum]) else: ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): diff_arr = [0] * n for i in range(n): diff_arr[i] = arr1[i] - arr2[i] d = {} prefix_sum = 0 largest = 0 for i in range(n): prefix_sum += diff_arr[i] if prefix_sum == 0: ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR ...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): for i in range(len(arr1)): arr1[i] = arr1[i] - arr2[i] PFS = [0] s = 0 dic = {(0): -1} ans = 0 for i in range(len(arr1)): s = s + arr1[i] if s not in dic: d...
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_O...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): temp = [(i - j) for i, j in zip(arr1, arr2)] presum = 0 d = {(0): -1} maxlen = 0 for i in range(len(temp)): presum = presum + temp[i] if presum not in d: d[presum] = i ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): n = len(arr1) diff_arr = [(arr1[i] - arr2[i]) for i in range(n)] for i in range(1, n): diff_arr[i] += diff_arr[i - 1] max_len = 0 hm = {} for i in range(n): psum = diff_arr[i] ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR AS...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): d = {} maxi = 0 pre1 = 0 pre2 = 0 for i in range(n): pre1 += arr1[i] pre2 += arr2[i] diff = pre1 - pre2 if diff == 0: maxi = max(maxi, i + 1) ...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, a, b, n): d = {(0): -1} res = s = 0 for i, (x, y) in enumerate(zip(a, b)): s += x + (-y if y else 0) if s not in d: d[s] = i res = max(res, i - d[s]) return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): d = {(0): -1} ls = s1 = s2 = 0 for i in range(n): s1 += arr1[i] s2 += arr2[i] c = s1 - s2 if c in d: ls = max(ls, i - d[c]) else: d[c] = i ...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): d = {} cursum = 0 maxlen = 0 for i in range(n): cursum += arr1[i] - arr2[i] if arr1[i] - arr2[i] == 0 and maxlen == 0: m = 1 if cursum == 0: maxlen = i + 1 ...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): arr = [0] * n for i in range(n): arr[i] = arr1[i] - arr2[i] d = {(0): -1} ans = 0 sm = 0 for i in range(n): sm += arr[i] if sm in d: ans = max(ans, i - d[sm...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): arr = [0] * n for i in range(n): arr[i] = arr1[i] - arr2[i] max_span = 0 ps = 0 ps_dict = {} for i in range(n): ps += arr[i] if ps == 0: max_span = max(max_...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR V...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): diff = [(arr1[i] - arr2[i]) for i in range(n)] d, end_idx = {}, -1 tot = max_len = 0 for i in range(n): tot += diff[i] if tot == 0: max_len, end_idx = i + 1, i if d.get(tot...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR DICT NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NONE NONE IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR V...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): diff = [(-1) for i in range(2 * n + 1)] s1 = 0 s2 = 0 mle = 0 for i in range(n): s1 += arr1[i] s2 += arr2[i] d = s1 - s2 index = n + d if d == 0: ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_O...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): count, ans = 0, 0 dic = {} arr = [] for i, j in zip(arr1, arr2): arr.append(i - j) for i in range(n): count += arr[i] if count == 0: ans = i + 1 elif co...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RE...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): arr3 = [] sum = 0 mx = 0 mp = {(0): -1} zip_object = zip(arr1, arr2) for arr1, arr2 in zip_object: arr3.append(arr1 - arr2) for i in range(n): sum += arr3[i] if sum...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): temp = [] for i in range(n): temp.append(arr1[i] - arr2[i]) ans = 0 csum = 0 d = {} for i in range(len(arr1)): csum += temp[i] if csum == 0: ans = i + 1 ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR B...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): for i in range(n): arr1[i] = arr1[i] - arr2[i] dic = {(0): -1} m = 0 s = 0 for i in range(n): s += arr1[i] if s not in dic: dic[s] = i else: ...
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): for i in range(1, n): arr1[i] += arr1[i - 1] arr2[i] += arr2[i - 1] lst = [(arr1[i] - arr2[i]) for i in range(n)] rec = {(0): -1} ans = 0 for i in range(n): if lst[i] in rec: ...
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR ...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): d = {} cursum = 0 mlen = 0 temp = [0] * n for i in range(n): temp[i] = arr1[i] - arr2[i] for i in range(n): cursum += temp[i] if cursum == 0: mlen = i + 1 ...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): counter = 0 max_len = 0 seen_count = {(0): -1} for i in range(len(arr1)): if arr1[i] == 0 and arr2[i] == 1: counter -= 1 elif arr1[i] == 1 and arr2[i] == 0: counter += ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): mx = 0 mp = dict() mp[0] = -1 sum = 0 for i in range(n): arr1[i] = arr1[i] - arr2[i] sum += arr1[i] if sum in mp: mx = max(mx, i - mp[sum]) else: ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): arr = [] for i in range(n): arr.append(arr1[i] - arr2[i]) sm = 0 count = 0 mx = 0 d = dict() d[0] = -1 for i in range(n): sm += arr[i] if sm in d: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIG...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): a = {(0): 0} s1 = 0 s2 = 0 ans = 0 for i in range(n): s1 += arr1[i] s2 += arr2[i] if s1 - s2 == 0: ans = i + 1 elif s1 - s2 in a: ans = ...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): diffArr = [(arr1[i] - arr2[i]) for i in range(len(arr1))] j = 0 prefSum = 0 maxLen = 0 st = {} while j < len(arr1): prefSum += diffArr[j] if prefSum == 0: maxLen = max(...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR B...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): total1 = total2 = 0 hmap = {(0): -1} ans = 0 for i in range(n): total1 += arr1[i] total2 += arr2[i] if total1 - total2 in hmap: ans = max(ans, i - hmap[total1 - total2]) ...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, a1, a2, n): s = [] s1 = 0 s2 = 0 df = {(0): -1} dl = {} ans = 0 for i in range(n): s1 += a1[i] s2 += a2[i] if s1 == s2: ans = 1 s.append(s1 - s2) ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): arr = [] for i in range(n): arr.append(arr1[i] - arr2[i]) dic = {} s = 0 for i in range(n): s = s + arr[i] arr[i] = s for i in arr: dic[i] = -1 for i in...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASS...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): arr1[0] = arr1[0] - arr2[0] dic = {(0): 0} for i in range(1, n): arr1[i] = arr1[i] - arr2[i] arr1[i] = arr1[i] + arr1[i - 1] c = 0 for i in range(n): if arr1[i] == 0: ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): count = 0 mxVal = 0 s1 = 0 s2 = 0 dif = {} for i in range(n): s1 = s1 + arr1[i] s2 = s2 + arr2[i] diff = s1 - s2 if diff == 0: mxVal = i + 1 ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VA...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): arr3 = [] pre_sum = 0 res = 0 length = 0 m = {} for i in range(n): arr3.append(arr1[i] - arr2[i]) for i in range(n): pre_sum += arr3[i] if pre_sum == 0: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, A, B, n): sumA = [0] sumB = [0] length = len(A) ans = 0 for i in range(length): sumA.append(sumA[-1] + A[i]) sumB.append(sumB[-1] + B[i]) diff = [(sumAi - sumBi) for sumAi, sumBi in zip(sumA, sumB)] ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CAL...
Given two binary arrays arr1[] and arr2[] of same size N. Find length of the longest common span [i, j] where j>=i such that arr1[i] + arr1[i+1] + …. + arr1[j] = arr2[i] + arr2[i+1] + …. + arr2[j]. Example 1: Input: N = 6 Arr1[] = {0, 1, 0, 0, 0, 0} Arr2[] = {1, 0, 1, 0, 0, 1} Output: 4 Explanation: The longest span...
class Solution: def longestCommonSum(self, arr1, arr2, n): pre1 = 0 pre2 = 0 diff = {} diff[0] = -1 ma = 0 for i in range(n): pre1 += arr1[i] pre2 += arr2[i] if pre1 - pre2 not in diff: diff[pre1 - pre2] = i ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, K): hashTable = {(0): -1} _sum = 0 ans = 0 for i in range(n): _sum += arr[i] modulus = _sum % K if modulus in hashTable: ans = max(ans, i - hashTable[modulus]) el...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, K): mp = dict() mp[0] = -1 Sum = 0 max_len = 0 for i in range(n): Sum += arr[i] rem = Sum % K if rem < 0: rem += K if rem not in mp: mp[re...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, K): prefix = [0] for i in range(n): prefix.append(prefix[-1] + arr[i]) res = 0 remain = {} for i in range(n + 1): remai = prefix[i] % K if remai not in remain: remain...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, K): total = 0 maximum = 0 dict1 = dict() for i in range(n): total += arr[i] if total % K == 0: maximum = max(maximum, i + 1) if total % K in dict1: maximum = ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, K): d = {} curr = 0 res = 0 for i in range(n): curr += arr[i] curr %= K if curr == 0: res = max(res, i + 1) elif curr in d: res = max(res, i - d[c...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): d = {} s = 0 ans = 0 d[0] = -1 for i in range(n): s += arr[i] if d.get(s % k, -2) == -2: d[s % k] = i else: ans = max(ans, i - d[s % k]) r...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): arr = [(i % k) for i in arr] a = [0] for i in arr: a.append((a[-1] + i) % k) m = 0 dic = {} for i in range(len(a)): if a[i] in dic: m = max(m, i - dic[a[i]]) ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): pre = [0] for i in range(n): pre.append((pre[-1] + arr[i]) % k) a = [-1] * k ans = 0 for i in range(n + 1): if a[pre[i]] == -1: a[pre[i]] = i else: ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN ...
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, K): if K == 1: return n a = [] a.append(arr[0] % K) for i in range(1, n): a.append((a[i - 1] + arr[i]) % K) d = {} maxi = -999 for i in range(n): if a[i] == 0: ...
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR N...
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, K): c, lon = 0, 0 dic = {} for i in range(n): c += arr[i] if c % K == 0: lon = max(i + 1, lon) if c % K in dic: lon = max(i + 1 - dic[c % K], lon) if c no...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_...
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): d = [0] * n cu = 0 for i in range(n): cu = cu + arr[i] d[i] = cu m = {} ans = 0 for i in range(len(d)): q = d[i] % k if q in m: ans = max(ans,...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR ...
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, K): x = [float("inf")] * K x[0] = -1 output = 0 cur = 0 for i, num in enumerate(arr): cur += num temp = cur % K if x[temp] == float("inf"): x[temp] = i el...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): pfSum = [] a = 0 for i in range(n): a += arr[i] pfSum.append(a) dic = {} ans = 0 for i in range(n): val = pfSum[i] if val % k == 0: ans = max(...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASS...
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): prefix_sum = [0] * (n + 1) for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + arr[i - 1] max_len = 0 mod_map = {} for i in range(n + 1): mod = prefix_sum[i] % k if mod...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR ...
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, K): max_l = 0 mod_arr = [0] * n cur_sum = 0 maps = {} for i in range(n): cur_sum += arr[i] mod_arr[i] = (cur_sum % K + K) % K if mod_arr[i] == 0: max_l = i + 1 ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR...
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, K): d = {(0): -1} ans = 0 s = 0 k = K for i in range(n): s = s + arr[i] r = s % k if r not in d: d[r] = i else: ans = max(ans, i - d[r]) ...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): d = {} sum = 0 mx = 0 rem = 0 for i in range(len(arr)): sum += arr[i] rem = sum % k if rem == 0: mx = max(mx, i + 1) if rem < 0: rem +...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR A...
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): prerem = [0] * n res = 0 count, maxlen = 0, 0 for i in range(n): res += arr[i] prerem[i] = res % k d = {} d[0] = -1 for i in range(n): if prerem[i] not in d: ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FU...
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): um = {} max_len = 0 curr_sum = 0 for i in range(n): curr_sum += arr[i] mod = (curr_sum % k + k) % k if mod == 0: max_len = i + 1 elif mod in um.keys(): ...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): um = {} mod_arr = [(0) for i in range(n)] max_len = 0 curr_sum = 0 for i in range(n): curr_sum += arr[i] mod_arr[i] = curr_sum % k if mod_arr[i] == 0: max_len = i...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ...
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, K): sumi = 0 d = {(0): 0} maxi = 0 for i in range(len(arr)): sumi = sumi + arr[i] remainder = sumi % K if remainder in d: re = i + 1 - d[remainder] maxi = max...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): if n == 1: return 1 if arr[0] % k == 0 else 0 mp = dict() ans = 0 sm = 0 for i in range(n): sm += arr[i] mod = (sm % k + k) % k if mod == 0: ans = i +...
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR...
Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K. Example 1: Input: A[] = {2, 7, 6, 1, 4, 5} K = 3 Output: 4 Explanation:The subarray is {7, 6, 1, 4} with sum 18, which is divisible by 3. Example 2: Input: A[]...
class Solution: def longSubarrWthSumDivByK(self, arr, n, k): if k == 1: return n p = [] r = [] p.append(arr[0]) r.append(arr[0] % k) for i in range(1, len(arr)): p.append(p[i - 1] + arr[i]) r.append(p[i] % k) d = {} ...
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBE...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: size = len(cardPoints) - k minSum = float("inf") cur = 0 left = 0 for i, v in enumerate(cardPoints): cur += v if i - left + 1 > size: cur -= cardPoints[left] ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP F...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: left, right = [0], [0] for i in range(k): left.append(left[-1] + cardPoints[i]) right.append(right[-1] + cardPoints[len(cardPoints) - 1 - i]) res = 0 for i in range(k + 1): ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP ...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) pre = [0] * (n + 1) for i in range(n): pre[i + 1] = pre[i] + cardPoints[i] max_val = -1 for i in range(k + 1): max_val = max(max_val, pre[i] + pre[n] - pre[n...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: right_index = len(cardPoints) - k curr_max = sum(cardPoints[right_index:]) curr_sum = curr_max for left_index in range(0, k): curr_sum -= cardPoints[right_index] right_index += 1 ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: s = sum(cardPoints[:k]) res = s for i in range(1, k + 1): s += cardPoints[-i] - cardPoints[k - i] res = max(res, s) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) cum_sum = [(0) for i in range(n)] cum_sum[0] = cardPoints[0] rev_sum = [(0) for i in range(n)] rev_sum[0] = cardPoints[-1] for i in range(1, n): cum_sum[i] = cum...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER 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 ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR ...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: forwardSum = [m for m in cardPoints] backwardSum = cardPoints.copy() backwardSum.append(0) for c in range(1, len(cardPoints)): forwardSum[c] = forwardSum[c - 1] + forwardSum[c] for l in ran...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: remainCnt = len(cardPoints) - k if remainCnt == 0: return sum(cardPoints) minRemainSum = float("inf") curr = 0 cnt = 0 for i in range(len(cardPoints)): cnt += 1 ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VA...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: j = len(cardPoints) - 1 ms = 0 for i in range(k): ms += cardPoints[j] j -= 1 cand = ms for i in range(k): cand += cardPoints[i] - cardPoints[j + 1] j += ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: pre = [cardPoints[0]] n = len(cardPoints) for i in range(1, n): pre.append(pre[-1] + cardPoints[i]) if k == n: return pre[-1] s = pre[-1] cur_s = pre[n - k - 1] ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR RETURN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VA...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: total = sum(cardPoints) nk = len(cardPoints) - k if nk == 0: return total current = sum(cardPoints[0:nk]) max_score = current for i in range(1, k + 1): current = current...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VA...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, A: List[int], k: int) -> int: su = sum(A[:k]) n = len(A) res = su for i in range(k): su -= A[k - i - 1] su += A[n - i - 1] res = max(res, su) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: score = 0 l = k prefix = [0] for i in range(len(cardPoints)): prefix.append(prefix[-1] + cardPoints[i]) for i in range(k + 1): score = max(score, prefix[l] + prefix[-1] - prefix...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR VA...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: left = [0] * (k + 1) right = [0] * (k + 1) for i in range(k): left[i + 1] = left[i] + cardPoints[i] right[i + 1] = right[i] + cardPoints[-i - 1] return max(left[j] + right[k - j] for j ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VA...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) - k current = 0 for i in range(n): current += cardPoints[i] result = current for i in range(1, k + 1): current -= cardPoints[i - 1] current += ca...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: answer = 0 left = [0] * len(cardPoints) right = [0] * len(cardPoints) for i in range(len(cardPoints)): if i == 0: left[0] = cardPoints[0] else: left[i] =...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER 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 ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL V...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: totalPoints, window = sum(cardPoints), len(cardPoints) - k i, j, Sum, Min = 0, 0, 0, totalPoints while j < len(cardPoints): Sum += cardPoints[j] if j - i + 1 > window: Sum -= ca...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: f, b = [0], [0] for n in cardPoints: f.append(f[-1] + n) for n in cardPoints[::-1]: b.append(b[-1] + n) allCombo = [(f[i] + b[k - i]) for i in range(k + 1)] return max(allCombo)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST NUMBER LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: if not cardPoints or k == 0: return 0 for i in range(1, len(cardPoints)): cardPoints[i] += cardPoints[i - 1] if k == len(cardPoints): return cardPoints[-1] ans = cardPoints[...
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_O...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: ans = 0 curSum = 0 n = len(cardPoints) for i in range(n - k, n + k): curSum += cardPoints[i % n] if i >= n: curSum -= cardPoints[(i - k) % n] ans = max(ans, ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: cardLen = len(cardPoints) frontSum = [0] for num in cardPoints: frontSum.append(frontSum[-1] + num) backSum = [(0) for _ in range(cardLen + 1)] for i in range(cardLen - 1, -1, -1): ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR V...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: if not cardPoints or len(cardPoints) == 0: return 0 window = len(cardPoints) - k res = float("inf") s = 0 for i in range(window): s += cardPoints[i] res = min(s, res) ...
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: ans = total = sum(cardPoints[:k]) for i in range(1, k + 1): total -= cardPoints[k - i] total += cardPoints[-1 - i + 1] ans = max(ans, total) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: min_len = len(cardPoints) - k curr_sum = 0 min_val = 0 for start in range(len(cardPoints) - min_len + 1): if start == 0: curr_sum = sum(cardPoints[start : start + min_len]) ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VA...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) - k min = 0 window = 0 all = 0 for i in range(n): window += cardPoints[i] all += cardPoints[i] min = window for x in range(k): al...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VA...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, l: List[int], k: int) -> int: length = len(l) if k == length: return sum(l) elif k == 0: return 0 k = length - k v = curr_min = sum(l[:k]) for i in range(k, length): v = v - l[i - k] + l[i] ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP ...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: max_score = 0 curr_score = 0 init_hand = cardPoints[len(cardPoints) - k :] max_score = sum(init_hand) curr_score = max_score for i in range(k): curr_score -= init_hand[i] ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: length = len(cardPoints) total = sum(cardPoints) if k == length: return total curr = 0 temp = 2**31 - 1 left = 0 for right in range(length): curr += cardPoints[r...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints, k: int) -> int: N = len(cardPoints) preS, afterS = [0] * (N + 1), [0] * (N + 1) ans = 0 for i in range(1, N + 1): preS[i] = preS[i - 1] + cardPoints[i - 1] for j in range(1, N + 1): afterS[j] = afterS[j -...
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER A...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) - k l, r, res, count = 0, 0, sum(cardPoints[:n]), 0 while r < len(cardPoints): count += cardPoints[r] if r >= n: count -= cardPoints[l] l += ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: if len(cardPoints) < k: return 0 if len(cardPoints) == k: return sum(cardPoints) n = len(cardPoints) res, cur = sum(cardPoints[:k]), sum(cardPoints[:k]) for i in range(k): ...
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VA...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, p: List[int], k: int) -> int: s = sum(p) if k == len(p): return s n = len(p) for i in range(1, len(p)): p[i] += p[i - 1] return s - min(p[n + i - k - 1] - (p[i - 1] if i else 0) for i in range(k + 1))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VA...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) sums = [0] * (n + 1) for i in range(1, n + 1): sums[i] = sums[i - 1] + cardPoints[i - 1] ans = float("inf") for i in range(k + 1): ans = min(ans, sums[i + n ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VA...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: total = [(0) for _ in range(len(cardPoints))] total[0] = cardPoints[0] for i in range(1, len(cardPoints)): total[i] = total[i - 1] + cardPoints[i] max_sum = 0 for i in range(k + 1): ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_O...
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have ...
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) runningsum = 0 start = end = 0 total = sum(cardPoints) minsum = float("inf") while end < len(cardPoints): runningsum += cardPoints[end] if end - star...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR AS...