JavaScript Program for Two Pointers Technique



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. In this example, we are going to solve the given problem using brute force and two pointers technique.

In this article we are given a sorted array, our task is to find if there exists any pair of elements such that their sum is equal to a given number. Users must be familiar with array, loop and nested loops, and if/else statement.

Approaches for Pair Sum Problem

Here is a list of approaches which we will be discussing in this article with stepwise explaination and complete example codes.

Using Brute Force Approach

In this approach to find the pair with given sum, we will use brute force approach where we iterate over each elements of the array to find the pair with given sum.

  • We have declared an array and a variable initially. We have defined a function findSumPair() that accepts the array and target sum value as its argument.
  • In the function, first we get the length of the array using length property.
  • Then we have used two for loop, where first for loop selects an element from the array and second for loop iterates over the loop making pairs with the selected element using first for loop.
  • If the sum of the element in pair is equal to required sum, then function returns the indices of elements in pair in web console using console.log().
  • If the pair is not found then message is displayed pair not found.

Example

Here is a complete example code implementing above mentioned steps using brute force approach.

const array = [1, 3, 5, 7, 9];
const X = 10;
console.log(`Array: ${array}`);
console.log(`Target sum: ${X}`);

function findSumPair(array, X) {
    const n = array.length;
    for (let i = 0; i 


Using Two Pointers Technique

We have used two pointers technique to find the pairs in array with given sum.The two pointers is an efficient technique to solve problems of sorted arrays. We maintain two pointers (such as left and right in this example) at different positions and moving them according to requirement. It helps in getting better time complexity than using nested loops.

  • We have declared an array and a variable initially. We have defined a function findSumPair() that accepts the array and target sum value as its argument.
  • Then we have defined two pointers, left and right where left pointer represents first element of the array and right pointer points to last element of the array.
  • Then we find sum using left and right indices. If the sum is equal to the target, return the indices left and right.
  • If the sum is less than the target, increment left and the sum is greater than the target, decrement right. This loop continues till left pointer is less than right pointer using while loop.
  • If no such pair exists, return null.

Example 1

Input:
array = [1, 3, 5, 7, 9], X = 12
Left = 1, Right = 9, sum = 10
since sum  left = 3

Now, Left = 3, Right = 9, sum = 12
since sum = x, 
return index of Left(3) & Right(9) i.e 1 and 4 

Example 2

Here is a complete example code implementing above mentioned steps using two pointers technique.

const array = [1, 3, 5, 7, 9], X = 12;
console.log(`Array: ${array}`);
console.log(`Target sum: ${X}`);

function findSumPair(array, X) {
    let left = 0;
    let right = array.length - 1;
    while (left 


Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Brute Force O(n^2) O(1)
Two Pointers O(n) O(1)

Conclusion

In this article we have discussed a JavaScript program for two pointers technique, where we first discussed the brute force approach to solve the problem and then used two pointers approach to solve the problem. The two pointers technique is efficient than brute force as mentioned in the time complexity table.

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2024-12-06T11:33:29+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements