Hướng dẫn max contiguous subarray python assignment - phép gán python phân mảng con liền kề tối đa
Subarray tối đa tiếp giáp Show Đưa ra một danh sách các số nguyên, hãy viết một chương trình để xác định danh sách phụ tiếp giáp có số tiền lớn nhất và in tổng. Bất kỳ lát cắt không trống nào của danh sách với kích thước bước 1 có thể được coi là một danh sách phụ liên tục. Đầu vào sẽ chứa các số nguyên được phân tách không gian, biểu thị các phần tử của danh sách. Đầu ra phải là một số nguyên. Ví dụ: nếu danh sách đã cho là [2, -4, 5, -1, 2, -3], thì tất cả các danh sách phụ tiếp giáp có thể có,
Trong số các danh sách phụ tiếp giáp ở trên, danh sách phụ tiếp giáp [5, -1, 2] có số tiền lớn nhất là 6. Đầu vào mẫu 1 2 -4 5 -1 2 -3 Đầu ra mẫu 1 6 Đầu vào mẫu 2 -2 -3 4 -1 -2 1 5 -3 Đầu ra mẫu 2 7 8myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 81 89Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 43 5 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 45__222222 now, possible contiguous lists are: [1] [1, 6] [1, 6, -7] [1, 6, -7, 5] [6] [6, -7] [6, -7, 5] [-7] [-7, 5] [5] 05 5 07 08 09 10Để in Subarray với số tiền tối đa, chúng tôi duy trì các chỉ số bất cứ khi nào chúng tôi nhận được số tiền tối đa. & NBSP; & NBSP; myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max) 0 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)24 2 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)26 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)33 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)5 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)41 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)45 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9
9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)49 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 9 1 2 3 4 5myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 77 8myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 2 5 3 8 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)67 6 7myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)41 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)75 7myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)45 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)79 Đầu ra mẫu 2 7
Viết một chương trình hiệu quả để tìm tổng số subarray liên tục trong một mảng một chiều của các số có số tiền lớn nhất. & NBSP; Thuật toán Kadane từ: Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far Giải thích: & nbsp; Ý tưởng đơn giản của thuật toán Kadane, là tìm kiếm tất cả các phân đoạn tiếp giáp tích cực của mảng (MAX_IVED_HERE được sử dụng cho việc này). Và theo dõi phân đoạn liên tục tổng tối đa trong số tất cả các phân đoạn dương (MAX_SO_FAR được sử dụng cho việc này). Mỗi lần chúng tôi nhận được một tổng dương so sánh nó với MAX_SO_FAR và cập nhật MAX_SO_FAR nếu nó lớn hơn MAX_SO_FAR & NBSP; Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 4 Program: Python3 0 1 2 3 4 5 3 7 8 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)3 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)5 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 9 1 2 3 4 5myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 7 8myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 2 3 8 5 6 7myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far0 8 5 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far3 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far5 7myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 ________ 29 & nbsp; & nbsp; 9Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 41 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 42 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 43 5 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 45 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 47 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 70 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 73 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 76 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 70 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 72 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 75 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 78 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 61 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 64 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 67 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 00Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 03 04 05 5 07____108 09 10Output: Maximum contiguous sum is 7 Cách tiếp cận khác: Python3 7 8 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)3 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)5 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 9 1 2 3 4 5myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 7 8myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 2 3 8 5 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far3 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far5 7myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 ________ 29 & nbsp; & nbsp; 9Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 41 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 42 7myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far0 9Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 41 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 42
Python3 7 8 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 16myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 04 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)3 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)5 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 9 1 2 3 4 5myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 7 8myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 2 3 9Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 41 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 42 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 43 5 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 45 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 47 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 70 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 73 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 76 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 70 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 72 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 75 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 78 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 61 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 64 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 67 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 00Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 03 04 05 5 07____108 09 10Output: Maximum contiguous sum is 7 Cách tiếp cận khác: Python3 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 16myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 04 7 8 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)3 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)5 9myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 9 1 2 3 4 5myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 7 8myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 2 3 8 5 6 7myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far0 8 5 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far3 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far5 7myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 ________ 29 & nbsp; & nbsp; 7myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)0 5 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far0 8 5 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far3 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far5 7myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 ________ 29 & nbsp; & nbsp; 8 5 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far3 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)9 Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_far < max_ending_here) max_so_far = max_ending_here (c) if(max_ending_here < 0) max_ending_here = 0 return max_so_far5 7myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)7 5 ________ 29 & nbsp; & nbsp; 9Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 41 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 42 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 43 5 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 45 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 47 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 70 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 73 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 76 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 70 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 72 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 75 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 78 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 61 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 64 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 67 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 00Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 03 04 05 5 07____108 09 10Cách tiếp cận khác: Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 43 5 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 45 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 47 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 70 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 73 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 76 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 70 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 72 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 75 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 78 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 61 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 64 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 Maximum contiguous sum is 7 Starting index 2 Ending index 67 Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 00Lets take the example: {-2, -3, 4, -1, -2, 1, 5, -3} max_so_far = max_ending_here = 0 for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0 for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0 for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till now for i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3 for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1 for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2 for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_far for i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 48 myList = [-1, 2, 4, -7, 5, -9] listOfSubLists = [] for i in range(len(myList)+1): for j in range(i+1, len(myList)+1): listOfSubLists.append(myList[i:j]) listOfAllSum = [] for i in listOfSubLists: listOfAllSum.append(sum(i)) max = -1 index = 0 for i in range(len(listOfAllSum)): if listOfAllSum[i]>max: max = listOfAllSum[i] index = i print('List with maximum sum is: ', listOfSubLists[index]) print('Maximum sum is: ', max)2 03 04 37 09 39Output: Maximum contiguous sum is 7 Starting index 2 Ending index 6 05 5 07____108 09 10Độ phức tạp về thời gian: O (n)O(n) Không gian phụ trợ: O (1)O(1) Bây giờ hãy thử câu hỏi dưới đây & nbsp; đưa ra một loạt các số nguyên (có thể là một số yếu tố âm), hãy viết một chương trình C để tìm ra * sản phẩm tối đa * có thể bằng cách nhân các số nguyên liên tiếp trong mảng trong đó n ≤ mảng_size.Ngoài ra, in điểm bắt đầu của sản phẩm tối đa Subarray. Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên. |