Identifying two pointer subproblem


Some problems may consist of smaller subproblems that can be solved using the two-pointer technique. Solving these subproblems may either partially or fully solve the original problem. These are usually medium or hard problems, as breaking down a problem into subproblems may not be obvious and may require some critical observation. Sometimes, the subproblems themselves may not be solvable by directly applying the two-pointer technique and may require further reduction to a two-pointer pattern problem.

Asking yourself the following questions will help you determine whether a problem is a two-pointer subproblem pattern problem or not.

Ask yourself questions:

Q1. Can the problem or solution be broken down into smaller subproblems?
Q2. Can any subproblem be solved using the two-pointer technique?

Example

Let's consider an example problem and see how to break it down into smaller subproblems that are solvable using the two-pointer technique to understand it better.

Problem statement: We are given an array `arr` of integers and an integer value `k`. We must rotate the array `k` times to the left.

Consider the following example with k = 4 for an array of size 8.

Loading Image

Rotate an array k times to the left where k = 4

Brute force solution

The brute-force solution is to create another array and copy items from the original array to the new one at k-shifted indices. Then, we can copy the items from the new array to the original one. Consider the following example, where we rotate an array of size 8 four times to the left.

Loading Player

1 of 30

Rotate array 4 times to the left using temporary array

The implementation of the brute force solution is given as follows.

  1. C++

  2. Java

  3. Typescript

  4. Javascript

  5. Python

Though the solution is correct, an additional array must be created, which has a space complexity of O(N) in any case. Let's now see if this problem can be broken down into smaller subproblems that can be solved using the two-pointer technique.

Two pointer subproblem

Let's ask ourselves the questions we listed above to identify if this problem has subproblems that can be solved by the two-pointer technique.

Ask yourself questions:

Q1. Can the problem or solution be broken down into smaller subproblems?
A1. Yes, we can break down the solution as a combination of 3 in-place reversals.

Q2. Can any subproblem be solved using the two-pointer technique?
A2. Yes, we can solve all subproblems (in-place reversals) using the two-pointer technique.

The critical observation here is that a rotation can be viewed as a sequence of three reversal operations. We reverse the first k element, then the remaining N-k items, and finally, we reverse the entire array where N is the array's size.

Loading Image

We can shift items k times to the left by combining three in-place reversal operations.

The two-pointer technique can solve all the subproblems (reversals) of the given problem. The implementation of the two-pointer solution is given below.

  1. C++

  2. Java

  3. Typescript

  4. Javascript

  5. Python

The process above summarizes how we can identify a problem that can be broken down into smaller subproblems solvable by the two-pointer technique and how to aggregate the results of those subproblems into the final solution.

Example problems

Most problems in this category are medium or hard problems, as identifying the subproblems may be difficult. Also, sometimes, the problem or the subproblem may need to be reduced to a two-pointer problem, which can be difficult. Below is a list of problems in the two-pointer subproblem pattern.

We will now solve these problems to get a better understanding of breaking down a problem into subproblems solvable by the two-pointer technique.

Login to save progress

Was this helpful?