Skip to content

Commit e173088

Browse files
committed
largest contiguous subarray
1 parent 18c65fc commit e173088

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Array/largest_contiguous_subarray.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def largestContiguous(arr):
2+
3+
max_so_far = 0
4+
max_ending_here = 0
5+
6+
for i in range(len(arr)):
7+
8+
max_ending_here = max_ending_here + arr[i]
9+
if max_ending_here < 0:
10+
max_ending_here = 0
11+
if max_ending_here > max_so_far:
12+
max_so_far = max_ending_here
13+
return max_so_far
14+
15+
16+
arr1 = [-2, 3, -5, 6, 2]
17+
arr2 = [2, -3, 4, -1, -2, 1, 5, -3]
18+
print("the largest continuous sub-array is :", largestContiguous(arr2))

0 commit comments

Comments
 (0)