• + 0 comments

    Solution in go

    func fairRations(B []int32) string {
        oddIndex := -1
        totalBreads := int32(0)
        
        for i := 0; i < len(B); i++ {
            if B[i]%2 > 0 {
                if oddIndex < 0 {
                    oddIndex = i
                    continue
                }
                
                totalBreads += int32((i - oddIndex) * 2)
                oddIndex = -1
            }
        }
        if oddIndex >= 0 {
            return "NO"
        }
        
        return fmt.Sprintf("%d", totalBreads)
    }