# Two Pointers Technique

Two pointer is really an easy and effective technique which is typically used for searching pairs in a sorted arrays.

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**.

`// Naive solution to find if there is a ` `// pair in A[0..N-1] with given sum. ` ` ` `bool` `isPairSum(A[], N, X) ` `{ ` ` ` `for` `(i = 0; i < N; i++) { ` ` ` `for` `(j = 0; j < N; j++) { ` ` ` `if` `(A[i] + A[j] == X) ` ` ` `return` `true` `; ` `// pair exists ` ` ` ` ` `if` `(A[i] + A[j] > X) ` ` ` `break` `; ` `// as the array is sorted ` ` ` `} ` ` ` `} ` ` ` ` ` `// No pair found with given sum. ` ` ` `return` `false` `; ` `} ` |

*chevron_right*

*filter_none*

The time complexity of this solution is **O(n ^{2})**.

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.

`// Two pointer technique based solution to find ` `// if there is a pair in A[0..N-1] with given sum. ` `bool` `isPairSum(A[], N, X) ` `{ ` ` ` `// represents first pointer ` ` ` `int` `i = 0; ` ` ` ` ` `// represents second pointer ` ` ` `int` `j = N - 1; ` ` ` ` ` `while` `(i < j) { ` ` ` ` ` `// If we find a pair ` ` ` `if` `(A[i] + A[j] == X) ` ` ` `return` `true` `; ` ` ` ` ` `// If sum of elements at current ` ` ` `// pointers is less, we move towards ` ` ` `// higher values by doing i++ ` ` ` `else` `if` `(A[i] + A[j] < X) ` ` ` `i++; ` ` ` ` ` `// If sum of elements at current ` ` ` `// pointers is more, we move towards ` ` ` `// lower values by doing i++ ` ` ` `else` ` ` `j--; ` ` ` `} ` ` ` `return` `false` `; ` `} ` |

*chevron_right*

*filter_none*

Illustration :

The above solution works in **O(n)**

**How does this work?**

The algorithm basically uses the fact that input array is sorted. We start sum of extreme values (smallest and largest) and conditionally move both pointers. We move left pointer i when sum of A[i] and A[j[ is less than X. We do not miss any pair because 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

## Recommended Posts:

- Window Sliding Technique
- C++ Program to compare two string using pointers
- Program to reverse an array using pointers
- Repeated Character Whose First Appearance is Leftmost
- Queries for bitwise AND in the index range [L, R] of the given array
- Find number from its divisors
- Queries for bitwise OR in the index range [L, R] of the given array
- Minimum steps required to reach the end of a matrix | Set 2
- Find the index which is the last to be reduced to zero after performing a given operation
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Minimize the number of steps required to reach the end of the array | Set 2
- Count index pairs which satisfy the given condition
- Maximum sum such that no two elements are adjacent | Set 2
- Minimize the number of steps required to reach the end of the 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 contribute@geeksforgeeks.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.