description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. Example 1: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 Output: true Example 2: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 Output: false
class Solution: def findRow(self, matrix, target, l, h): if l > h: return -1 elif l == h: return h mid = l + int((h - l) / 2) if matrix[mid][0] <= target <= matrix[mid][-1]: return mid elif matrix[l][0] <= target < matrix[mid][0]: return self.findRow(matrix, target, l, mid - 1) else: return self.findRow(matrix, target, mid + 1, h) def bSearch(self, arr, target, l, h): if l > h: return -1 mid = l + int((h - l) / 2) if arr[mid] == target: return mid elif arr[l] <= target < arr[mid]: return self.bSearch(arr, target, l, mid - 1) else: return self.bSearch(arr, target, mid + 1, h) def searchMatrix(self, matrix, target): rows = len(matrix) if rows == 0: return False cols = len(matrix[0]) inRow = self.findRow(matrix, target, 0, rows - 1) print("row present in %s" % inRow) arr = matrix[inRow] index = self.bSearch(arr, target, 0, cols - 1) print("present at index %s" % index) if index != -1: return True return False
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. Example 1: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 Output: true Example 2: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 Output: false
class Solution: def searchMatrix(self, matrix, target): if not matrix or len(matrix) == 0: return False row = len(matrix) col = len(matrix[0]) if col == 0: return False l, r = 0, row * col - 1 while l <= r: m = l + r >> 1 i, j = divmod(m, col) if matrix[i][j] == target: return True elif target < matrix[i][j]: r = m - 1 else: l = m + 1 return False
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. Example 1: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 Output: true Example 2: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 Output: false
class Solution: def searchMatrix(self, matrix, target): print(len(matrix)) if len(matrix) == 0: return False if len(matrix) == 1 and len(matrix[0]) == 0: return False existFlag = 0 last = len(matrix[0]) - 1 for i in range(len(matrix)): if matrix[i][last] < target: continue else: print("falg set to 1") existFlag = 1 break if existFlag == 1: print("yes it is 1") for j in range(last, -1, -1): print(("j= ", j)) if matrix[i][j] == target: return True return False
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. Example 1: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 Output: true Example 2: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 Output: false
class Solution: def searchMatrix(self, matrix, target): if not matrix: return False row_size = len(matrix) column_size = len(matrix[0]) low = 0 high = row_size * column_size - 1 while low <= high: mid = (high + low) // 2 r, c = divmod(mid, column_size) val = matrix[r][c] if target == val: return True elif target > val: low = mid + 1 else: high = mid - 1 return False
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. Example 1: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 Output: true Example 2: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 Output: false
class Solution: def searchMatrix(self, matrix, target): if not matrix or target is None: return False n = len(matrix[0]) lo, hi = 0, len(matrix) * n while lo < hi: mid = (lo + hi) / 2 x = matrix[int(mid / n)][int(mid % n)] if x < target: lo = mid + 1 elif x > target: hi = mid else: return True return False
CLASS_DEF FUNC_DEF IF VAR VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. Example 1: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 Output: true Example 2: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 Output: false
class Solution: def searchMatrix(self, matrix, target): def get(now, n): x = now // n y = now % n return matrix[x][y] m = len(matrix) if m == 0: return False n = len(matrix[0]) if n == 0: return False i = 0 j = m * n - 1 while i <= j: mid = (i + j) // 2 if get(mid, n) == target: return True elif get(mid, n) < target: i = mid + 1 else: j = mid - 1 return False
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. Example 1: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 Output: true Example 2: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 Output: false
class Solution: def searchMatrix(self, matrix, target): row = len(matrix) if row == 0: return False column = len(matrix[0]) if column == 0: return False i = 0 while i < row: if target <= matrix[i][column - 1]: for j in range(column - 1, -1, -1): if matrix[i][j] == target: return True else: pass return False else: i += 1 return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. Example 1: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 Output: true Example 2: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 Output: false
class Solution: def searchMatrix(self, matrix, target): i = 0 if len(matrix) == 0 or len(matrix[0]) == 0: return False while i < len(matrix): if matrix[i][0] > target: break i += 1 i -= 1 target_vector = matrix[i] l = 0 r = len(target_vector) - 1 while l <= r: mid = (l + r) // 2 print(target_vector[mid]) if target_vector[mid] == target: return True if target_vector[mid] < target: l = mid + 1 else: r = mid - 1 return False
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row. Example 1: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 Output: true Example 2: Input: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 Output: false
class Solution: def searchMatrix(self, matrix, target): if not matrix or not matrix[0]: return False i, j = 0, len(matrix[0]) - 1 while i < len(matrix) and j >= 0: if matrix[i][j] == target: return True elif matrix[i][j] > target: j -= 1 else: i += 1 return False
CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER
β€” Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? β€” Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees. Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai β€” some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly! The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart: * The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; * the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; * and of course, the walk should be successful: there must be at least two trees in the woodland belt left. Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart? Input The first line contains a single integer n β€” the initial number of trees in the woodland belt, 2 ≀ n. The second line contains space-separated integers ai β€” the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. * to get 30 points, you need to solve the problem with constraints: n ≀ 100 (subproblem A1); * to get 100 points, you need to solve the problem with constraints: n ≀ 3Β·105 (subproblems A1+A2). Output In the first line print two integers β€” the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k. In the next line print k integers β€” the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right. If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal. Examples Input 5 1 2 3 1 2 Output 8 1 1 Input 5 1 -2 3 1 -2 Output 5 2 2 5
def main(): n, aa = int(input()), list(map(int, input().split())) partialsum, s, d, ranges = [0] * n, 0, {}, [] for i, a in enumerate(aa): if a > 0: s += a partialsum[i] = s if a in d: d[a].append(i) else: d[a] = [i] ranges = [] for a, l in d.items(): lo, hi = l[0], l[-1] if lo < hi: ranges.append((partialsum[hi - 1] - partialsum[lo] + a * 2, lo, hi)) s, lo, hi = max(ranges) res = list(range(1, lo + 1)) for i in range(lo + 1, hi): if aa[i] < 0: res.append(i + 1) res.extend(range(hi + 2, n + 1)) print(s, len(res)) print(" ".join(map(str, res))) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER VAR NUMBER DICT LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to 23:59.
class Solution: def findMinDifference(self, timePoints): def cv2mnt(t): h, m = t.split(":") mnt = int(h) * 60 + int(m) return mnt lst = [(0) for i in range(1440)] for t in timePoints: mnt = cv2mnt(t) if lst[mnt] == 1: return 0 else: lst[mnt] = 1 beg = 0 stk = [i for i, n in enumerate(lst) if n == 1] stk.append(stk[0] + 1440) res = 1439 for i in range(len(stk) - 1): dif = abs(stk[i] - stk[i + 1]) dif_ = min(1440 - dif, dif) res = min(res, dif_) if res == 1: return 1 return res
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to 23:59.
class Solution: def findMinDifference(self, timePoints): def get_minutes(time_point): hour, minute = list(map(int, time_point.split(":"))) return 60 * hour + minute minutes = sorted(map(get_minutes, timePoints)) minutes.append(60 * 24 + minutes[0]) return min(y - x for x, y in zip(minutes, minutes[1:]))
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to 23:59.
class Solution: def findMinDifference(self, timePoints): if len(timePoints) > 1440: return 0 timeNum = [0] * len(timePoints) for i in range(len(timePoints)): timeNum[i] = 60 * int(timePoints[i][:2]) + int(timePoints[i][3:]) timeNum.sort() minMin = 24 * 60 for i in range(len(timeNum) - 1): minMin = min(minMin, timeNum[i + 1] - timeNum[i]) minMin = min(minMin, 24 * 60 + timeNum[0] - timeNum[-1]) return minMin
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to 23:59.
class Solution: def findMinDifference(self, timePoints): s = [0] * 1440 for c in timePoints: t = (int(c[0]) * 10 + int(c[1])) * 60 + int(c[3]) * 10 + int(c[4]) if s[t]: return 0 s[t] = 1 a = [] for i in range(1440): if s[i] == 1: a.append(i) m = a[0] + 1440 - a[-1] for i in range(1, len(a)): m = min(m, a[i] - a[i - 1]) return m
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to 23:59.
class Solution: def findMinDifference(self, timePoints): uniques = set() for i in range(len(timePoints)): timePoints[i] = int(timePoints[i][:2]) * 60 + int(timePoints[i][-2:]) if timePoints[i] in uniques: return 0 uniques.add(timePoints[i]) uniques.clear() timePoints.sort() minimum = timePoints[0] + 24 * 60 - timePoints[-1] for i in range(1, len(timePoints)): temp = timePoints[i] - timePoints[i - 1] if temp < minimum: minimum = temp return minimum
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to 23:59.
class Solution: def findMinDifference(self, timePoints): tlen = len(timePoints) if tlen < 2: return 0 arr = [0] * tlen for i in range(tlen): h, m = timePoints[i].split(":") arr[i] = int(h) * 60 + int(m) arr.sort() minx = float("inf") for i in range(1, tlen): if minx > arr[i] - arr[i - 1]: minx = arr[i] - arr[i - 1] if minx > arr[0] + 24 * 60 - arr[-1]: minx = arr[0] + 24 * 60 - arr[-1] return minx
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER VAR NUMBER RETURN VAR
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to 23:59.
class Solution: def findMinDifference(self, timePoints): for i in range(len(timePoints)): t = timePoints[i] h = "" m = "" deb = True for j in range(len(t)): if t[j] == ":": deb = False elif deb: h += t[j] else: m += t[j] timePoints[i] = int(m) + 60 * int(h) timePoints.sort() print(timePoints) mi = float("inf") for i in range(len(timePoints) - 1): diff = timePoints[i + 1] - timePoints[i] if diff < mi: mi = diff last = 24 * 60 - timePoints[-1] + timePoints[0] if last < mi: mi = last return mi
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to 23:59.
class Solution: def convertToInt(self, time): hours = int(time.split(":")[0]) mins = int(time.split(":")[1]) return 60 * hours + mins def findMinDifference(self, timePoints): s = set() for time in timePoints: mins = self.convertToInt(time) if mins in s: return 0 else: s.add(mins) result = 12 * 60 s = sorted(s) N = len(s) prev = s[N - 1] - 24 * 60 for i in range(N): if s[i] - prev < result: result = s[i] - prev prev = s[i] return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER RETURN BIN_OP BIN_OP NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to 23:59.
class Solution: def to_minutes(self, timePoints): ret_list = [] for hour in timePoints: h, m = tuple(map(int, hour.split(":"))) m += h * 60 ret_list.append(m) return ret_list def findMinDifference(self, timePoints): minutes_p = self.to_minutes(timePoints) minutes_p.sort() min_minutes = 1439 for i in range(len(minutes_p) - 1): if i == 0: min_minutes = min( abs(minutes_p[i] - minutes_p[i - 1] + 1440), min_minutes ) min_minutes = min( min( abs(minutes_p[i] - minutes_p[i + 1]), abs(1440 - abs(minutes_p[i] - minutes_p[i + 1])), ), min_minutes, ) print(min_minutes) return min_minutes
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to 23:59.
class Solution: def timetominute(self, time1): t1 = int(time1[:2]) * 60 + int(time1[3:5]) return t1 def findMinDifference(self, timePoints): count = 1440 for i in range(len(timePoints)): timePoints[i] = self.timetominute(timePoints[i]) timePoints.sort(reverse=False) print(timePoints) for i in range(len(timePoints)): if ( timePoints[(i + len(timePoints)) % len(timePoints) - 1] - timePoints[(i + 1 + len(timePoints)) % len(timePoints) - 1] < 0 ): count = min( min( timePoints[(i + 1 + len(timePoints)) % len(timePoints) - 1] - timePoints[(i + len(timePoints)) % len(timePoints) - 1], timePoints[(i + len(timePoints)) % len(timePoints) - 1] - timePoints[(i + 1 + len(timePoints)) % len(timePoints) - 1] + 1440, ), count, ) else: count = min( min( timePoints[(i + len(timePoints)) % len(timePoints) - 1] - timePoints[(i + 1 + len(timePoints)) % len(timePoints) - 1], timePoints[(i + 1 + len(timePoints)) % len(timePoints) - 1] - timePoints[(i + len(timePoints)) % len(timePoints) - 1] + 1440, ), count, ) return count
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR RETURN VAR
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to 23:59.
class Solution: def findMinDifference(self, timePoints): timePoints = sorted(map(lambda x: [int(n) for n in x.split(":")], timePoints)) timePoints.append([timePoints[0][0] + 24, timePoints[0][1]]) res = float("inf") for i in range(1, len(timePoints)): diff = (timePoints[i][0] - timePoints[i - 1][0]) * 60 + ( timePoints[i][1] - timePoints[i - 1][1] ) if diff > 720: diff = 1440 - diff res = min(res, diff) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): l = [] temp = head while temp: l.append(temp.data) temp = temp.next l.sort() k = DoublyLinkedList() for i in l: k.append(i) return k.head
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): temp = head res = [] while temp: res.append(temp.data) temp = temp.next res.sort() res1 = sorted(res, reverse=True) temp = ans = head i = 0 while temp: temp.data = res[i] i += 1 temp = temp.next return ans
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
import sys def Merge(head1, head2): if head1 == None: return head2 if head2 == None: return head1 if head1.data < head2.data: temp = Merge(head1.next, head2) head1.next = temp temp.prev = head1 return head1 else: temp = Merge(head1, head2.next) head2.next = temp temp.prev = head2 return head2 def sortDoubly(head): if head == None or head.next == None: return head pre = None slow = head fast = head while fast != None and fast.next != None: pre = slow slow = slow.next fast = fast.next.next pre.next = None slow.prev = None head1 = sortDoubly(head) head2 = sortDoubly(slow) return Merge(head1, head2) sys.setrecursionlimit(1000000) class Node: def __init__(self, data): self.data = data self.next = None self.prev = None class DoublyLinkedList: def __init__(self): self.head = None self.tail = None def append(self, new_data): new_node = Node(new_data) if self.head is None: self.head = new_node self.tail = new_node return new_node.prev = self.tail self.tail.next = new_node self.tail = new_node def printList(self, node): while node.next is not None: node = node.next while node.prev is not None: node = node.prev while node is not None: print(node.data, end=" ") node = node.next print() def printList(node): temp = node while node is not None: print(node.data, end=" ") temp = node node = node.next print() while temp: print(temp.data, end=" ") temp = temp.prev if __name__ == "__main__": t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().strip().split())) llist = DoublyLinkedList() for e in arr: llist.append(e) llist.head = sortDoubly(llist.head) printList(llist.head) print()
IMPORT FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF WHILE VAR NONE ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): def split(head): if head.next == None: return head sp = head fp = head while fp and fp.next: sp = sp.next fp = fp.next.next if fp: n = sp.next n.prev = None sp.next = None else: n = sp sp = sp.prev n.prev = None sp.next = None return head, n if head.next == None: return head l, r = split(head) ll = sortDoubly(l) rr = sortDoubly(r) ans = None while ll and rr: if ll.data <= rr.data: n = Node(ll.data) if ans: ans.next = n n.prev = ans ans = ans.next else: ans = n h1 = ans ll = ll.next else: n = Node(rr.data) if ans: ans.next = n n.prev = ans ans = ans.next else: ans = n h1 = ans rr = rr.next while ll: n = Node(ll.data) ans.next = n n.prev = ans ans = ans.next ll = ll.next while rr: n = Node(rr.data) ans.next = n n.prev = ans ans = ans.next rr = rr.next ans.next = None return h1
FUNC_DEF FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE RETURN VAR VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): if head == None or head.next == None: return head mid = findMid(head) left = head right = mid.next mid.next = None left = sortDoubly(left) right = sortDoubly(right) result = merge(left, right) return result def findMid(head): fast = head.next slow = head while fast and fast.next: fast = fast.next.next slow = slow.next return slow def merge(left, right): if left == None: return right if right == None: return left ans = Node(-1) temp = ans while left != None and right != None: if left.data > right.data: temp.next = right temp.next.prev = temp temp = right right = right.next else: temp.next = left temp.next.prev = temp temp = left left = left.next while left: temp.next = left temp.next.prev = temp temp = left left = left.next while right: temp.next = right temp.next.prev = temp temp = right right = right.next ans = ans.next ans.prev = None return ans
FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): m = [] s = head while s: m.append(s.data) s = s.next s = head m.sort() while s: s.data = m.pop(0) s = s.next return head
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def findMid(head): slow = head fast = head.next while fast != None and fast.next != None: slow = slow.next fast = fast.next.next return slow def merge(head1, head2): merged = Node(-1) temp = merged while head1 != None and head2 != None: if head1.data < head2.data: temp.next = head1 head1.prev = temp head1 = head1.next else: temp.next = head2 head2.prev = temp head2 = head2.next temp = temp.next while head1 != None: temp.next = head1 head1.prev = temp head1 = head1.next temp = temp.next while head2 != None: temp.next = head2 head2.prev = temp head2 = head2.next temp = temp.next merged = merged.next merged.prev = None return merged def sortDoubly(head): if head.next == None: return head mid = findMid(head) head2 = mid.next head2.prev = None mid.next = None newHead1 = sortDoubly(head) newHead2 = sortDoubly(head2) finalHead = merge(newHead1, newHead2) return finalHead
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def merge(head1, head2): if head1 == None: return head2 if head2 == None: return head1 node = root = None while head1 != None and head2 != None: if head1.data < head2.data: if root == None: node = root = head1 head1.prev = None head1 = head1.next else: node.next = head1 node.next.prev = node node = node.next head1 = head1.next elif root == None: node = root = head2 head2.prev = None head2 = head2.next else: node.next = head2 node.next.prev = node node = node.next head2 = head2.next if head1: node.next = head1 head1.prev = node if head2: node.next = head2 head2.prev = node return root def sortDoubly(head): if head.next == None or head == None: return head fast = mid = head while fast != None and fast.next != None: fast = fast.next.next mid = mid.next mid.prev.next = None mid.prev = None left = sortDoubly(head) right = sortDoubly(mid) return merge(left, right)
FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR NONE WHILE VAR NONE VAR NONE IF VAR VAR IF VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): b = [] tmp = head while tmp: b.append(tmp.data) tmp = tmp.next b.sort() tmp = head for i in b: tmp.data = i tmp = tmp.next return head
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def find_middle(head): slow = head fast = head while fast.next != None and fast.next.next != None: slow = slow.next fast = fast.next.next return slow def sortDoubly(head): if head == None or head.next == None: return head middle_head = find_middle(head) middle_head_next = middle_head.next middle_head.next = None middle_head_next.prev = None left_head = sortDoubly(head) right_head = sortDoubly(middle_head_next) sorted_head = merge(left_head, right_head) return sorted_head def merge(l, r): if l == None: return r if r == None: return l if l.data <= r.data: new_head = Node(l.data) smaller_head = merge(l.next, r) new_head.next = smaller_head smaller_head.prev = new_head else: new_head = Node(r.data) smaller_head = merge(l, r.next) new_head.next = smaller_head smaller_head.prev = new_head return new_head
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): if head is None or head.next is None: return head mid = getMid(head) nextofmid = mid.next mid.next.prev = None mid.next = None left = sortDoubly(head) right = sortDoubly(nextofmid) sortedlist = sortedmerge(left, right) return sortedlist def sortedmerge(left, right): if left is None: return right if right is None: return left dummy = Node(0) tail = dummy while left and right: if left.data <= right.data: tail.next = left left.prev = tail left = left.next else: tail.next = right right.prev = tail right = right.next tail = tail.next if left: tail.next = left left.prev = tail if right: tail.next = right right.prev = tail head = dummy.next head.prev = None return head def getMid(head): if head is None: return head fast = head slow = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next return slow
FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): a = [] l = DoublyLinkedList() curr = head while curr: a.append(curr.data) curr = curr.next a.sort() for i in range(len(a)): l.append(a[i]) return l.head
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): if head == None or head.next == None: return head firstHalf, secondHalf = findMid4Doubly(head) hold1 = sortDoubly(firstHalf) hold2 = sortDoubly(secondHalf) head = sortedMerge4Doubly(hold1, hold2) return head def findMid4Doubly(head): slow = head fast = head temp = head prevslow = head while fast != None and fast.next != None: prevslow = slow slow = slow.next fast = fast.next.next if fast == None: slow.prev = None prevslow.next = None return head, slow else: temp = slow.next temp.prev = None slow.next = None return head, temp def sortedMerge4Doubly(head1, head2): if head1 == None: return head2 if head2 == None: return head1 head = tail = None if head1.data <= head2.data: head = tail = head1 head1 = head1.next else: head = tail = head2 head2 = head2.next while head1 != None and head2 != None: if head1.data <= head2.data: tail.next, head1.prev = head1, tail tail = head1 head1 = head1.next else: tail.next, head2.prev = head2, tail tail = head2 head2 = head2.next if head1 == None: tail.next, head2.prev = head2, tail else: tail.next, head1.prev = head1, tail return head
FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE RETURN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE RETURN VAR VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR NONE IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def getMiddle(head): slow = head fast = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next return slow def merge(l, r): if l is None: return r if r is None: return l if l.data < r.data: l.next = merge(l.next, r) l.next.prev = l l.prev = None return l else: r.next = merge(l, r.next) r.next.prev = r r.prev = None return r def sortDoubly(head): if head is None or head.next is None: return head mid = getMiddle(head) nexmid = mid.next mid.next = None nexmid.prev = None left = sortDoubly(head) right = sortDoubly(nexmid) return merge(left, right)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def length(p): ans = 0 while p: p = p.next ans += 1 return ans def merge(left, right): head = Node(-1) prev = head while left or right: if not left: prev.next = right right.prev = prev break elif not right: prev.next = left left.prev = prev break elif left.data < right.data: prev.next = left left.prev = prev prev = left left = left.next else: prev.next = right right.prev = prev prev = right right = right.next if head.next: head.next.prev = None return head.next def sortDoubly(head): if not head: return None if not head.next: return head n = length(head) half = n // 2 p = head while half > 1: p = p.next half -= 1 prev = p p = p.next prev.next = None if p: p.prev = None left = sortDoubly(head) right = sortDoubly(p) return merge(left, right)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF IF VAR RETURN NONE IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): return mergeSort(head) def mergeSort(head): if head is None or head.next is None: return head slow, fast = head, head.next while fast.next and fast.next.next: slow = slow.next fast = fast.next.next mid_prev = slow mid = slow.next mid_prev.next = None mid.prev = None head1 = mergeSort(head) head2 = mergeSort(mid) return merge(head1, head2) def merge(head1, head2): curr = dummy = Node(0) p1, p2 = head1, head2 while p1 and p2: if p1.data <= p2.data: curr.next = p1 p1.prev = curr p1 = p1.next else: curr.next = p2 p2.prev = curr p2 = p2.next curr = curr.next curr.next = p1 or p2 if p1: p1.prev = curr elif p2: p2.prev = curr dummy.next.prev = None return dummy.next
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def merge(head1, head2): if head2 == None: return head1 tmp = temp = pre = None while head1 and head2: if head1.data <= head2.data: if not tmp: temp = head1 temp.prev = pre pre = temp tmp = head1 else: tmp.next = head1 head1.prev = tmp pre = head1 tmp = tmp.next head1 = head1.next else: if not tmp: temp = head2 temp.prev = pre tmp = head2 else: tmp.next = head2 head2.prev = tmp pre = head2 tmp = tmp.next head2 = head2.next while head1: tmp.next = head1 head1.prev = tmp pre = head1 tmp = tmp.next head1 = head1.next while head2: tmp.next = head2 head2.prev = tmp pre = head2 tmp = tmp.next head2 = head2.next return temp def mergesort(head): if head: if not head.next: return head slow = fast = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next nxt = slow.next slow.next = None l = mergesort(head) r = mergesort(nxt) head = merge(l, r) return head return None def sortDoubly(head): return mergesort(head)
FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR VAR VAR NONE WHILE VAR VAR IF VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR IF VAR RETURN VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NONE FUNC_DEF RETURN FUNC_CALL VAR VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def merge(m1, m2, newhead): if m1 == None: return m2 if m2 == None: return m1 while m1 and m2: if m1.data < m2.data: if newhead == None: node = Node(m1.data) tail = node newhead = node else: node = Node(m1.data) tail.next = node node.prev = tail tail = tail.next m1 = m1.next else: if newhead == None: node = Node(m2.data) tail = node newhead = node else: node = Node(m2.data) tail.next = node node.prev = tail tail = tail.next m2 = m2.next while m1: node = Node(m1.data) tail.next = node node.prev = tail tail = tail.next m1 = m1.next while m2: node = Node(m2.data) tail.next = node node.prev = tail tail = tail.next m2 = m2.next return newhead def merge1(head, l): if head == None or head.next == None: return head a = l // 2 - 1 n = head while a > 0: n = n.next a -= 1 b = n.next n.next = None m1 = merge1(head, l // 2) m2 = merge1(b, l - l // 2) newhead = None return merge(m1, m2, newhead) def sortDoubly(head): l = 0 n = head while n: l += 1 n = n.next return merge1(head, l) return head
FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR WHILE VAR VAR IF VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
import sys def sortDoubly(head): temp = head l = [] while temp is not None: l.append(temp.data) temp = temp.next l.sort() temp = head for i in l: temp.data = i temp = temp.next return head sys.setrecursionlimit(1000000) class Node: def __init__(self, data): self.data = data self.next = None self.prev = None class DoublyLinkedList: def __init__(self): self.head = None self.tail = None def append(self, new_data): new_node = Node(new_data) if self.head is None: self.head = new_node self.tail = new_node return new_node.prev = self.tail self.tail.next = new_node self.tail = new_node def printList(self, node): while node.next is not None: node = node.next while node.prev is not None: node = node.prev while node is not None: print(node.data, end=" ") node = node.next print() def printList(node): temp = node while node is not None: print(node.data, end=" ") temp = node node = node.next print() while temp: print(temp.data, end=" ") temp = temp.prev if __name__ == "__main__": t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().strip().split())) llist = DoublyLinkedList() for e in arr: llist.append(e) llist.head = sortDoubly(llist.head) printList(llist.head) print()
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF WHILE VAR NONE ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def merge(list1, list2): if list1 == None: return list2 if list2 == None: return list2 a = list1 b = list2 if a.data <= b.data: head = a tail = a a = a.next else: head = b tail = b b = b.next while a != None and b != None: if a.data <= b.data: tail.next = a a.prev = tail tail = a a = a.next else: tail.next = b b.prev = tail tail = b b = b.next if a != None: tail.next = a a.prev = tail if b != None: tail.next = b b.prev = tail return head def findMid(head): slow = head fast = head while fast.next != None and fast.next.next != None: slow = slow.next fast = fast.next.next return slow def sortDoubly(head): if head == None or head.next == None: return head mid = findMid(head) nexttomid = mid.next mid.next = None nexttomid.prev = None left = sortDoubly(head) right = sortDoubly(nexttomid) return merge(left, right)
FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortedMerge(head1, head2): head3 = Node(0) p1 = head1 p2 = head2 p3 = head3 while p1 or p2: if p1 and p2: if p1.data > p2.data: temp = Node(p2.data) p3.next = temp temp.prev = p3 p3 = p3.next p2 = p2.next else: temp = Node(p1.data) p3.next = temp temp.prev = p3 p3 = p3.next p1 = p1.next elif p1: temp = Node(p1.data) p3.next = temp temp.prev = p3 p3 = p3.next p1 = p1.next elif p2: temp = Node(p2.data) p3.next = temp temp.prev = p3 p3 = p3.next p2 = p2.next else: break head3.next.prev = None return head3.next def sortDoubly(head): if not head or not head.next: return head slow = head fast = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next right = slow.next slow.next = None left = head left = sortDoubly(left) right = sortDoubly(right) result = sortedMerge(left, right) return result
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): a = [] while head: a.append(head.data) head = head.next a.sort() dummy = Node(-1) move = dummy for i in a: new = Node(i) move.next = new new.prev = move move = move.next val = dummy.next val.prev = None return val
FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def merge(first, second): if first is None: return second if second is None: return first if first.data < second.data: first.next = merge(first.next, second) first.next.prev = first first.prev = None return first else: second.next = merge(first, second.next) second.next.prev = second second.prev = None return second def sortDoubly(head): if head is None: return head if head.next is None: return head second = split(head) head = sortDoubly(head) second = sortDoubly(second) return merge(head, second) def split(head): fast = slow = head while True: if fast.next is None: break if fast.next.next is None: break fast = fast.next.next slow = slow.next temp = slow.next slow.next = None return temp
FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR WHILE NUMBER IF VAR NONE IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): ll = DoublyLinkedList() ptr = head lst = [] while ptr: lst.append(ptr.data) ptr = ptr.next lst.sort() for i in lst: ll.append(i) return ll.head
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): if head.next == None: return head def split(head): if head.next == None: return head fast = head slow = head while fast is not None and fast.next is not None: fast = fast.next.next slow = slow.next if fast: temp = slow.next slow.next.prev = None slow.next = None else: temp = slow slow = slow.prev temp.prev = None slow.next = None return temp temp = split(head) left = sortDoubly(head) right = sortDoubly(temp) result = None while left and right: if left.data <= right.data: newNode = Node(left.data) if result: result.next = newNode newNode.prev = result result = result.next else: result = newNode ans = result left = left.next else: newNode = Node(right.data) if result: result.next = newNode newNode.prev = result result = result.next else: result = newNode ans = result right = right.next while left: result.next = left left.prev = result break while right: result.next = right right.prev = result break return ans
FUNC_DEF IF VAR NONE RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): a = [] curr = head while curr != None: a.append(curr.data) curr = curr.next a.sort() i = 0 temp = head while temp: temp.data = a[i] i += 1 temp = temp.next return head
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): tail = head while tail.next is not None: tail = tail.next head, tail = sort(head, tail) return head def printList1(head, tail): temp = head while temp is not None: print(temp.data, end=" ") temp = temp.next print() temp = tail while temp is not None: print(temp.data, end=" ") temp = temp.prev print() def sort(head, tail): if head is None or head.next is None: return head, head mid, end = head, head while end is not None and end.next is not None: mid = mid.next end = end.next.next head_pointer = head.prev tail_pointer = tail.next head.prev = None if head_pointer: head_pointer.next = None tail.next = None if tail_pointer: tail_pointer.prev = None prev_mid = mid.prev prev_mid.next = None mid.prev = None head1, tail1 = sort(head, prev_mid) head2, tail2 = sort(mid, tail) head, tail = merge(head1, tail1, head2, tail2) head.prev = head_pointer if head_pointer: head_pointer.next = head tail.next = tail_pointer if tail_pointer: tail_pointer.prev = tail return head, tail def conditionalPrint(head1, tail1, head2, tail2, nodes): if head1 == 3 and tail1 == 5 and head2 == 1 and tail2 == 9: for node in nodes: print(node.data, end=" ") print() def merge(head1, tail1, head2, tail2): p1, p2, p3, p4 = ( head1.data if head1 else "", tail1.data if tail1 else "", head2.data if head2 else "", tail2.data if tail2 else "", ) cur = None if head1.data <= head2.data: cur = head1 head1 = head1.next else: cur = head2 head2 = head2.next cur_head = cur while head1 is not None and head2 is not None: if head1.data <= head2.data: cur.next = head1 head1 = head1.next else: cur.next = head2 head2 = head2.next cur.next.prev = cur cur = cur.next while head1 is not None: cur.next = head1 head1 = head1.next cur.next.prev = cur cur = cur.next while head2 is not None: cur.next = head2 head2 = head2.next cur.next.prev = cur cur = cur.next return cur_head, cur
FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR NONE ASSIGN VAR NONE IF VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def fd(h): s, f = h, h while f.next != None and f.next.next != None: s = s.next f = f.next.next temp = s.next s.next = None return temp def sortDoubly(head): if head == None or head.next == None: return head spli = fd(head) nh1 = sortDoubly(head) nh2 = sortDoubly(spli) return merge(nh1, nh2) def merge(h1, h2): if h1 == None: return h2 elif h2 == None: return h1 if h1.data < h2.data: h1.next = merge(h1.next, h2) h1.next.prev = h1 h1.prev = None return h1 else: h2.next = merge(h1, h2.next) h2.next.prev = h2 h2.prev = None return h2
FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def merge(a, L, R): i = 0 j = 0 k = 0 while i < len(L) and j < len(R): if L[i] <= R[j]: a[k] = L[i] i += 1 k += 1 else: a[k] = R[j] j += 1 k += 1 while i < len(L): a[k] = L[i] i += 1 k += 1 while j < len(R): a[k] = R[j] j += 1 k += 1 def merge_sort(a): if len(a) > 1: mid = len(a) // 2 L = a[:mid] R = a[mid:] merge_sort(L) merge_sort(R) merge(a, L, R) def sortDoubly(head): a = [] curr = head while curr is not None: a.append(curr.data) curr = curr.next merge_sort(a) curr = head i = 0 while curr is not None: curr.data = a[i] i += 1 curr = curr.next return head
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NONE ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): if head is None or head.next is None: return head slow = head fast = head while fast.next is not None and fast.next.next is not None: slow = slow.next fast = fast.next.next head1 = head head2 = slow.next slow.next = None head2.prev = None head1 = sortDoubly(head1) head2 = sortDoubly(head2) return merge(head1, head2) def merge(head1, head2): if head1 is None: return head2 if head2 is None: return head1 if head1.data < head2.data: head1.next = merge(head1.next, head2) head1.next.prev = head1 head1.prev = None return head1 else: head2.next = merge(head1, head2.next) head2.next.prev = head2 head2.prev = None return head2
FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): if head == None or head.next == None: return head h1 = head h2 = Csplit(head) l1 = sortDoubly(h1) l2 = sortDoubly(h2) return Cmerge(l1, l2) def Csplit(head): fast = slow = head while 1: if fast.next == None or fast.next.next == None: break fast = fast.next.next slow = slow.next secondHead = slow.next secondHead.prev = None slow.next = None return secondHead def Cmerge(l1, l2): if l1 == None: return l2 elif l2 == None: return l1 if l1.data < l2.data: l1.next = Cmerge(l1.next, l2) l1.next.prev = l1 return l1 else: l2.next = Cmerge(l1, l2.next) l2.next.prev = l2 return l2
FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR WHILE NUMBER IF VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def sortDoubly(head): head1 = None head2 = None a = head b = None c = None p = [] while a is not None: p.append(a.data) a = a.next for i in sorted(p): m = Node(i) if head1 is None: head1 = m a = head1 else: a.next = m m.prev = a a = a.next return head1
FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR LIST WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given Pointer/Reference to the head of a doubly linked list of N nodes, the task is to Sort the given doubly linked list using Merge Sort in both non-decreasing and non-increasing order. Example 1: Input: N = 8 value[] = {7,3,5,2,6,4,1,8} Output: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Explanation: After sorting the given linked list in both ways, resultant matrix will be as given in the first two line of output, where first line is the output for non-decreasing order and next line is for non- increasing order. Example 2: Input: N = 5 value[] = {9,15,0,-1,0} Output: -1 0 0 9 15 15 9 0 0 -1 Explanation: After sorting the given linked list in both ways, the resultant list will be -1 0 0 9 15 in non-decreasing order and 15 9 0 0 -1 in non-increasing order. Your Task: The task is to complete the function sortDoubly() which sorts the doubly linked list. The printing is done automatically by the driver code. Constraints: 1 <= N <= 10^{5}
def link_mid(head): fast, slow = head.next, head while fast and fast.next: slow = slow.next fast = fast.next.next return slow def merge_sort(hd1, hd2): ans = None while hd1 and hd2: if hd1.data < hd2.data: if ans == None: ans = hd1 nxt = ans else: nxt.next = hd1 hd1.prev = nxt nxt = nxt.next hd1 = hd1.next else: if ans == None: ans = hd2 nxt = ans else: nxt.next = hd2 hd2.prev = nxt nxt = nxt.next hd2 = hd2.next if hd1: if ans == None: ans = hd1 else: nxt.next = hd1 hd1.prev = nxt if hd2: if ans == None: ans = hd2 else: nxt.next = hd2 hd2.prev = nxt return ans def merge(head): if head.next == None: return head mid = link_mid(head) temp = mid.next mid.next = None temp.prev = None left = merge(head) right = merge(temp) return merge_sort(left, right) def sortDoubly(head): return merge(head)
FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR
Given two arrays X[] and Y[] of size M and N respectively. Find number of pairs such that x^{y} > y^{x} where x is an element from X[] and y is an element from Y[]. Example 1: Input: M = 3, N = 2 X[] = {2, 1, 6}, Y = {1, 5} Output: 3 Explanation: There are total 3 pairs where pow(x, y) is greater than pow(y, x) Pairs are (2, 1), (2, 5) and (6, 1). Example 2: Input: M = 3, N = 3 X[] = {10, 19, 18}, Y[] = {11, 15, 9} Output: 2 Explanation: There are total 2 pairs where pow(x, y) is greater than pow(y, x) Pairs are (10, 11) and (10, 15). Your Task: You don't need to read input or print anything. Your task is to complete the function countPairs() which takes array X[], array Y[], m and n as input parameters and returns an integer denoting the number of pairs that are true to the given condition. Expected Time Complexity: O(N*logN + M*logM) Expected Auxiliary Space: O(1) Constraints: 1 ≀ M, N ≀ 10^{5} 1 ≀ X[i], Y[i] ≀ 10^{3}
class Solution: def get_index(self, y, n, ele): ans = -1 low = 0 high = n - 1 while low <= high: mid = (low + high) // 2 if Y[mid] > ele: ans = mid high = mid - 1 else: low = mid + 1 return ans def countPairs(self, X, Y, m, n): X.sort() Y.sort() y_c0 = Y.count(0) y_c1 = Y.count(1) y_c3 = Y.count(3) y_c4 = Y.count(4) y_c2 = Y.count(2) i = 0 j = 0 ind = -1 c = 0 for i in range(m): if X[i] == 0: continue elif X[i] == 1: c += y_c0 elif X[i] == 2: ind = self.get_index(Y, n, 2) if ind != -1: c += n - ind c -= y_c3 c -= y_c4 c += y_c1 + y_c0 else: ind = self.get_index(Y, n, X[i]) if ind != -1: c += n - ind c += y_c1 + y_c0 if X[i] == 3: c += y_c2 return c
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR VAR RETURN VAR
Given two arrays X[] and Y[] of size M and N respectively. Find number of pairs such that x^{y} > y^{x} where x is an element from X[] and y is an element from Y[]. Example 1: Input: M = 3, N = 2 X[] = {2, 1, 6}, Y = {1, 5} Output: 3 Explanation: There are total 3 pairs where pow(x, y) is greater than pow(y, x) Pairs are (2, 1), (2, 5) and (6, 1). Example 2: Input: M = 3, N = 3 X[] = {10, 19, 18}, Y[] = {11, 15, 9} Output: 2 Explanation: There are total 2 pairs where pow(x, y) is greater than pow(y, x) Pairs are (10, 11) and (10, 15). Your Task: You don't need to read input or print anything. Your task is to complete the function countPairs() which takes array X[], array Y[], m and n as input parameters and returns an integer denoting the number of pairs that are true to the given condition. Expected Time Complexity: O(N*logN + M*logM) Expected Auxiliary Space: O(1) Constraints: 1 ≀ M, N ≀ 10^{5} 1 ≀ X[i], Y[i] ≀ 10^{3}
class Solution: def countPairs(self, X, Y, m, n): for i in range(m): X[i] **= 1 / X[i] for i in range(n): Y[i] **= 1 / Y[i] X.sort() Y.sort() i = j = 0 ans = 0 while i < m: if j == n: ans += n else: while j < n and X[i] > Y[j]: j += 1 ans += j i += 1 return ans
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR
Given two arrays X[] and Y[] of size M and N respectively. Find number of pairs such that x^{y} > y^{x} where x is an element from X[] and y is an element from Y[]. Example 1: Input: M = 3, N = 2 X[] = {2, 1, 6}, Y = {1, 5} Output: 3 Explanation: There are total 3 pairs where pow(x, y) is greater than pow(y, x) Pairs are (2, 1), (2, 5) and (6, 1). Example 2: Input: M = 3, N = 3 X[] = {10, 19, 18}, Y[] = {11, 15, 9} Output: 2 Explanation: There are total 2 pairs where pow(x, y) is greater than pow(y, x) Pairs are (10, 11) and (10, 15). Your Task: You don't need to read input or print anything. Your task is to complete the function countPairs() which takes array X[], array Y[], m and n as input parameters and returns an integer denoting the number of pairs that are true to the given condition. Expected Time Complexity: O(N*logN + M*logM) Expected Auxiliary Space: O(1) Constraints: 1 ≀ M, N ≀ 10^{5} 1 ≀ X[i], Y[i] ≀ 10^{3}
def search(arr, l, r, x): if r >= l: mid = l + (r - l) // 2 if arr[mid] <= x: return search(arr, mid + 1, r, x) else: return search(arr, l, mid - 1, x) return l class Solution: def countPairs(self, X, Y, m, n): nn = [0] * 5 for i in range(n): if Y[i] < 5: nn[Y[i]] += 1 Y.sort() pairs = 0 for i in X: if i == 0: continue elif i == 1: pairs += nn[0] else: ss = search(Y, 0, n - 1, i) if ss > n - 1: ss = 0 else: pairs += n - ss pairs += nn[0] + nn[1] if i == 2: pairs -= nn[3] + nn[4] if i == 3: pairs += nn[2] return pairs
FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN VAR
Given two arrays X[] and Y[] of size M and N respectively. Find number of pairs such that x^{y} > y^{x} where x is an element from X[] and y is an element from Y[]. Example 1: Input: M = 3, N = 2 X[] = {2, 1, 6}, Y = {1, 5} Output: 3 Explanation: There are total 3 pairs where pow(x, y) is greater than pow(y, x) Pairs are (2, 1), (2, 5) and (6, 1). Example 2: Input: M = 3, N = 3 X[] = {10, 19, 18}, Y[] = {11, 15, 9} Output: 2 Explanation: There are total 2 pairs where pow(x, y) is greater than pow(y, x) Pairs are (10, 11) and (10, 15). Your Task: You don't need to read input or print anything. Your task is to complete the function countPairs() which takes array X[], array Y[], m and n as input parameters and returns an integer denoting the number of pairs that are true to the given condition. Expected Time Complexity: O(N*logN + M*logM) Expected Auxiliary Space: O(1) Constraints: 1 ≀ M, N ≀ 10^{5} 1 ≀ X[i], Y[i] ≀ 10^{3}
class Solution: def bina(self, x, g, n): l = 0 h = n - 1 ans = -1 while l <= h: m = (l + h) // 2 if g[m] > x: ans = m h = m - 1 else: l = m + 1 return ans def countPairs(self, a, b, M, N): s = {} for i in range(5): s[i] = 0 for i in b: if i in s: s[i] += 1 b.sort() a.sort() c = 0 for i in a: if i == 0: continue elif i == 1: c += s[0] elif i == 2: ind = self.bina(i, b, N) if ind != -1: c += N - ind c += s[1] + s[0] - s[3] - s[4] else: ind = self.bina(i, b, N) if ind != -1: c += N - ind c += s[0] + s[1] if i == 3: c += s[2] return c
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def func(): n = int(input()) array = [] sum = 0 for index, number in enumerate(input().split()): num = int(number) array.append(num) sum += num if sum % 1 == 1: print("NO") return setn = set() sumSub = 0 indexdict = {} for indexm, number in enumerate(array): sumSub += number sum2 = sumSub * 2 - sum if sum2 % 2 == 1: continue sum2 /= 2 setn.add(sum2) indexdict[sum2] = indexm i = 0 if 0 in setn: print("YES") return while i < n: num = array[i] if num in setn and num in indexdict and i < indexdict[num]: print("YES") return if -num in setn and -num in indexdict and i > indexdict[-num]: print("YES") return i += 1 print("NO") func()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def read_ints(): return list(map(int, input().split())) n = int(input()) a = [0] + read_ints() s = a.copy() for i in range(1, n + 1): s[i] += s[i - 1] total = s[n] found = False for x in s: if x << 1 == total: found = True break else: for i, x in enumerate(s[1:], 1): def search(lo, hi, target): while lo <= hi: m = lo + hi >> 1 cur = s[m] << 1 if cur == target: return True elif cur < target: lo = m + 1 else: hi = m - 1 return False if search(i, n, total + 2 * a[i]) or search(1, i - 1, total - 2 * a[i]): found = True print("YES" if found else "NO")
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) a = list(map(int, input().strip().split())) if sum(a) % 2 != 0: print("NO") else: b = set([]) f = sum(a) // 2 s = 0 c = 1 for i in range(n): b.add(a[i]) s = s + a[i] if s == f: print("YES") c = 0 break elif s - f in b: print("YES") c = 0 break if c == 1: a = a[::-1] b = set([]) f = sum(a) // 2 s = 0 c = 1 for i in range(n): b.add(a[i]) s = s + a[i] if s == f: print("YES") c = 0 break elif s - f in b: print("YES") c = 0 break if c == 1: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def isPossible(v): if sum(v) % 2 == 1: return False ls = 0 ts = sum(v) // 2 mp = {} for x in v: if not x in mp: mp[x] = 0 mp[x] += 1 for x in v: if ts - ls in mp and mp[ts - ls] > 0: return True ls += x mp[x] -= 1 return False n = int(input()) v = [int(x) for x in input().split()] p = isPossible(v) v.reverse() p2 = isPossible(v) if p or p2: print("YES") else: print("NO")
FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) a = list(map(int, input().split())) if n == 1: print("NO") exit(0) p = 0 left = dict() right = dict() s = 0 for i in a: right[i] = right.get(i, 0) + 1 s += i if s % 2 != 0: print("NO") exit(0) s //= 2 for i in range(n + 1): if right.get(s - p, 0) > 0 or left.get(p - s, 0) > 0: print("YES") exit(0) if i < n: p += a[i] right[a[i]] -= 1 left[a[i]] = left.get(a[i], 0) + 1 print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def f(aa, s): d = {a: i for i, a in enumerate(aa)} for i, a in enumerate(aa): if s in d and i <= d[s]: return True s -= a if s <= 0: break return not s n = int(input()) aa = list(map(int, input().split())) s, res = sum(aa), False if not s & 1 and n > 1: res = f(aa, s // 2) if not res: aa.reverse() res = f(aa, s // 2) print(("NO", "YES")[res])
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING STRING VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
from sys import stdin n = int(stdin.readline()) arr = [int(x) for x in stdin.readline().split()] s = sum(arr) if s % 2 == 1: print("NO") else: goal = s // 2 s = 0 e = {} e2 = set() for x in arr: if x in e: e[x] += 1 else: e[x] = 1 if goal in e: print("YES") else: valid = False for x in arr: s += x e2.add(x) e[x] -= 1 if e[x] == 0: del e[x] if goal - s in e: valid = True break if s - goal in e2: valid = True break if valid: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) a = list(map(int, input().split())) s1 = [0] * n s1[0] = a[0] for i in range(1, n): s1[i] = s1[i - 1] + a[i] s2 = [0] * n s2[n - 1] = a[n - 1] for i in range(n - 2, -1, -1): s2[i] = s2[i + 1] + a[i] s1 = [0] + s1 s2 = s2 + [0] d = {} for i in range(n): if a[i] not in d: d[a[i]] = 1 else: d[a[i]] += 1 t = {} for i in range(n + 1): diff = s1[i] - s2[i] if diff == 0: print("YES") exit() elif diff < 0 and diff % 2 == 0: if abs(diff) // 2 in d: print("YES") exit() elif diff > 0 and diff % 2 == 0: if abs(diff) // 2 in t: print("YES") exit() if i < n: d[a[i]] -= 1 if d[a[i]] == 0: del d[a[i]] if a[i] not in t: t[a[i]] = 1 else: t[a[i]] += 1 print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
import sys from itertools import accumulate def solve(): n = int(input()) a = [int(i) for i in input().split()] S = sum(a) if S & 1: print("NO") return f_a = dict() l_a = dict() for i in range(n): if a[i] not in f_a: f_a[a[i]] = i for i in range(n - 1, -1, -1): if a[i] not in l_a: l_a[a[i]] = i ps = [0] + list(accumulate(a)) S //= 2 for k in range(1, n + 1): x = S - ps[k] if x == 0: print("YES") return elif x > 0: if x in l_a and k <= l_a[x]: print("YES") return else: x *= -1 if x in f_a and f_a[x] < k: print("YES") return print("NO") solve()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) l = input() a = [] ma = -1 b = [] im = {} out = {} x = 0 yes = False for i in l.split(): aux = int(i) a.append(aux) x += aux b.append(x) if not aux in out: out[aux] = 1 else: out[aux] += 1 if ma == -1: ma = a[0] elif ma < a[-1]: ma = a[-1] half = x / 2 if x % 2 == 0 and ma < half: for i in range(n): num = b[i] out[a[i]] -= 1 if out[a[i]] == 0: del out[a[i]] if not a[i] in im: im[a[i]] = 1 else: im[a[i]] += 1 if num == half: yes = True break elif num < half and num + ma >= half: if half - num in out: yes = True break elif num > half and num - ma <= half: if num - half in im: yes = True break else: yes = ma == half if yes: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
a = int(input()) nums = [int(i) for i in input().split()] tot = sum(nums) def runner(lpr): total = 0 used = {} for i in range(*lpr): used[nums[i]] = 1 total += nums[i] distance = 2 * total - tot if distance % 2 == 0 and used.get(distance // 2, -1) != -1: print("YES") exit(0) runner((0, a)) runner((a - 1, -1, -1)) print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) arr = [int(x) for x in input().split()] if n == 1: print("NO") exit(0) s = sum(arr) sp = 0 ss = s setq = {} setp = {} for i in arr: setq[i] = setq.get(i, 0) + 1 setp[i] = 0 for i in range(n): sp += arr[i] ss -= arr[i] setp[arr[i]] += 1 setq[arr[i]] -= 1 val = ss - sp if val > 0 and not val & 1: val //= 2 ans = setq.get(val, 0) if ans > 0: print("YES") exit(0) elif val < 0 and not -val & 1: val = -val val //= 2 ans = setp.get(val, 0) if ans > 0: print("YES") exit(0) elif val == 0: print("YES") exit(0) print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) a = [int(x) for x in input().split()] l = [0] for x in a: l.append(l[-1] + x) r = [0] for x in reversed(a): r.append(r[-1] + x) r = r[::-1] ls, rs = {}, {} for x in a: rs.setdefault(x, 0) rs[x] += 1 for i in range(n): if i > 0: x = a[i - 1] ls.setdefault(x, 0) ls[x] += 1 rs[x] -= 1 d = l[i] - r[i] if d == 0: print("YES") break if d % 2 == 1: continue if d > 0 and ls.get(d // 2, 0) > 0: print("YES") break if d < 0 and rs.get(-d // 2, 0) > 0: print("YES") break else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR DICT DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
from sys import exit n = int(input().strip()) ais = list(map(int, input().strip().split())) iais = [(0) for _ in range(n + 1)] for i in range(n): iais[i + 1] = iais[i] + ais[i] s = iais[-1] if s % 2 == 1: print("NO") exit() s2 = s // 2 if s2 in iais: print("YES") exit() ia = list(range(n)) ia.sort(key=lambda i: ais[i], reverse=True) j = 0 for i in ia: while j < n and s2 - iais[j] > ais[i]: j += 1 if s2 - iais[j] == ais[i] and i >= j: print("YES") exit() if s2 - iais[j] < 0: break for i in reversed(ia): while j < n and s2 - iais[j] > -ais[i]: j += 1 if s2 - iais[j] == -ais[i] and i < j: print("YES") exit() print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
import sys def read_line(): return sys.stdin.readline()[:-1] def read_int(): return int(sys.stdin.readline()) def read_int_line(): return [int(v) for v in sys.stdin.readline().split()] n = read_int() a = read_int_line() d = {} d[0] = -1 pref = [0] * (n + 1) for i in range(1, n + 1): pref[i] = pref[i - 1] + a[i - 1] if a[i - 1] not in d: d[a[i - 1]] = i - 1 tot = pref[n] req = tot // 2 f = False for i in range(1, n + 1): if pref[i] >= req: if pref[i] - req in d: if d[pref[i] - req] < i: f = True break a = a[::-1] d = {} d[0] = -1 pref = [0] * (n + 1) for i in range(1, n + 1): pref[i] = pref[i - 1] + a[i - 1] if a[i - 1] not in d: d[a[i - 1]] = i - 1 tot = pref[n] req = tot // 2 f1 = False for i in range(1, n + 1): if pref[i] >= req: if pref[i] - req in d: if d[pref[i] - req] < i: f1 = True break if (f or f1) and tot % 2 == 0: print("YES") else: print("NO")
IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER 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 IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER 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 IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def solve(n, a): tot = 0 for i in range(n): tot += a[i] diffs = [] diffs.append(-tot) for i in range(n): tot -= 2 * a[i] diffs.append(-tot) if tot == 0: return "YES" for i in range(n): diffmake = 2 * a[i] j = binary(diffs, diffmake) if j > i and j != -1: return "YES" j = binary(diffs, -diffmake) if i >= j and j != -1: return "YES" return "NO" def binary(a, value): hi = len(a) lo = -1 while lo + 1 < hi: mi = (lo + hi) // 2 if a[mi] == value: return mi if a[mi] < value: lo = mi else: hi = mi return -1 n = int(input()) a = input().split() for i in range(n): a[i] = int(a[i]) print(solve(n, a))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER RETURN STRING RETURN STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
import sys n = int(input()) a = list(map(int, input().split())) lt, rt = 0, sum(a) cur = set() cur.add(0) ans = False for i in range(n - 1): cur.add(2 * a[i]) lt += a[i] rt -= a[i] if lt - rt in cur: ans = True lt, rt = sum(a), 0 cur = set() cur.add(0) for i in range(n - 1, -1, -1): cur.add(2 * a[i]) lt -= a[i] rt += a[i] if rt - lt in cur: ans = True print("YES" if ans else "NO")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def verifica(l, r, p): global vetor_aux while l <= r: meio = l + r >> 1 if vetor_aux[meio] == p: return True if vetor_aux[meio] < p: l = meio + 1 else: r = meio - 1 return False n = int(input()) vetor = list(map(int, input().split())) vetor = [0] + vetor vetor_aux = [0] * len(vetor) for i in range(1, n + 1): vetor_aux[i] = vetor_aux[i - 1] + vetor[i] if vetor_aux[n] & 1: print("NO") exit() for i in range(1, n + 1): if verifica(i + 1, n, vetor_aux[n] / 2 + vetor[i]): print("YES") exit() for i in reversed(range(n + 1)): if verifica(1, i - 1, vetor_aux[n] / 2 - vetor[i]): print("YES") exit() print("NO")
FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def process(A): S = sum(A) d1 = {} for x in A: if x not in d1: d1[x] = 0 d1[x] += 1 if S % 2 == 1: return "NO" S1 = 0 d2 = set([]) for x in A: S1 += x d1[x] -= 1 d2.add(x) y = S1 - S // 2 if y == 0: return "YES" if y in d2: return "YES" y2 = S // 2 - S1 if y2 in d1 and d1[y2] > 0: return "YES" return "NO" n = int(input()) A = [int(x) for x in input().split()] print(process(A))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST FOR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR VAR RETURN STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
import sys n = int(input()) a = [int(i) for i in input().split()] total = sum(a) if total % 2 == 1: print("NO") sys.exit() part_sum = total // 2 seen = set([0]) sum_so_far = 0 for x in a: sum_so_far += x seen.add(x) if sum_so_far - part_sum in seen: print("YES") sys.exit() seen = set([0]) sum_so_far = 0 for x in a[::-1]: sum_so_far += x seen.add(x) if sum_so_far - part_sum in seen: print("YES") sys.exit() print("NO")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
import sys class BIT_RSQ(object): __slots__ = ["nodes", "size"] def __init__(self, size: int): self.nodes = [0] * (size + 1) self.size = size + 1 def add(self, index: int, value: int): while index < self.size: self.nodes[index] += value index += index & -index def sum(self, right: int): result = 0 while right: result += self.nodes[right] right -= right & -right return result def lower_bound(self, value: int): k, result = 1 << self.size.bit_length() - 1, 0 while k: if k + result < self.size and self.nodes[k + result] < value: value -= self.nodes[k + result] result += k k >>= 1 return result + 1 n = int(input()) a = list(map(int, input().split())) total = sum(a) if total & 1: print("NO") exit() half = total >> 1 for _ in range(2): bit = BIT_RSQ(n + 10) l, r = 0, total for i in range(n): l += a[i] r -= a[i] try: if l == r or half > r and bit.sum(bit.lower_bound(half - r)) == half - r: print("YES") exit() except Exception: pass bit.add(n - i + 5, a[i]) a.reverse() print("NO")
IMPORT CLASS_DEF VAR ASSIGN VAR LIST STRING STRING FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF VAR ASSIGN VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER WHILE VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) b = list(map(int, input().split())) d = dict() e = dict() for j in range(n): if b[j] in d.keys(): d[b[j]] += 1 e[b[j]] += 1 else: d[b[j]] = 1 e[b[j]] = 1 if sum(b) % 2 != 0: print("NO") else: p = sum(b) // 2 j = 0 s = 0 f = 0 while j < n: if s == p: f = 1 break if p - s in d.keys() and d[p - s] > 0: f = 1 break s += b[j] d[b[j]] += -1 j += 1 j = 0 s = 0 b.reverse() while j < n: if s == p: f = 1 break if p - s in e.keys() and e[p - s] > 0: f = 1 break s += b[j] e[b[j]] += -1 j += 1 if f == 1: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def divPos(arr): sumArr = sum(arr) targetSum = sumArr / 2 if sumArr % 2 != 0: return False lArr = set() sumlArr = 0 for i in range(len(arr)): lArr.add(arr[i]) sumlArr += arr[i] if sumlArr == targetSum or sumlArr > targetSum and sumlArr - targetSum in lArr: return True return False def __starting_point(): n = input() arr = [int(x) for x in input().split()] if divPos(arr) or divPos(arr[::-1]): print("YES") else: print("NO") __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) A = list(map(int, input().split())) d = dict() acc = 0 for i, j in enumerate(A): acc += j d[acc] = i if acc % 2 == 1: print("NO") exit() for i, j in enumerate(A): v = acc // 2 - j if v in d and d[v] < i: print("YES") exit() v = acc / 2 + j if v in d and d[v] >= i: print("YES") exit() print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def possible(a): if len(a) == 1: return False if len(a) == 2: return a[0] == a[1] total = sum(a) vals = set([a[0]]) running = a[0] for el in a[1:]: running += el vals.add(el) if running == total - running: return True if (running - (total - running)) % 2 == 0 and ( running - (total - running) ) // 2 in vals: return True return False n = int(input()) a = list(map(int, input().split())) if possible(a) or possible(a[::-1]): print("YES") else: print("NO")
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) a = list(map(int, input().split())) d = {} for i in range(n): if a[i] in d: d[a[i]] += 1 else: d[a[i]] = 1 if sum(a) % 2 == 1: print("NO") else: i = 0 lim = sum(a) // 2 tot = 0 ans = "NO" d2 = {} while i < n: tot += a[i] d[a[i]] -= 1 if a[i] in d2: d2[a[i]] += 1 else: d2[a[i]] = 1 if tot < lim: x = lim - tot if x in d: if d[x] > 0: ans = "YES" break elif tot == lim: ans = "YES" break else: x = tot - lim if x in d2: if d2[x] > 0: ans = "YES" break i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR DICT WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) a = [int(x) for x in input().split()] def helper(b): total = sum(b) target = total / 2 s = set() if total % 2 != 0: return False else: ans = 0 for i in b: ans += i s.add(i) if ans == target or ans - target > 0 and ans - target in s: return True return False if helper(a) or helper(a[::-1]): print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def list_input(): return list(map(int, input().split())) def map_input(): return map(int, input().split()) def map_string(): return input().split() n = int(input()) a = list_input() k = sum(a) cur = 0 s = set([0]) pref = [0] for i in a: pref.append(pref[-1] + i) s.add(pref[-1]) for i in range(n): s.remove(pref[i]) if k / 2 + a[i] in s: print("YES") break else: a.reverse() cur = 0 s = set([0]) pref = [0] for i in a: pref.append(pref[-1] + i) s.add(pref[-1]) for i in range(n): s.remove(pref[i]) if k / 2 + a[i] in s: print("YES") break else: print("NO")
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
from itertools import * n = int(input()) l = list(map(int, input().split())) for j in range(0, 2): m = {} l = [0] + list(reversed(l)) for i in range(len(l)): m[l[i]] = i ac = list(accumulate(l)) s = ac[-1] for i in range(0, len(ac) - 1): if ac[i] == s / 2 or s / 2 - ac[i] in m.keys() and m[s / 2 - ac[i]] > i: print("YES") quit() print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
NO = "NO" YES = "YES" n = int(input()) if n == 1: print(NO) exit() A = list(map(int, input().split())) if n <= 2: if A[0] == A[1]: print(YES) exit() else: print(NO) exit() s = sum(A) if s % 2 == 1: print(NO) exit() p = s // 2 o_list = set(A[:2]) o_sum = sum(A[:2]) o_max = max(o_list) for x in range(2, n): o_list.add(A[x]) o_sum = o_sum + A[x] o_max = A[x] if o_max < A[x] else o_max if o_sum - p in o_list: print(YES) exit() A.reverse() o_list = set(A[:2]) o_sum = sum(A[:2]) o_max = max(o_list) for x in range(2, n): o_list.add(A[x]) o_sum = o_sum + A[x] o_max = A[x] if o_max < A[x] else o_max if o_sum - p in o_list: print(YES) exit() print(NO)
ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
class Dick: def __init__(self): self.s = 0 self.m = dict() def add(self, x): self.s += x if x in self.m: self.m[x] += 1 else: self.m[x] = 1 def rm(self, x): self.s -= x if self.m[x] > 1: self.m[x] -= 1 else: self.m.pop(x) def shasei(self): return self.s def ininder(self, x): return x in self.m def ok(a, n): w = sum(a) if w & 1: return 0 f = Dick() s = Dick() for x in a: s.add(x) w //= 2 for i in range(n): if s.ininder(w - f.shasei()) or f.ininder(w - s.shasei()): return 1 f.add(a[i]) s.rm(a[i]) return 0 n = int(input()) a = list(map(int, input().split())) print("YES" if ok(a, n) else "NO")
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR FUNC_DEF RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) a = list(map(int, input().split())) s = sum(a) def solve(a): rtn = False tmp = 0 dic = {} for i in range(n): tmp += a[i] dic[a[i]] = True if tmp == s // 2: rtn = True break elif s // 2 < tmp and tmp - s // 2 in dic: rtn = True break return rtn if n == 1 or s % 2 == 1: print("NO") elif solve(a) or solve(list(reversed(a))): print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER RETURN VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def solve(a): s = sum(a) if s % 2 == 1: return False s //= 2 elements = set() cs = 0 n = len(a) for i in range(n): cs += a[i] if cs == s: return True elements.add(a[i]) if i > 0 and cs > s and cs - s in elements: return True return False def main(): n = int(input()) a = list(map(int, input().split())) if solve(a): print("YES") elif solve(a[::-1]): print("YES") else: print("NO") main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def solve(): n = int(input()) a = list(map(int, input().split())) ok = 0 Su = sum(a) di = dict() di[0] = 1 su = 0 for i in range(n): su += a[i] if di.get(a[i] * 2, 0) == 0: di[a[i] * 2] = 1 if di.get(su - Su + su, 0) == 0: pass else: ok = 1 di.clear() di[0] = 1 su = 0 for i in range(-1, -n - 1, -1): su += a[i] if di.get(a[i] * 2, 0) == 0: di[a[i] * 2] = 1 if di.get(su - Su + su, 0) == 0: pass else: ok = 1 if ok: print("YES") else: print("NO") solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) s = sum(a) if s % 2: print("NO") exit() pre = 0 used = set() flag = False for rep in range(2): for i in range(n): pre += a[i] used.add(a[i]) if pre - s // 2 in used: flag = True a.reverse() used = set() pre = 0 if flag: print("YES") else: print("NO")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def find(ar: list) -> bool: p = [0] s = sum(ar) if s % 2 != 0: return False for elm in ar: p.append(p[-1] + elm) if p[-1] == s >> 1: return True lk = set() n = len(ar) for i in range(1, n): lk.add(ar[i - 1]) elm = -(s >> 1) + (p[i] + ar[i]) if elm in lk: return True return False n = int(input()) ar = list(map(int, input().split())) rar = ar[::-1] if find(ar) or find(rar): print("YES") else: print("NO")
FUNC_DEF VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) z = list(map(int, input().split())) s = sum(z) c = s if c % 2 != 0: f = 1 elif z[0] == s / 2 or z[-1] == s / 2: f = 0 else: f = 1 for k in range(2): d = set() c = 0 for i in range(n): d.add(z[i]) c += z[i] if c - s / 2 in d: f = 0 break if f == 0: break z.reverse() print(["YES", "NO"][f])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST STRING STRING VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
import sys from itertools import accumulate def solve(): n = int(input()) a = [int(i) for i in input().split()] S = sum(a) if S & 1: print("NO") return S //= 2 ap = set() ps = [0] + list(accumulate(a)) for i in range(1, n + 1): x = ps[i] - S ap.add(a[i - 1]) if x == 0 or x in ap: print("YES") return ap = set() a = a[::-1] ps = [0] + list(accumulate(a)) for i in range(1, n + 1): x = ps[i] - S ap.add(a[i - 1]) if x == 0 or x in ap: print("YES") return print("NO") solve()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
def mp(): return list(map(int, input().split())) def lt(): return list(map(int, input().split())) def pt(x): print(x) def ip(): return input() def it(): return int(input()) def sl(x): return [t for t in x] def spl(x): return x.split() def aj(liste, item): liste.append(item) def bin(x): return "{0:b}".format(x) def listring(l): return " ".join([str(x) for x in l]) def ptlist(l): print(" ".join([str(x) for x in l])) n = it() a = lt() k = 0 left = 0 right = sum(a) bl = False dict = {} for i in range(n): if a[i] in dict: ma, mi = dict[a[i]] dict[a[i]] = max(ma, i), min(mi, i) else: dict[a[i]] = i, i while k < n and not bl: if right == left: bl = True elif right > left: x = (right - left) / 2 if x == int(x) and int(x) in dict and dict[int(x)][0] >= k: bl = True else: right -= a[k] left += a[k] k += 1 else: x = (left - right) / 2 if x == int(x) and int(x) in dict and dict[int(x)][1] <= k - 1: bl = True else: right -= a[k] left += a[k] k += 1 if bl: pt("YES") else: pt("NO")
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL STRING VAR FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR WHILE VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) prefix_sum = [a[0]] postfix_sum = [a[-1]] for i in range(1, n): prefix_sum.append(prefix_sum[-1] + a[i]) postfix_sum.append(postfix_sum[-1] + a[-(i + 1)]) if prefix_sum[-1] & 1 or n == 1: print("NO") sys.exit() target = prefix_sum[-1] // 2 if target in prefix_sum: print("YES") sys.exit() seen_pre = {a[0]} seen_post = {a[-1]} for i in range(1, n): post_i = -(i + 1) seen_pre.add(a[i]) seen_post.add(a[post_i]) if prefix_sum[i] > target and prefix_sum[i] - target in seen_pre: print("YES") sys.exit() if postfix_sum[i] > target and postfix_sum[i] - target in seen_post: print("YES") sys.exit() print("NO")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) a = list(map(int, input().split())) e = set([0]) p = 0 s = sum(a) if s % 2: st = False else: st = True if st: st = False for j in range(2): for i in a: p += i e.add(i) if p - s // 2 in e: st = True break e = set([0]) p = 0 a.reverse() print("YES" if st else "NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING STRING
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? -----Input----- The first line contains single integer n (1 ≀ n ≀ 100000) β€” the size of the array. The second line contains n integers a_1, a_2... a_{n} (1 ≀ a_{i} ≀ 10^9) β€” the elements of the array. -----Output----- Print YES if Vasya can divide the array after moving one element. Otherwise print NO. -----Examples----- Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES -----Note----- In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left.
n = int(input()) a = list(map(int, input().split())) rev_a = a[::-1] def main(): bag = set() target = sum(a) / 2 s = 0 for i in a: bag.add(i) s += i if s == target: print("YES") return if s > target and s - target in bag: print("YES") return bag = set() s = 0 for i in rev_a: bag.add(i) s += i if s == target: print("YES") return if s > target and s - target in bag: print("YES") return print("NO") main()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR