awelgorithms

Start both pointers on the same cell

Updated

Starting together means L = R at the same index, usually 0. The pair is one cell until one pointer moves. Same-direction scans grow a gap from this zero-gap start.

What to focus on

After init, L == R and that index is in range. Reading a[L] and a[R] is the same slot twice until they split.

Worked examples

start 0

a = [4, 1, 9]
  1. L ← 0
  2. R ← 0
  3. both sit on 4

Result: L=0, R=0

only cell

a = [7]
  1. the only index is 0
  2. L and R both land there

Result: L=0, R=0

empty

a = []
  1. no cell to share
  2. do not set L=R=0

Result: no L, R

Try it

Write startTogether(a). a is an array of integers. L and R are indices that share one in-range cell. Set L = R, usually 0. Print L and R. Return nothing. On [4, 1, 9], [4, 1, 9, 1], and [7], both pointers share index 0.

a may be empty. Length ≥ 1.