Skip to content

Instantly share code, notes, and snippets.

@frectonz
Created January 2, 2023 11:06
Show Gist options
  • Save frectonz/5e74607c83c0e1f0a6ef793e60411dac to your computer and use it in GitHub Desktop.
Save frectonz/5e74607c83c0e1f0a6ef793e60411dac to your computer and use it in GitHub Desktop.
From cassido's January 2, 2023 Newsletter
package main
import "fmt"
func main() {
res1 := maxSubArray([]int{-4, 2, -5, 1, 2, 3, 6, -5, 1}, 4)
fmt.Println(res1)
res2 := maxSubArray([]int{1, 2, 0, 5}, 2)
fmt.Println(res2)
}
func maxSubArray(list []int, size int) []int {
var maxSum int
var maxSubArray []int
for i := 0; i <= len(list)-size; i++ {
currentSubArray := list[i : i+size]
var sum int
for _, n := range currentSubArray {
sum += n
}
if sum > maxSum {
maxSum = sum
maxSubArray = currentSubArray
}
}
return maxSubArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment