Find Largest sum of contiguous Subarray in Python

prepinsta-prime

Get over 200+ Courses under One Subscription

mute

Don’t settle Learn from the Best with PrepInsta Prime Subscription

Learn from Top 1%

One Subscription, For Everything

The new cool way of learning and upskilling -

Limitless Learning

One Subscription access everything

Job Assistance

Get Access to PrepInsta Prime

Top Faculty

from FAANG/IITs/TOP MNC's

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

2 comments on “Find Largest sum of contiguous Subarray in Python”


  • Manisha

    # Python program to find maximum contiguous subarray

    # Function to find the maximum contiguous subarray
    from math import inf
    maxint=inf
    def maxSubArraySum(a,size):

    max_so_far = -maxint – 1
    max_ending_here = 0

    for i in range(0, size):
    max_ending_here = max_ending_here + a[i]
    if (max_so_far < max_ending_here):
    max_so_far = max_ending_here

    if max_ending_here < 0:
    max_ending_here = 0
    return max_so_far

    # Driver function to check the above function
    a = [-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7]
    print ("Maximum contiguous sum is", maxSubArraySum(a,len(a)))

    #This code is contributed by _Devesh Agrawal_


  • md138halima

    —python—
    import numpy as np
    arr=np.array([-2,-2,4,-1,-2,1,5,-3])

    max=float(‘-inf’)
    for i in range(0,len(arr)):
    for j in range(i+1,len(arr)):
    sub_arr=arr[i:j]
    s=np.sum(sub_arr)
    if(s>max):
    max=s
    print(max)