kadane's algorithm gfg code example

Example 1: kadane's algorithm

def kadane(inputArray):
	maxSum = float("-inf")
	curSum = 0
    
	for x in inputArray:
  		curSum = max(0, curSum + x)
  		maxSum = max(maxSum, curSum)
	return maxSum

Example 2: kadane algorithm

public int kadane(int[] arr){
	int max_so_far = 0, curr_max = Integer.MIN_VALUE;
    for(int i: arr){
    	max_so_far += i;
        if(max_so_far<0) max_so_far = 0;
        if(max_so_far>curr_max) curr_max = max_so_far;
    }
    return curr_max;
}

Example 3: Kadane's Algorithm

/*	Code by DEVANSH SINGH
	
    Kadane's Algorithm
	
    Maximum Subarray Sum
*/
from sys import stdin,setrecursionlimit
setrecursionlimit(10**7)

def maxSubarraySum(arr, n) :

    curSum = 0
    preSum = 0
    maxSum = 0
    for i in range(n) :

        if(i == 0) :
            curSum = arr[i]
        
        else :

            curSum = max(arr[i], preSum + arr[i])
        
        preSum = curSum
        maxSum = max(maxSum, curSum)
    
    return maxSum
/*	Code by DEVANSH SINGH
*/

#taking inpit using fast I/O
def takeInput() :
	
    n =  int(input())

    if(n == 0) :
        return list(), n

    arr = list(map(int, stdin.readline().strip().split(" ")))

    return arr, n


#main
arr, n = takeInput()
print(maxSubarraySum(arr, n))
/*	Code by DEVANSH SINGH
*/

/*	Code by DEVANSH SINGH
*/
/*	Code by DEVANSH SINGH
*/