Two Pointer Technique on Arrays in DSA Go - Time & Space Complexity
We want to understand how the time taken by the two pointer technique changes as the array size grows.
How many steps does it take to complete the process when using two pointers?
Analyze the time complexity of the following code snippet.
func twoPointerExample(arr []int) bool {
left, right := 0, len(arr)-1
for left < right {
if arr[left]+arr[right] == 0 {
return true
} else if arr[left]+arr[right] < 0 {
left++
} else {
right--
}
}
return false
}
This code uses two pointers moving from the start and end of a sorted array to find if any two numbers sum to zero.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for loop that moves the left and right pointers through the array.
- How many times: The combined number of moves of left and right 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 size of the input array.
[X] Wrong: "Since there is a nested if inside the loop, the time complexity must be O(n²)."
[OK] Correct: The if statements do not create nested loops; the pointers move independently and only once through the array, so the total steps remain linear.
Understanding how two pointers move through an array efficiently is a key skill that shows you can optimize simple searches beyond brute force.
"What if the array was not sorted? How would the time complexity of this two pointer approach change?"
Practice
Two Pointer Technique when used on arrays?Solution
Step 1: Understand the technique
The two pointer technique uses two indices or pointers to traverse the array, often starting from different ends or positions.Step 2: Compare with other options
The other options describe different methods, but only using two indices to scan the array from different ends or positions simultaneously correctly describes the two pointer approach.Final Answer:
Use two indices to scan the array from different ends or positions simultaneously. -> Option CQuick Check:
Two pointers move independently to solve problems efficiently [OK]
- Thinking it uses two separate arrays
- Confusing with nested loops approach
- Assuming only one pointer moves
left and right for an array arr in Go?Solution
Step 1: Understand array indexing in Go
Arrays and slices in Go are zero-indexed, so the first element is at index 0 and the last at len(arr)-1.Step 2: Check pointer initialization
Setting left to 0 and right to len(arr)-1 correctly points to the first and last elements respectively.Final Answer:
left := 0; right := len(arr) - 1 -> Option BQuick Check:
Start pointers at 0 and last index [OK]
- Setting right to len(arr) causes out-of-range error
- Starting left at 1 misses first element
- Using negative indices which Go does not support
arr := []int{1, 3, 5, 7, 9}
left, right := 0, len(arr)-1
for left < right {
if arr[left]+arr[right] == 10 {
fmt.Println(left, right)
break
} else if arr[left]+arr[right] < 10 {
left++
} else {
right--
}
}Solution
Step 1: Trace the pointers and sums
Initial pointers: left=0 (arr[0]=1), right=4 (arr[4]=9), sum=10 ==10, so prints 0 4 and breaks.Step 2: Verify the condition and loop
Actually, sum=1+9=10, so it prints 0 4 and breaks immediately.Final Answer:
0 4 -> Option AQuick Check:
Sum 1+9=10 at indices 0 and 4 [OK]
- Not breaking after first match
- Confusing indices with values
- Incorrect pointer updates
arr := []int{2, 4, 6, 8}
left, right := 0, len(arr)
for left < right {
sum := arr[left] + arr[right]
if sum == 8 {
fmt.Println("Found")
break
} else if sum < 8 {
left++
} else {
right--
}
}Solution
Step 1: Check pointer initialization
right is set to len(arr), but valid indices go from 0 to len(arr)-1. This causes out-of-range error when accessing arr[right].Step 2: Verify other parts
left starting at 0 is correct, loop condition left < right is valid, sum calculation is correct.Final Answer:
right is initialized to len(arr), which is out of range -> Option DQuick Check:
Array indices go 0 to len-1, not len [OK]
- Setting right to len(arr) causes runtime error
- Changing loop condition unnecessarily
- Misunderstanding array indexing
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, use the two pointer technique to find all pairs that sum to 10. Which of the following Go code snippets correctly prints all such pairs?Solution
Step 1: Check pointer initialization and loop condition
left, right := 0, len(arr)-1 for left < right { sum := arr[left] + arr[right] if sum == 10 { fmt.Println(arr[left], arr[right]) left++ right-- } else if sum < 10 { left++ } else { right-- } } initializes left=0 and right=len(arr)-1, which are valid indices. Loop runs while left < right to avoid duplicates and out-of-range.Step 2: Verify pointer updates on sum match
When sum == 10, both pointers move inward to find other pairs. This correctly finds all pairs without repeats.Final Answer:
left, right := 0, len(arr)-1 for left < right { sum := arr[left] + arr[right] if sum == 10 { fmt.Println(arr[left], arr[right]) left++ right-- } else if sum < 10 { left++ } else { right-- } } -> Option AQuick Check:
Two pointers move inward, printing pairs when sum matches [OK]
- Using right = len(arr) causes index error
- Loop condition left <= right prints duplicates
- Starting left at 1 misses pairs with first element
Ready to prove it?
Pass the test and this pattern moves unknown
