Skip to content
Back to libraryPart 2 · Core techniques

2.2Two pointers

Before you start

It helps to have read 1.5 Sorting and 2.1 Prefix sums. Overall level: Core.

Practice: Two pointers

0 of 4 solved

About these problems

Apply, Connect, Reason and Stretch describe difficulty within this topic, not an official contest rating. Open any problem in any order; reading the chapter first is optional.

  1. 1Target pairYou are given a target x and a list of n integers a1 ≤ a2 ≤ ... ≤ an in nondecreasing order. Determine whether there are two different positions i ≠ j with ai + aj = x. Print YES if such a pair exists, otherwise print NO.ApplyNot started
  2. 2Longest affordable runYou are given a limit k and a list of n positive integers a1, a2, ..., an. Find the greatest length of a block of consecutive elements whose sum is at most k. Print 0 if no nonempty block fits.ConnectNot started
  3. 3Count affordable runsYou are given a limit k and a list of n positive integers a1, a2, ..., an. Count all nonempty blocks of consecutive elements whose sum is at most k. Blocks at different positions count separately, even if they hold the same values.ReasonNot started
  4. 4At most k signalsYou are given a list of n integers a1, a2, ..., an. Find the length of the longest block of consecutive elements that contains at most k distinct values.StretchNot started

Read the lesson