Two pointers is really an easy and effective technique which is typically used for searching pairs in a sorted array.
Given a sorted array A (sorted in ascending order), having N integers, find if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X.
Let’s see the naive solution.
Time Complexity: O(n2).
Now let’s see how the two-pointer technique works. We take two pointers, one representing the first element and other representing the last element of the array, and then we add the values kept at both the pointers. If their sum is smaller than X then we shift the left pointer to right or if their sum is greater than X then we shift the right pointer to left, in order to get closer to the sum. We keep moving the pointers until we get the sum as X.
Time Complexity: O(n)
How does this work?
The algorithm basically uses the fact that the input array is sorted. We start the sum of extreme values (smallest and largest) and conditionally move both pointers. We move left pointer i when the sum of A[i] and A[j] is less than X. We do not miss any pair because the sum is already smaller than X. Same logic applies for right pointer j.
More problems based on two pointer technique.
- Find the closest pair from two sorted arrays
- Find the pair in array whose sum is closest to x
- Find all triplets with zero sum
- Find a triplet that sum to a given value
- Find a triplet such that sum of two equals to third element
- Find four elements that sum to a given value
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Window Sliding Technique
- Parzen Windows density estimation technique
- Encryption and Decryption of String according to given technique
- Improving Linear Search Technique
- C++ Program to compare two string using pointers
- Move all zeroes to end of array using Two-Pointers
- Program to reverse an array using pointers
- Program to implement Separate Chaining in C++ STL without the use of pointers
- Minimum sum of two elements from two arrays such that indexes are not same
- Check if two items can be selected from two different categories without exceeding price
- Add two numbers represented by two arrays
- Median of two sorted arrays of same size
- Maximum sum such that no two elements are adjacent
- Two elements whose sum is closest to zero
- Maximum difference between two elements such that larger element appears after the smaller number
- Union and Intersection of two sorted arrays
- Find the two repeating elements in a given array
- Find the minimum distance between two numbers
- Implement two stacks in an array
- Find the two numbers with odd occurrences in an unsorted array
This article is contributed by Rohan Das. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.