Subarray Division 2

  • + 0 comments

    O(n) prefix sum solution in Golang

    func birthday(s []int32, d int32, m int32) int32 {
        prefixSums := make([]int32, len(s)+1)
        for i, n := range s {
            prefixSums[i+1] = prefixSums[i] + n
        }
        
        var countOfSegments int32
        for i := m; i < int32(len(prefixSums)); i++ {
            if prefixSums[i] - prefixSums[i-m] == d {
                countOfSegments++
            }
        }
        return countOfSegments
    }