Two Pointer Technique on Arrays in DSA Javascript - Time & Space Complexity
We want to understand how fast the two pointer technique works on arrays.
How does the number of steps grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
function twoPointerSum(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left < right) {
const sum = arr[left] + arr[right];
if (sum === target) return true;
else if (sum < target) left++;
else right--;
}
return false;
}
This code checks if two numbers in a sorted array add up to a target using two pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop moves two pointers through the array.
- How many times: Each pointer moves at most the length of the array once.
As the array size grows, the total steps increase roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps grow directly with the size of the array.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the array size.
[X] Wrong: "The two pointer method checks every pair, so it must be O(n²)."
[OK] Correct: The pointers move inward without repeating pairs, so total steps are just about n, not n squared.
Knowing this technique helps you solve many array problems efficiently and shows you understand smart ways to reduce work.
"What if the array was not sorted? How would the time complexity change?"
Practice
Two Pointer Technique when used on arrays?Solution
Step 1: Understand the technique
The two pointer technique uses two indices (pointers) to traverse the array, often starting from opposite ends or the same end.Step 2: Purpose of two pointers
This helps solve problems like finding pairs, removing duplicates, or partitioning without extra space and in linear time.Final Answer:
Use two indices moving towards each other or in the same direction to solve problems efficiently -> Option AQuick Check:
Two pointers = two indices moving efficiently [OK]
- Confusing with nested loops approach
- Assuming sorting is always required
- Thinking recursion is part of two pointers
left and right for an array arr in JavaScript?Solution
Step 1: Understand pointer initialization
For two pointers starting at the array ends,leftstarts at 0 (start),rightatarr.length - 1(end).Step 2: Check each option
const left = 0, right = arr.length - 1; correctly setsleft = 0andright = arr.length - 1. Others have incorrect indices.Final Answer:
const left = 0, right = arr.length - 1; -> Option DQuick Check:
Start pointer at 0, end pointer at length-1 [OK]
- Setting right pointer to arr.length (out of bounds)
- Swapping left and right initialization
- Using 1-based indexing instead of 0-based
const arr = [1, 2, 3, 4, 5];
let left = 0;
let right = arr.length - 1;
const result = [];
while (left <= right) {
if (arr[left] + arr[right] === 6) {
result.push([arr[left], arr[right]]);
left++;
right--;
} else if (arr[left] + arr[right] < 6) {
left++;
} else {
right--;
}
}
console.log(result);Solution
Step 1: Trace the two pointers and sums
Initially left=0 (1), right=4 (5), sum=6 -> push [1,5], move left=1, right=3.
Now left=1 (2), right=3 (4), sum=6 -> push [2,4], move left=2, right=2.
Now left=2 (3), right=2 (3), sum=6 -> push [3,3], move left=3, right=1 but loop ends since left > right.Step 2: Check the result array
Result contains [[1,5],[2,4],[3,3]]. But note the conditionleft <= rightallows the middle element pairing only if left equals right.
Here, at left=2 and right=2, sum=6, so [3,3] is added.Final Answer:
[[1,5],[2,4],[3,3]] -> Option CQuick Check:
Pairs summing to 6 = [[1,5],[2,4],[3,3]] [OK]
- Ignoring the case when left equals right
- Not updating pointers correctly after finding a pair
- Assuming only pairs with different indices count
function isSorted(arr) {
let left = 0;
let right = arr.length - 1;
while (left < right) {
if (arr[left] > arr[left + 1]) {
return false;
}
left++;
}
return true;
}Solution
Step 1: Analyze pointer usage
The variablerightis set but never used inside the loop or condition, making it unnecessary.Step 2: Check loop and comparison logic
The loop correctly checks adjacent elements usingleftpointer only, sorightcan be removed without affecting logic.Final Answer:
The right pointer is unused and unnecessary -> Option BQuick Check:
Unused variable right pointer = bug [OK]
- Thinking right pointer must be used in two pointer technique always
- Changing loop condition unnecessarily
- Confusing adjacent element checks with two pointer sum checks
arr and a target sum target, which code snippet correctly uses the two pointer technique to find if there exists a pair that sums to target?const arr = [1, 3, 4, 5, 7, 10, 12]; const target = 9; // Which snippet is correct?
Solution
Step 1: Understand two pointer approach for pair sum
Start with pointers at array ends: left=0, right=length-1. Move pointers based on sum compared to target.Step 2: Check each option logic
let left = 0, right = arr.length - 1; while (left < right) { const sum = arr[left] + arr[right]; if (sum === target) return true; else if (sum < target) left++; else right--; } return false; correctly moves left pointer forward if sum < target, right pointer backward if sum > target, and returns true if sum equals target.
let left = 0, right = arr.length - 1; while (left <= right) { const sum = arr[left] + arr[right]; if (sum === target) return true; else if (sum > target) left++; else right--; } return false; incorrectly moves left pointer forward when sum > target.
for (let left = 0, right = arr.length - 1; left < right; left++, right--) { if (arr[left] + arr[right] === target) return true; } return false; moves both pointers every iteration, missing some pairs.
let left = 0, right = 0; while (left < arr.length && right < arr.length) { if (arr[left] + arr[right] === target) return true; else if (arr[left] + arr[right] < target) right++; else left++; } return false; starts both pointers at 0, which is incorrect for this problem.Final Answer:
Option A code snippet -> Option AQuick Check:
Correct pointer moves based on sum vs target [OK]
- Moving wrong pointer when sum is greater or smaller
- Using for loop that moves both pointers simultaneously
- Starting both pointers at same index
Ready to prove it?
Pass the test and this pattern moves unknown
