awelgorithms

Read the pair at both pointers

Updated

Reading the pair looks at a[L] and a[R] without moving. The pair is two values and two indices. You do not write.

What to focus on

L and R must both be in range. If L == R you are reading one cell twice, only legal if the problem allows a self-pair.

Worked examples

opposite

a = [4, 1, 9], L=0, R=2
  1. L=0, R=2
  2. read a[0]=4 and a[2]=9

Result: (4, 9) at (0, 2)

adjacent

a = [4, 1, 9], L=0, R=1
  1. L=0, R=1
  2. read 4 and 1

Result: (4, 1) at (0, 1)

empty

a = []
  1. no in-range L or R
  2. do not invent a pair

Result: no pair

Try it

Write readPair(a, L, R). a is an array of integers. L is the left index. R is the right index. Print values and indices. Do not move the pointers. Return nothing. On [4, 1, 9] at (0, 2) print (4, 9). At (0, 1) print (4, 1). At (1, 1) print (1, 1).

a may be empty. L and R are in-range indices.