0% found this document useful (0 votes)
62 views74 pages

Two Pointers - Without Code

The document provides an overview of the Two Pointers technique, including its definition, variants such as Parallel Pointers and Colliding Pointers, and common pitfalls to avoid. It outlines practical applications of the technique in solving problems related to arrays, such as merging sorted arrays and finding pairs that sum to a target. Additionally, it offers resources for further learning and includes a motivational quote.

Uploaded by

remidan37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views74 pages

Two Pointers - Without Code

The document provides an overview of the Two Pointers technique, including its definition, variants such as Parallel Pointers and Colliding Pointers, and common pitfalls to avoid. It outlines practical applications of the technique in solving problems related to arrays, such as merging sorted arrays and finding pairs that sum to a target. Additionally, it offers resources for further learning and includes a motivational quote.

Uploaded by

remidan37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

Two Pointers

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.

Input: Two sorted arrays of integers.


Output: The merged 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:

a. Collect all the elements into one big array;


b. Sort it with any sorting method built into your language.

● Such an algorithm will take time


● = size of array a.
● = size of array b.
Pointers on Separate Arrays- Efficient
● Two arrays or Lists, each has been assigned a pointer

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 ?

● To answer this question, let's understand which element will be


in the first position in the output array. Of course, this is the
smallest element among all in first and second array. The
smallest element of the first array is at the beginning of the
array, same as with the second array.
Pointers on Separate Arrays- Efficient
left right

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).

● Input: A sorted array of integers and a target number.


● Output: Two numbers from the array that sum up to the target
number.
Colliding Pointers - Bruteforce

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.

Input: An array of integers.


Output: The array, modified in-place, such that all non-zero
numbers are at the beginning of the array.

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.

● This is usually applied to problems in which the position of one


pointer is directly dependent on the position of the other.
For-While Combo - Bruteforce
● What would a brute force approach look like?
● For every element in the second array, iterate over the first array
and count the ones that are smaller than it.
● This is very inefficient - O(n2)

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.

# left -> our left pointer


# right -> our right pointer
# what should our condition be? What else ıs wrong wıth t

while left < right:


pass

while left <= right:


pass
Checkpoint - Link
Practice Problems
Divide Players into Teams of Equal Skill
Boats to Save People
Container With Most Water
Sum of Square Numbers
Partition Labels
Merge Sorted Array
Remove Duplicates from Sorted Array
Rotate Array
Alternating Subsequence
Helpful resources

Codeforces Pilot Course

Geeksforgeeks

Algodaily
Quote of the day

In the end, it’s not the year in


your life that counts. It's the life
in your years

Abraham Lincoln

You might also like