Two Pointer Technique on Arrays in DSA C# - Time & Space Complexity
We want to understand how fast the two pointer technique works on arrays.
How does the time needed grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
int left = 0, right = arr.Length - 1;
while (left < right) {
if (arr[left] + arr[right] == target) {
// found pair
break;
} else if (arr[left] + arr[right] < target) {
left++;
} else {
right--;
}
}
This code uses two pointers moving from both ends to find a pair that sums to a target.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop moving pointers left and right.
- How many times: Each pointer moves at most the length of the array once.
As the array size grows, the total steps grow 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 increase directly with input size, not faster or slower.
Time Complexity: O(n)
This means the time needed grows in a straight line with the array size.
[X] Wrong: "The two pointer method is like nested loops, so it must be O(n²)."
[OK] Correct: Actually, each pointer moves only forward once, so total steps add up to n, not n times n.
Understanding this technique helps you solve many array problems efficiently and shows you can write fast code.
"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 two pointer approach
The technique uses two pointers (indices) to scan the array from different positions, often from start and end. This helps solve problems efficiently by reducing the number of iterations compared to nested loops.Final Answer:
Use two indices to traverse the array from different ends or positions simultaneously -> Option AQuick Check:
Two pointers move from different ends = D [OK]
- Thinking it always requires sorting first
- Using only one pointer instead of two
- Confusing with recursion or binary search
left and right for an array arr in C#?Solution
Step 1: Understand array indexing and initialize pointers correctly
Arrays start at index 0 and end at index Length - 1. Left pointer starts at 0 (first element), right pointer at Length - 1 (last element).Final Answer:
int left = 0, right = arr.Length - 1; -> Option DQuick Check:
Start at 0 and end at Length-1 = A [OK]
- Starting right pointer at arr.Length (out of bounds)
- Starting left pointer at 1 instead of 0
- Using negative indices
int[] arr = {1, 2, 3, 4, 5, 6};
int left = 0, right = arr.Length - 1;
int sum = 0;
while (left < right) {
sum += arr[left] + arr[right];
left++;
right--;
}
Console.WriteLine(sum);Solution
Step 1: Trace the loop iterations
Pairs added: (1+6)=7, (2+5)=7, (3+4)=7; sum = 7+7+7 = 21. Loop runs while left < right, so stops after these pairs.Final Answer:
21 -> Option AQuick Check:
Sum of pairs from ends = 21 [OK]
- Adding elements twice
- Loop running when left equals right
- Incorrect sum calculation
arr contains two numbers that sum to target.int left = 0, right = arr.Length - 1;
while (left <= right) {
int sum = arr[left] + arr[right];
if (sum == target) return true;
else if (sum < target) left++;
else right--;
}
return false;Solution
Step 1: Analyze loop condition
Usingleft <= rightallows pointers to be equal, causing sum of same element twice. Change toleft < rightto avoid using the same element twice.Final Answer:
The loop condition should beleft < rightinstead ofleft <= right-> Option CQuick Check:
Pointers must not cross or equal for two distinct elements [OK]
- Allowing pointers to be equal causing wrong sums
- Starting both pointers at 0
- Misplacing return statements
arr and a number k, how can you use the two pointer technique to find if there exists a pair with sum equal to k? Choose the correct approach.Solution
Step 1: Set pointers and move based on sum
Left pointer at 0, right pointer at last index. If sum < k, move left right to increase sum; if sum > k, move right left to decrease sum.Final Answer:
Initialize two pointers at start and end; move left pointer right if sum < k, else move right pointer left -> Option BQuick Check:
Two pointers adjust to reach target sum efficiently [OK]
- Starting both pointers at same side
- Moving pointers in wrong direction
- Sorting array again unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
