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

Đư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ó,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]

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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


81
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


89

    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
3
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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 = 4
5__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]

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


05
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


07
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


08
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


09
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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]

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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]
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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]
41
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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]
45
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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]
49
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
1
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
2
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
3
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
4
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


77

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


2
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


3

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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]
67

6

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
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]
41
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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]
75

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
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]
45
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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]
79

Đầu ra mẫu 2

7

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 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; 
The simple idea of Kadane’s algorithm is to look for all positive contiguous segments of the array [max_ending_here is used for this]. And keep track of maximum sum contiguous segment among all positive segments [max_so_far is used for this]. Each time we get a positive-sum compare it with max_so_far and update max_so_far if it is greater than max_so_far 

    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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


0
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


1
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


2
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


3

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


4
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


3

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


8

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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]
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
1
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
2
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
3
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
4
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
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
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
7

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


2
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


3

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


6

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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_far
0

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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_far
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]
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_far
5

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5 ________ 29 & nbsp; & nbsp;

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
    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
1
    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
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 = 4
3
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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 = 4
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
    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
7
    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
8
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
0
    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
8
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
3
    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
8
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
6
    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
8
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
0
    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
8
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
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 = 4
8
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
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 = 4
8
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
8
    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
8
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 6
1
    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
8
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 6
4
    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
8
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 6
7
    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
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


00
    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
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


03
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


04

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


05
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


07____108
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


09
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


10

Output:

Maximum contiguous sum is 7

Cách tiếp cận khác:

Python3

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


8

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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]
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
1
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
2
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
3
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
4
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
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
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
7

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


2
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


3

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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_far
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]
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_far
5

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5 ________ 29 & nbsp; & nbsp;

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
    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
1
    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
2

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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_far
0

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
    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
1
    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
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 = 4
3
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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 = 4
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
    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
7
    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
8
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
0
    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
8
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
3
    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
8
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
6
    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
8
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
0
    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
8
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
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 = 4
8
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
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 = 4
8
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
8
    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
8
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 6
1
    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
8
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 6
4
    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
8
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 6
7
    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
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


00
    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
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


03
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


04
O[n] 

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


05
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


07____108
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


09
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


10
Dynamic Programming
Following is another simple implementation suggested by Mohit Kumar. The implementation handles the case when all numbers in the array are negative. 

Python3

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


8

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


16
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


04

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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]
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
1
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
2
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
3
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
4
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
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
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
7

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


2
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


3

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
    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
1
    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
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 = 4
3
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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 = 4
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
    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
7
    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
8
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
0
    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
8
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
3
    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
8
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
6
    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
8
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
0
    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
8
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
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 = 4
8
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
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 = 4
8
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
8
    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
8
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 6
1
    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
8
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 6
4
    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
8
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 6
7
    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
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


00
    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
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


03
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


04

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


05
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


07____108
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


09
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


10

Output: 

Maximum contiguous sum is 7

Cách tiếp cận khác:

Python3

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


16
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


04

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


8

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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]
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
1
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
2
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
3
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
4
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
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
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
7

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


2
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


3

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


6

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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_far
0

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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_far
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]
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_far
5

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5 ________ 29 & nbsp; & nbsp;

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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_far
0

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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_far
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]
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_far
5

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5 ________ 29 & nbsp; & nbsp;

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
8
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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_far
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]
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_far
5

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


7
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


5 ________ 29 & nbsp; & nbsp;

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


9
    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
1
    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
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 = 4
3
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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 = 4
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
    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
7
    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
8
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
0
    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
8
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
3
    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
8
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
6
    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
8
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
0
    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
8
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
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 = 4
8
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
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 = 4
8
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
8
    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
8
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 6
1
    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
8
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 6
4
    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
8
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 6
7
    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
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


00
    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
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


03
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


04

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


05
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


07____108
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


09
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


10

Cá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 = 4
3
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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 = 4
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
    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
7
    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
8
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
0
    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
8
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
3
    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
8
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
6
    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
8
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
0
    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
8
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
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 = 4
8
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
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 = 4
8
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
8
    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
8
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 6
1
    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
8
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 6
4
    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
8
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 6
7
    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
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


00
    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
8
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
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


03
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


04

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
37
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


09
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
39

Output: 

Maximum contiguous sum is 7
Starting index 2
Ending index 6

Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


05
[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[2, -4, 5, -1, 2, -3]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[-4, 5, -1, 2, -3]
[5]
[5, -1]
[5, -1, 2]
[5, -1, 2, -3]
[-1]
[-1, 2]
[-1, 2, -3]
[2]
[2, -3]
[-3]
5
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


07____108
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


09
Max Contiguous Subarray
Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.
Input

The input will contain space-separated integers, denoting the elements of the list.
Output

The output should be an integer.
Explanation

For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,

[2]
[2, -4]
[2, -4, 5]
[2, -4, 5, -1]
[2, -4, 5, -1, 2]
[-4]
[-4, 5]
[-4, 5, -1]
[-4, 5, -1, 2]
[5]
[5, -1]
[5, -1, 2]
[-1]
[-1, 2]
[2]
Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.
Sample Input 1
2 -4 5 -1 2 -3
Sample Output 1
6

Sample Input 2
-2 -3 4 -1 -2 1 5 -3
Sample Output 2
7


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.
Given an array of integers [possibly some elements negative], write a C program to find out the *maximum product* possible by multiplying ‘n’ consecutive integers in the array where n ≤ ARRAY_SIZE. Also, print the starting point of the maximum product 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.


Bài Viết Liên Quan

Chủ Đề