awelgorithms

Give each sequence its own pointer

Updated

Two sequences get two independent indices: i on a, j on b. They are not L and R on one array. Each pointer only reads its own sequence.

What to focus on

i is legal only for a (0..a.length-1). j is legal only for b. Moving i never changes j. A shorter partner still starts at 0.

Worked examples

both nonempty

a = [1, 4, 9], b = [4, 7]
  1. i ← 0 on a
  2. j ← 0 on b
  3. heads 1 and 4

Result: i=0, j=0

b shorter

a = [1, 4, 9], b = [4]
  1. i ← 0
  2. j ← 0
  3. b has no index 1

Result: i=0, j=0 · b last is 0

empty b

a = [1, 4, 9], b = []
  1. i ← 0 is legal
  2. j has nowhere to sit

Result: i=0, no j

Try it

Write placeTwoSeq(a, b). a is an array of integers. b is a second array of integers. i is the index on a. j is the index on b. Set i = 0 and j = 0. Print i and j. Each pointer stays on its own array. On a = [1, 4, 9] with b = [4, 7], with b = [4], and a = [1] with b = [4, 7, 9], print i=0, j=0.

a and b are nonempty arrays.