Javatpoint Logo
Javatpoint Logo

Two Pointers Technique


Developers and computer scientists are constantly searching for effective strategies to optimise their code in the world of problem-solving and algorithmic challenges. They have a number of potent weapons, including the "Two Pointers Technique." Due to its success in resolving a variety of issues involving arrays or linked lists, this technique has become extremely popular.

Understanding the Two Pointers Technique

The Two Pointers Technique is a straightforward but effective algorithmic technique that uses two pointers to traverse an array or linked list at the same time. The method is particularly helpful for finding pairs, subarrays, or sequences that meet specific requirements in order to solve problems. We can efficiently explore the data structure and find solutions with less time complexity by using two pointers, one moving forward and the other moving backward or both moving in different directions.

How the Two Pointers Technique Works

The Two Pointers Technique's fundamental idea is to initialise two pointers in various locations and then manipulate their motions in accordance with predetermined criteria. In most cases, this entails changing the pointers' locations according to whether the present elements or values satisfy the desired conditions.

Step 1: Sorting (if needed)

In some circumstances, sorting the input data is necessary before using the Two Pointers Technique. We can quickly spot trends and quickly locate the desired elements by sorting the data.

Step 2: Initialization

Two pointers, commonly referred to as "left" and "right," that point to various places in the data structure are initialised. These pointers' initial positions are selected based on the specifications of the problem.

Step 3: Moving the Pointers

The pointers are then moved in accordance with predetermined guidelines or conditions. Depending on the type of the issue, these rules might involve incrementing or decrementing the pointers.

Step 4: Assessing the elements

At each step, we assess the objects or values the two pointers point to to see if they meet the requirements of the problem.

Step 5: Finding the Solution

The desired answer or the necessary subarray, pair, or sequence that satisfies the requirements of the problem is eventually discovered as the pointers are moved through the data structure.

Applications of the Two Pointers Technique

The Two Pointers Technique is useful in a variety of situations where problems need to be solved. This method excels when used to solve the following typical issues:

1. Two Sum Problem

The Two Pointers Technique can quickly locate two numbers whose sum is equal to the target given an array of integers and the target value.

2. Three Sum Problem

The goal of this puzzle is to identify all triplets that are distinct and add up to the specified target value in the array.

3. Merge Two Sorted Lists

With minimal time complexity, the two sorted linked lists can be combined using the two pointers technique.

4. Removing Duplicates

The Two Pointers Technique can eliminate duplicates in-place with O(1) extra space when used with sorted arrays.

5. Finding Palindromic Substrings

The method effectively finds all palindromic substrings for a given string.

Code in Python:


Input Array: [2, 7, 11, 15]
Target: 9
Indices of the two numbers: [0, 1]
Numbers: 2 and 7 add up to 9

The code is explained as follows:

  • Two arguments are required for the two_sum function: target (the desired sum) and nums (the input array of numbers).
  • In order to make the Two Pointers technique easier to use, the input array numbers are sorted in ascending order.
  • The left and right pointers are initialized. The left and right arrows indicate the start and finish of the array, respectively.
  • As long as the left pointer is smaller than the right pointer, a while loop is executed.
  • The total of the numbers at the left and right pointers is determined and saved in the current_sum variable for each iteration of the loop.
  • The two numbers at the current pointers add up to the target if current_sum equals the target. The indices of these two numbers are returned as a list by the function in this instance.
  • The left pointer is increased to take larger values into consideration if current_sum is less than the goal.
  • The correct pointer is decremented to take smaller values into account if current_sum exceeds the target.
  • To indicate that no solution was found, the function returns an empty list if the loop ends without locating a valid pair.

Enhancing Time Complexity

Time complexity is a crucial parameter in algorithm design because it quantifies how long an algorithm takes to run as the size of the input increases. When compared to naive or brute-force methods, developers frequently achieve better time complexity using the Two Pointers Technique. This improvement is especially significant for large datasets because it can improve performance significantly by reducing time complexity.

Memory Performance

The Two Pointers Technique is a desirable choice in situations where memory usage is a concern because it also offers memory efficiency. The method does not require additional memory overhead because it manipulates the pointers already present in the data structure, enabling efficient and resource-conscious solutions.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA