Two Pointer Technique on Arrays in DSA Java - Time & Space Complexity
We want to understand how the time taken by the two pointer technique changes as the array size grows.
Specifically, how many steps does it take to process the array using two pointers?
Analyze the time complexity of the following code snippet.
public void twoPointerExample(int[] arr) {
int left = 0, right = arr.length - 1;
while (left < right) {
if (arr[left] + arr[right] == 0) {
System.out.println("Pair found: " + arr[left] + ", " + arr[right]);
left++;
right--;
} else if (arr[left] + arr[right] < 0) {
left++;
} else {
right--;
}
}
}
This code uses two pointers moving from both ends of a sorted array to find pairs that sum to zero.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves the two pointers.
- How many times: The combined number of moves of both pointers is at most n, where n is the array length.
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 number of steps grows directly with the size of the array.
Time Complexity: O(n)
This means the time taken grows linearly with the array size; doubling the array roughly doubles the steps.
[X] Wrong: "Since there are two pointers, the time is O(n²)."
[OK] Correct: Each pointer moves only forward and never goes back, so total moves combined are at most n, not n times n.
Understanding this linear time behavior helps you explain efficient array processing in interviews with confidence.
What if we changed the code to reset one pointer inside the loop? How would the time complexity change?
Practice
Two Pointer Technique when used on arrays?Solution
Step 1: Understand the technique's approach
The two pointer technique uses two indices or pointers to scan the array from different positions, often from start and end.Step 2: Compare with other options
Sorting or recursion are different techniques; single pointer is not two pointer technique.Final Answer:
Use two indices to traverse the array from different ends or positions simultaneously -> Option BQuick Check:
Two pointers = two indices moving [OK]
- Confusing with sorting or recursion
- Thinking it uses only one pointer
- Assuming it always requires sorted arrays
left and right for an array arr in Java?Solution
Step 1: Identify valid pointer initialization
In Java, array indices start at 0 and end at length-1, so left = 0 and right = arr.length - 1 is correct.Step 2: Check other options for invalid indices
Options B, C, and D use invalid indices outside array bounds.Final Answer:
int left = 0, right = arr.length - 1; -> Option CQuick Check:
Start at 0 and end at length-1 [OK]
- Using 1-based indexing
- Setting pointers outside array bounds
- Confusing length with last index
int[] arr = {1, 3, 5, 7, 9};
int left = 0, right = arr.length - 1;
int sum = 0;
while (left < right) {
sum += arr[left] + arr[right];
left++;
right--;
}
System.out.println(sum);Solution
Step 1: Trace the loop iterations and sum calculation
Initial pointers: left=0, right=4 - Iteration 1: sum=0+(1+9)=10, left=1, right=3 - Iteration 2: sum=10+(3+7)=20, left=2, right=2 Loop stops as left is not less than right.Step 2: Final sum value
Sum after loop is 20.Final Answer:
20 -> Option DQuick Check:
Sum pairs (1+9) + (3+7) = 20 [OK]
- Including middle element when left == right
- Adding elements multiple times
- Incorrect pointer updates
int[] arr = {2, 4, 6, 8};
int left = 0, right = arr.length;
while (left < right) {
System.out.print(arr[left] + ",");
left++;
right--;
}Solution
Step 1: Check pointer initialization
Array indices go from 0 to length-1, but right is set to arr.length, which is out of bounds.Step 2: Validate other options
Loop condition and print placement are valid; sorting is not required here.Final Answer:
The 'right' pointer is initialized out of array bounds -> Option AQuick Check:
Right pointer must be arr.length - 1 [OK]
- Setting right = arr.length instead of arr.length - 1
- Confusing loop conditions
- Assuming sorting is always needed
arr and a target sum target, which two-pointer approach correctly finds if there exists a pair of numbers that add up 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: Understand pointer movement logic
If sum is less than target, increase sum by moving left pointer right; if sum is more, decrease sum by moving right pointer left.Step 2: Confirm correctness of approach
This approach works because array is sorted, so moving pointers adjusts sum towards target.Final Answer:
Move left pointer right if sum is less than target, else move right pointer left -> Option AQuick Check:
Adjust pointers based on sum vs target [OK]
- Moving pointers without checking sum
- Ignoring array sorted property
- Moving both pointers at once
Ready to prove it?
Pass the test and this pattern moves unknown
