š Scenario: Imagine you have a list of numbers representing the prices of different fruits in a market. You want to find all pairs of fruits whose prices add up to a specific budget. This is a common problem in shopping and budgeting.
šÆ Goal: You will learn how to find pairs of numbers that add up to a target sum using two methods: brute force and the two pointer technique. You will see why the two pointer technique is faster and better.
š What You'll Learn
Create an array called prices with the exact values: [2, 7, 11, 15]
Create a variable called target and set it to 9
Write a brute force nested for loop using i and j to find pairs that sum to target
Write a two pointer technique using left and right pointers to find pairs that sum to target
Print the pairs found by both methods
š” Why This Matters
š Real World
Finding pairs of items that fit a budget is common in shopping apps, budgeting tools, and financial software.
š¼ Career
Understanding efficient searching techniques like two pointer helps in coding interviews and building fast algorithms in software development.
Progress0 / 4 steps
1
Create the prices array
Create an array called prices with these exact values: [2, 7, 11, 15]
DSA Javascript
Hint
Use const prices = [2, 7, 11, 15]; to create the array.
2
Set the target sum
Create a variable called target and set it to 9
DSA Javascript
Hint
Use const target = 9; to set the target sum.
3
Find pairs using brute force
Write a nested for loop using i and j to find all pairs in prices that add up to target. Store the pairs in an array called bruteForcePairs.
DSA Javascript
Hint
Use two loops: outer loop with i, inner loop with j starting from i + 1. Check if sum equals target.
4
Find pairs using two pointer technique and print results
Sort the prices array. Use two pointers called left starting at 0 and right starting at the last index. Move pointers to find pairs that sum to target. Store pairs in twoPointerPairs. Then print both bruteForcePairs and twoPointerPairs.
DSA Javascript
Hint
Sort the array first. Use left and right pointers to find pairs. Print both arrays with console.log.
Practice
(1/5)
1. What is the main advantage of the two pointer technique over brute force in array problems?
easy
A. It always sorts the array before processing
B. It uses more memory to store intermediate results
C. It reduces time complexity by avoiding nested loops
D. It requires recursion to work properly
Solution
Step 1: Understand brute force time complexity
Brute force often uses nested loops, leading to O(n²) time complexity.
Step 2: Analyze two pointer technique efficiency
Two pointers move through the array in a single pass, reducing time complexity to O(n).
Final Answer:
It reduces time complexity by avoiding nested loops -> Option C
Quick Check:
Two pointer = faster than brute force [OK]
Hint: Two pointers scan once, brute force scans twice [OK]
Common Mistakes:
Thinking two pointer uses more memory
Assuming two pointer always sorts array
Believing recursion is required
2. Which of the following is the correct syntax to initialize two pointers at the start and end of an array arr in JavaScript?
easy
A. const left = 0, right = arr.length - 1;
B. let left = arr[0], right = arr[arr.length];
C. var left = 1, right = arr.length;
D. const left = arr.length, right = 0;
Solution
Step 1: Identify valid pointer initialization
Pointers should be indices, so left = 0 and right = arr.length - 1 are correct.
Step 2: Check invalid options
Options B, C, and D use incorrect indices or values outside array bounds.
Final Answer:
const left = 0, right = arr.length - 1; -> Option A
Quick Check:
Start at 0 and end at length-1 [OK]
Hint: Array indices start at 0, end at length-1 [OK]
Common Mistakes:
Using arr.length as index (out of bounds)
Starting pointers at wrong indices
Confusing values with indices
3. What will be the output of the following code snippet?
const arr = [1, 2, 3, 4, 5];
let left = 0, right = arr.length - 1;
let sumPairs = [];
while (left < right) {
sumPairs.push(arr[left] + arr[right]);
left++;
right--;
}
console.log(sumPairs);
medium
A. [6, 6, 6]
B. [6, 6]
C. [5, 7, 9]
D. [1, 5]
Solution
Step 1: Trace pointer movements and sums
Pairs: (1+5)=6, (2+4)=6, (3+3) stops because left == right, so only two pairs added.
Step 2: Check loop condition and sums stored
Loop runs while left < right, so only two sums are pushed: [6, 6].
Final Answer:
[6, 6] -> Option B
Quick Check:
Sum pairs from ends = [6, 6] [OK]
Hint: Two pointers meet in middle, sum pairs once [OK]
Common Mistakes:
Including middle element sum when pointers meet
Miscounting number of pairs
Confusing pointer update order
4. Identify the bug in the following two pointer code that tries to find if a pair sums to 10:
const arr = [1, 3, 5, 7, 9];
let left = 0, right = arr.length - 1;
while (left <= right) {
if (arr[left] + arr[right] === 10) {
console.log('Found');
break;
} else if (arr[left] + arr[right] < 10) {
left++;
} else {
right++;
}
}
medium
A. Loop condition should be left < right
B. Left pointer should decrement, not increment
C. Array is not sorted
D. Right pointer should decrement, not increment
Solution
Step 1: Analyze pointer updates
If sum is greater than 10, right pointer should move left (decrement) to reduce sum, not increment.
Step 2: Check loop condition and array
Loop condition is fine; array is sorted; only pointer update is wrong.
Final Answer:
Right pointer should decrement, not increment -> Option D
Quick Check:
Decrease right pointer to reduce sum [OK]
Hint: Decrease right pointer when sum too big [OK]
Common Mistakes:
Incrementing right pointer instead of decrementing
Changing loop condition unnecessarily
Ignoring array sorting requirement
5. Given a sorted array arr and a target sum k, which approach best finds all unique pairs that sum to k efficiently?
Choose the best method that beats brute force.
hard
A. Use two pointers starting at both ends, move inward based on sum comparison
B. Use nested loops to check every pair for sum equals k
C. Sort the array and use binary search for each element's complement
D. Use a hash map to store elements and check complements in one pass
Solution
Step 1: Compare approaches for efficiency
Nested loops (B) are O(n²), binary search (C) is O(n log n), hash map (A) is O(n) but uses extra space.
Step 2: Two pointer technique advantage
Two pointers (D) scan array once in O(n) time with O(1) space, ideal for sorted arrays.
Final Answer:
Use two pointers starting at both ends, move inward based on sum comparison -> Option A
Quick Check:
Two pointer = best for sorted arrays [OK]
Hint: Two pointers scan once, no extra space needed [OK]
Common Mistakes:
Choosing nested loops despite inefficiency
Ignoring space complexity of hash map
Using binary search unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.