# JavaScript Program for Two Pointers Technique

JavaScript Program for Two Pointers Technique is a commonly used algorithmic approach to solve various problems that require linear time complexity. This technique is widely used to find a solution for problems that involve searching, sorting, or manipulating arrays, strings, or linked lists. The approach works by maintaining two pointers, one starting from the beginning and the other starting from the end of the data structure, and then iterating through them towards each other until a solution is found.

In this tutorial, we will explore the concept of the Two Pointers Technique and how it can be implemented using the JavaScript programming language. So let’s get started first with the problem statement and then we move further in this interesting tutorial!

## Problem Statement

Given a sorted array A (which is sorted in ascending order), having N integers, examine if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X.

Now let’s understand the of the above program with some examples.

Input: const array = [1, 3, 5, 7, 9];
const X = 12;
Output: Pair found at indices 1 and 4


Explanation − In this case, the pair of elements (3, 9) in the input array adds up to the target sum of 12, and the program correctly identifies the pair at indices 1 and 4.

Input: const array = [1, 3, 5, 7, 9];
const X = 9;


Explanation − In this case, if the target sum is 9, no such pair exists, and the function should pair not found.

## Algorithm

The algorithm for the Two Pointers Technique program to find if there exists any pair of elements in a sorted array whose sum is equal to a given target −

• Initialize two pointers, left = 0 and right = length of array - 1.

• While left < right, do the following

• Calculate the sum of the elements at indices left and right.

• If the sum is equal to the target, return the indices left and right.

• If the sum is less than the target, increment left.

• If the sum is greater than the target, decrement right.

• If no such pair exists, return null.

The above algorithm uses the Two Pointers Technique to search for a pair of elements in a sorted array whose sum equals a given target. The pointers start from opposite ends of the array and move towards each other based on the comparison of the sum of the elements at the pointers with the target. If the sum is less than the target, the left pointer is moved to the right to increase the sum. If the sum is greater than the target, the right pointer is moved to the left to decrease the sum. If the sum is equal to the target, the program returns the indices of the pair of elements. If no such pair exists, the program returns a pair not found.

Now let’s understand this algorithm with an example where we implement the problem statement we discussed earlier using JavaScript.

## Example

In this program, we used the Two Pointers Technique to find whether there exists a pair of elements in a given sorted array whose sum equals a given target. By iterating through the array and moving the pointers based on the sum of the elements at the pointers, the program efficiently finds the pair of elements (if it exists) in O(N) time complexity, where N is the number of elements in the array.

function findSumPair(array, X) {
let left = 0;
let right = array.length - 1;
while (left < right) {
const sum = array[left] + array[right];
if (sum === X) {
console.log(Pair found at indices ${left} and${right});
return [left, right];
} else if (sum < X) {
left++;
} else {
right--;
}
}
return null;
}
const array = [1, 3, 5, 7, 9];
const X = 12;
console.log(Array: ${array}); console.log(Target sum:${X});
findSumPair(array, X);


## Conclusion

In this tutorial, we explored the concept of the Two Pointers Technique and how it can be implemented using JavaScript programming language to solve problems that involve searching or comparing pairs of elements in a sorted array. We also learned about the algorithm for finding a pair of elements in a sorted array whose sum equals a given target using the Two Pointers Technique. By using this technique, we can significantly improve the efficiency of our programs in terms of time complexity. In particular, the Two Pointers Technique can solve such problems in O(N) time complexity, which is much faster than the brute force approach of O(N^2). Therefore, it is essential to learn and apply this technique to solve similar problems efficiently.

Updated on: 17-Apr-2023

579 Views