Registrations for the November 2021 System Design Cohort are now open Enroll Today

MAX-SUM-CONTIGUOUS-SUBARRAY Solution



package main

import (
	"fmt"
)

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func maxSubArray(A []int) int {
	currentMax := A[0]
	globalMax := A[0]
	for i := 1; i < len(A); i++ {
		currentMax = max(A[i], currentMax+A[i])
		globalMax = max(globalMax, currentMax)
	}
	return globalMax
}

func main() {
	fmt.Println(maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}))
}

470+ Competitive Programming Solutions

If you like what you read subscribe to my newsletter.

I write essays on various engineering topics and share it through my weekly newsletter 👇