Javascript Program for Two Pointers Technique Last Updated : 16 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.Naive solution: JavaScript // Naive solution to find if there is a // pair in A[0..N-1] with given sum. function isPairSum(A, N, X) { for (let i = 0; i < N - 1; i++) { for (let j = i + 1; j < N; j++) { // as equal i and j means same element if (i == j) continue; // pair exists if (A[i] + A[j] == X) return 1; // as the array is sorted if (A[i] + A[j] > X) break; } } // No pair found with given sum. return 0; } // Driver code // array declaration let arr = [2, 3, 5, 8, 9, 10, 11]; // value to search let val = 17; // size of the array let arrSize = 7; // Function call console.log(isPairSum(arr, arrSize, val)); Output1 Time Complexity: O(n2).Efficieant Approach (Two Pointer Approach):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. JavaScript // Two pointer technique based solution to find // if there is a pair in A[0..N-1] with a given sum. function isPairSum(A, N, X) { // represents first pointer let i = 0; // represents second pointer let 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 j-- else j--; } return false; } // Driver code let arr = [2, 3, 5, 8, 9, 10, 11]; let val = 17; let arrSize = 7; let result = isPairSum(arr, arrSize, val); console.log(result); Outputtrue Illustration : Complexity Analysis:Time Complexity: O(n)Auxiliary Space: O(1) since using constant spaceHow 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 arraysFind the pair in array whose sum is closest to xFind all triplets with zero sumFind a triplet that sum to a given valueFind a triplet such that sum of two equals to third elementFind four elements that sum to a given valuePlease refer complete article on Two Pointers Technique for more details! Comment More infoAdvertise with us Next Article C++ Program for Two Pointers Technique K kartik Follow Improve Article Tags : JavaScript two-pointer-algorithm Practice Tags : two-pointer-algorithm Similar Reads Two Pointers Technique Two pointers is really an easy and effective technique that is typically used for Two Sum in Sorted Arrays, Closest Two Sum, Three Sum, Four Sum, Trapping Rain Water and many other popular interview questions. Given a sorted array arr (sorted in ascending order) and a target, find if there exists an 11 min read Two-Pointer Technique in a Linked List The two-pointer technique is a common technique used in linked list problems to solve a variety of problems efficiently. It involves using two pointers that traverse the linked list at different speeds or in different directions. Use Cases of Two-Pointer Technique in a Linked List:1. Finding the Mid 3 min read Java Program for Two Pointers Technique 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 so 4 min read C++ Program for Two Pointers Technique 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 so 4 min read C Program for Two Pointers Technique 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 so 3 min read Pointer-Analysis Pointers have been a thorn in the side of compilers for decades. The difficulty is in the difficulty of understanding pointers as things that point to other data, and how they relate to each other. Pointer analysis has been an essential part of compiler design ever since the early 1970s, with many d 7 min read Like