# Codility: MaximumDoubleSliceSum Solution

Lesson 9 Maximum Slice Problem

## Approach

(X, Y, Z): X + 1, Z - 1, and skip Y

Maintain 2 kadane prefix, one for ending at (normal), one for starting at (reversed)

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}function solution(A) {2  const N = A.length3  const maxEndingAt = Array(N).fill(0)4  const maxStartingAt = Array(N).fill(0)5
6  for (let i = 1; i < N - 1; i++) {7    maxEndingAt[i] = Math.max(0, maxEndingAt[i - 1] + A[i])8  }9  for (let i = N - 2; i > 0; i--) {10    maxStartingAt[i] = Math.max(0, A[i] + maxStartingAt[i + 1])11  }12
13  let max = -Infinity14  for (let i = 1; i < N - 1; i++) {15    max = Math.max(max, maxEndingAt[i - 1] + maxStartingAt[i + 1])16  }17  return max18}```

codility

## Next Post

Codility: MaxSliceSum

Jan 25, 2021

Lesson 9 Maximum Slice Problem

## Previous Post

Codility: Dominator

Jan 24, 2021