Find the largest continuous sum | Python

Problem :

Given an array of integers(positive and neg),
Find the largest continuous sum .

Solution : 

def large_cont_sum(arr):
	if len(arr) < 1 :
		return 0
	max_sum = current_sum = arr[0]

	for num in arr[1:]:

		current_sum = max(num,current_sum+num)
		max_sum = max(current_sum,max_sum)

	print max_sum

large_cont_sum([1,2,-1,3,4,10,10,-10,-1]) #29

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s