Two Pointer Approach

 

 
The two-pointer approach is a commonly used technique in C++ (and other programming languages) for solving problems involving arrays or linked lists. It involves using two pointers that traverse the data structure simultaneously, usually from different starting positions or at different speeds, to achieve a desired outcome efficiently.
In the case of arrays, the two pointers are often initialized to the start and end of the array, respectively. They can then be moved towards each other until they meet or cross each other, depending on the specific problem.
This approach is particularly useful when dealing with sorted or partially sorted arrays, as it allows us to efficiently search for a target value, find pairs with a specific sum, or identify a subarray with a given property.
For example, to find a pair of elements in a sorted array that adds up to a specific target sum, we can initialize two pointers, one pointing to the beginning of the array and the other to the end. We then compare the sum of the elements at these pointers with the target sum. If the sum is greater than the target, we decrement the end pointer. If the sum is smaller, we increment the start pointer. By repeating this process until we find the desired pair or until the pointers cross each other, we can efficiently find the solution in linear time complexity.
Similarly, the two-pointer approach can be applied to linked lists by using two pointers that move at different speeds or by maintaining a fixed distance between them. This technique is often employed to detect cycles in a linked list, find the middle node, or perform operations on the list in reverse order.
Overall, the two-pointer approach is a powerful technique for solving various array and linked list problems, as it allows for efficient traversal and comparison of elements by utilizing multiple pointers simultaneously.
 
In C++, the two pointer approach can be implemented using a pair of pointers or pointer-like iterators that reference the beginning and end of the sequence, respectively. Here's an example code that illustrates the two pointer approach for finding pairs of integers in a sorted array that add up to a target value:
 
#include <iostream> #include <vector> using namespace std; vector<pair<int, int>> twoSum(vector<int>& nums, int target) { vector<pair<int, int>> result; int left = 0; int right = nums.size() - 1; while (left < right) { int sum = nums[left] + nums[right]; if (sum == target) { result.push_back({left, right}); left++; right--; } else if (sum < target) { left++; } else { right--; } } return result; } int main() { vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int target = 7; vector<pair<int, int>> result = twoSum(nums, target); cout << "Pairs in nums that add up to " << target << ": "; for (auto [i, j] : result) { cout << "(" << nums[i] << ", " << nums[j] << ") "; } return 0; }
 
In this code, the twoSum function takes a reference to a vector of integers nums and an integer target as inputs, and returns a vector of pairs of integers that add up to target.
The two pointers left and right start at the beginning and end of the nums sequence, respectively.
In the while loop, the pointers move towards each other until they meet. At each iteration, the sum of the integers at their current positions is calculated and compared to the target value.
If the sum is equal to the target, the pair (left, right) is added to the result vector, and both pointers are moved forward and backward, respectively, to search for other pairs.
If the sum is less than the target, the left pointer is moved forward since increasing the left integer would increase the sum closer to the target.
If the sum is greater than the target, then the right pointer is moved backward since decreasing the right integer would decrease the sum closer to the target.
At the end of the function, the result vector is returned, and the main function prints out the pairs that add up to the target.
 
 
How is Two - Pointer approach beneficial ?
The two pointer approach is a faster and more efficient method than traditional methods when searching or sorting arrays or other sequence data structures.
In the example code provided above, the two pointer approach is used to find pairs of integers in a sorted array that add up to a target value. The two pointers start at opposite ends of the array and are moved towards each other until they meet.
At each iteration of the loop, the sum of the integers at the current positions of the pointers is computed, and the result is compared to the target value. If the sum is equal to the target, a pair of indices is stored in the result vector. If the sum is less than the target, the left pointer is incremented, since increasing the value of the integer at the left index would increase the sum. If the sum is greater than the target, the right pointer is decremented, since decreasing the value of the integer at the right index would decrease the sum.
This approach uses only one loop, resulting in a time complexity of O(n), where n is the size of the array. The traditional method to solve this problem would require an additional nested for loop, which would bring the time complexity to O(n^2). Thus, the two pointer approach is more efficient in terms of time complexity and hence faster than the traditional approach.
This method can also be extended to other sequence data structures like linked lists, where two pointers are used to traverse the linked list simultaneously. By operating on two elements at a time, the time requirements are significantly reduced over the traditional techniques that operate on one element at a time.
Overall, the advantage of using the two pointer approach is that it performs operations in linear time, improving the algorithm's efficiency and reducing the number of iterations the program requires to execute. Thus, for problems that require searching or sorting through an array or other sequence data structures, the two pointer approach is an effective method of optimizing performance and reducing time complexity.
 
 
PRACTICE PROBLEMS :
  1. Sort Colors
  1. Two Sum II - Input Array Is Sorted