Bird
0
0
DSA Cprogramming~10 mins

Two Pointer Technique on Arrays in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Two Pointer Technique on Arrays
Initialize two pointers: left=0, right=end
Check condition: left < right?
NoStop
Yes
Process elements at left and right
Move pointers: left++ or right-- or both
Back to Check condition
Start with two pointers at array ends, check condition, process elements, move pointers inward, repeat until pointers meet.
Execution Sample
DSA C
int left = 0, right = n - 1;
while (left < right) {
  if (arr[left] + arr[right] == target) {
    // found pair
    break;
  } else if (arr[left] + arr[right] < target) {
    left++;
  } else {
    right--;
  }
}
Find two numbers in a sorted array that sum to a target using two pointers moving inward.
Execution Table
StepOperationleftrightSum arr[left]+arr[right]ActionVisual State
1Initialize pointers05arr[0]+arr[5]=1+12=1313 < 15, move left++[1, 3, 5, 7, 9, 12] (left=0, right=5)
2Move left pointer15arr[1]+arr[5]=3+12=15Sum == target, found pair[1, 3, 5, 7, 9, 12] (left=1, right=5)
3Break loop1515Stop searching[1, 3, 5, 7, 9, 12] (left=1, right=5)
💡 Pointers met condition sum == target, loop stops.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
left0111
right5555
sumN/A131515
Key Moments - 3 Insights
Why do we move the left pointer when the sum is less than the target?
Because the array is sorted, increasing left moves to a bigger number, increasing the sum. See execution_table step 1 where sum 13 < 15, so left moves from 0 to 1.
Why do we stop when left pointer meets right pointer?
When left >= right, all pairs have been checked. In execution_table step 3, the loop stops because the pair sum equals target, or pointers crossed.
What if the array is not sorted?
The two pointer technique relies on sorting to decide pointer moves. Without sorting, this logic fails, so the method won't find pairs correctly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'left' after step 1?
A1
B0
C5
DN/A
💡 Hint
Check the 'left' column in execution_table row for step 1.
At which step does the sum of arr[left] + arr[right] equal the target?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the 'Sum arr[left]+arr[right]' and 'Action' columns in execution_table.
If the array was not sorted, what would happen to the pointer movement logic?
AIt would still work correctly
BPointers would move randomly
CThe logic to move pointers based on sum comparison would fail
DThe pointers would never move
💡 Hint
Refer to key_moments explanation about sorting necessity.
Concept Snapshot
Two Pointer Technique on Arrays:
- Use two pointers at start and end of sorted array
- Check sum of elements at pointers
- If sum < target, move left pointer right
- If sum > target, move right pointer left
- Stop when pointers meet or pair found
Full Transcript
The two pointer technique uses two indexes starting at the ends of a sorted array. We check the sum of the elements at these pointers. If the sum is less than the target, we move the left pointer right to increase the sum. If the sum is greater, we move the right pointer left to decrease the sum. We repeat this until the pointers meet or we find the target sum. This method works only on sorted arrays because the pointer movement depends on the order of elements.