awelgorithms

Expand across the whole array

Updated

Expand until R = n-1. Every cell is included. Starting from L=0 R=-1, the window becomes the whole array: L=0, R=n-1.

What to focus on

After this, R cannot grow. The right edge is parked at the last index. Sum of the window equals sum of a.

Worked examples

three cells

a = [4, 1, 9], L=0 R=-1
  1. R=0 include 4
  2. R=1 include 1
  3. R=2 include 9, R=n-1

Result: L=0 R=2 · [4, 1, 9]

four cells

a = [4, 1, 9, 1], L=0 R=-1
  1. expand four times
  2. R stops at 3

Result: L=0 R=3 · [4, 1, 9, 1]

one cell

a = [7], L=0 R=-1
  1. one expand
  2. R=0 = n-1

Result: L=0 R=0 · [7]

Try it

Write expandAll(a). a is an array of integers. Start from L=0, R=-1. Grow R until R = n-1. The window then includes every cell. Print (L, R) and prove R==n-1. Return nothing. On [4, 1, 9], [4, 1, 9, 1], and [7].

a is nonempty. Start from L=0, R=-1.