Two Pointers - Without Code
Two Pointers - Without Code
Lecture Flow
● Prerequisites
● Definitions
● Different Variants
● Things to Pay Attention (common pitfalls)
● Practice Questions
● Resources
● Quote of the Day
Prerequisites
● Basic flow control (for, while loops)
● Linear data structures
Definition
Definition
● Two Pointers technique is the use of two different pointers (usually to
keep track of array or string indices) to solve a problem with the
benefit of saving time and space.
left right
1 3 9 15 20
Variants
Parallel Pointers
Variants - Parallel Pointers
Given an array of integers, determine if it is sorted in non-decreasing order.
Input: An array of integers.
Output: True or false, whether or not the array is sorted.
1 3 9 15 20
Parallel Pointers - Approach Pattern
● In this problem pattern, we only need to look at two consecutive values.
● This is because for three consecutive numbers a, b, and c, if a <= b
and b <= c, then a <= c.
● The two pointers will iterate parallel to each other, until the right-most one
reaches the end of the array.
Parallel Pointers - Approach Pattern
left right
1 3 9 15 20
Parallel Pointers - Approach Pattern
left right
1 3 9 15 20
Parallel Pointers - Approach Pattern
left right
1 3 9 15 20
Parallel Pointers - Approach Pattern
left right
1 3 9 15 20
Practice
Problem Link
Pointers on Separate Arrays
Pointers on Separate Arrays - Problem Pattern
● You are given two arrays, sorted in non-decreasing order. Merge
them into one sorted array.
1 3 15
1 3 9 15 20
9 20
Pointers on Separate Arrays- Bruteforce
● The easiest way to do this is with the following algorithm:
left right
1 3 9 15 20 2 4 19 19
Pointers on Separate Arrays- Efficient
How do we merge them into one sorted array ?
1 3 9 15 20 2 4 19 19
1
Pointers on Separate Arrays- Efficient
left right
3 9 15 20 2 4 19 19
1 2
Pointers on Separate Arrays- Efficient
left right
3 9 15 20 4 19 19
1 2 3
Pointers on Separate Arrays- Efficient
left right
9 15 20 4 19 19
1 2 3 4
Pointers on Separate Arrays- Efficient
left right
9 15 20 19 19
1 2 3 4 9
Pointers on Separate Arrays- Efficient
left right
15 20 19 19
1 2 3 4 9 15
Pointers on Separate Arrays- Efficient
left right
20 19 19
1 2 3 4 9 15 19
Pointers on Separate Arrays- Efficient
left right
20 19
1 2 3 4 9 15 19 19
Pointers on Separate Arrays- Efficient
left right
20
1 2 3 4 9 15 19 19 20
Pointers on Separate Arrays- Efficient
Practice
Problem Link
Pointers on Separate Arrays- Efficient
Checkpoint - Link
Colliding Pointers
Colliding Pointers - Problem Pattern
Given an array of integers that is sorted in non-decreasing order,
find two numbers such that they add up to a specific target
number (it is guaranteed that at least one pair exists).
1 3 10 11 15 20 target = 25
1 + 3 3 + 10
... 10 + 15
1 + 10 3 + 11
. .
. .
. .
Colliding Pointers - Bruteforce
● We could implement a nested loop finding all possible pairs
of elements and adding them.
for i in range(n-1):
for j in range(i+1, n):
if arr[i] + arr[j] == target:
return [arr[i], arr[j]]
return []
Time complexity:
Colliding Pointers - Efficient Solution
● One pointer starts from beginning and other from the end
and they proceed towards each other.
● Since the array is sorted we can make some general
observations
○ Smaller sums come from the left half of the array
○ Larger sums come from the right half of the array
1 3 10 11 15 20
Colliding Pointers - Efficient Solution
● Therefore, using two pointers starting at the end points of
the array, we can choose to increase or decrease our current
sum however we like.
The basic idea is that:
● If our current sum is too small, move closer to the
right.
● If our current sum is too large, move closer to the
left.
1 3 10 11 15 20
Colliding Pointers - Efficient Solution
target = 25 current_sum = 21
left right
1 3 10 11 15 20
Colliding Pointers - Efficient Solution
target = 25 current_sum = 23
left right
1 3 10 11 15 20
Colliding Pointers - Efficient Solution
target = 25 current_sum = 30
left right
1 3 10 11 15 20
Colliding Pointers - Efficient Solution
target = 25 current_sum = 25
left right
1 3 10 11 15 20
Practice
Problem Link
Seeker and Placeholder
Seeker and Placeholder- Problem Pattern
You are given an array, group all non-zero elements to the
beginning of the array while maintaining their relative order. The
modification must be done in-place.
0 4 0 1 3 4 1 3 0 0
Seeker and Placeholder- Approach Pattern
● One pointer will iterate over the array, finding non-zero elements.
The other pointer will point to the next valid position for a
non-zero element.
Seeker and Placeholder- Approach Pattern
left right
0 4 0 1 3
Seeker and Placeholder- Approach Pattern
left right
4 0 0 1 3
Seeker and Placeholder- Approach Pattern
left right
4 0 0 1 3
Seeker and Placeholder- Approach Pattern
left right
4 0 0 1 3
Seeker and Placeholder- Approach Pattern
left right
4 1 0 0 3
Seeker and Placeholder- Approach Pattern
left right
4 1 0 0 3
Seeker and Placeholder- Approach Pattern
left right
4 1 3 0 0
Practice
Problem Link
For-While Combo
For-While Combo - Problem Pattern
You are given two arrays, sorted in non-decreasing order. For each
element of the second array, find the number of elements in the first
array are that strictly less than it.
● Input: Two sorted arrays.
● Output: A single sorted array containing all elements from both
arrays.
2 2 5 6
2 2 3 4
3 4 6 8
For-While Combo - Approach Pattern
● The for-while combination is used when one pointer moves one
step at a time, but the other one moves multiple steps at a time.
3 4 6
2 2 5 6 2 2 5 6 2 2
For-While Combo - Efficient
● Remember that both arrays are sorted
● This means that if there are n elements that are less than a specific
target, then there is at least n guaranteed elements for the next
target too.
For-While Combo - Efficient
● We are going to move pointer right over array b and move left over
array a until we find an aleft from array a that is greater than or
equal to bright . At this point, the value of left will represent the
number of elements in a that are less than bright.
left
a = 2 2 5 6
b = 3 4 6 8
right
For-While Combo - Efficient
left
a = 2 2 5 6
b = 3 4 6 8
right
For-While Combo - Efficient
left
a = 2 2 5 6
b = 3 4 6 8
right
For-While Combo - Efficient
left
a = 2 2 5 6
2
b = 3 4 6 8
right
For-While Combo - Efficient
left
a = 2 2 5 6
2 2
b = 3 4 6 8
right
For-While Combo - Efficient
left
a = 2 2 5 6
2 2
b = 3 4 6 8
right
For-While Combo - Efficient
left
a = 2 2 5 6
2 2 3
b = 3 4 6 8
right
For-While Combo - Efficient
left
a = 2 2 5 6
2 2 3
b = 3 4 6 8
right
For-While Combo - Efficient
left
a = 2 2 5 6
2 2 3 4
b = 3 4 6 8
right
Practice
Problem Link
Common Pitfalls
Common Pitfalls - Index out of bound
Trying to access elements using indices that are greater (or equal to) than
the size of our array will result in this exception.
Common Pitfalls - Look out for pointer conditions
One common mistake we make while doing two pointer problems is not
paying attention to our guard condition.
Geeksforgeeks
Algodaily
Quote of the day
Abraham Lincoln