note-537
note-537
Two pointers is an algorithmic technique where, as the name suggests, we maintain two pointers
to keep track of two indices in the array. The primary condition for using the Two Pointers
technique is monotonicity. When we can prove that based on a certain condition the pointer is
either going to move left or right and by how much, two pointers provide an efficient way to
solve problems.
1. Equi-directional: Both start from the beginning: we have fixed and variable sliding window
algorithms.
2. Opposite-directional: One at the start and the other at the end, they move close to each
other and meet somewhere in between, (-> <-).
Here, we maintain a sliding window of flexible length whose endpoints are stored in our moving
pointers. We keep one pointer always behind or at the other and use the other one to expand
the window size.
Problem Statement: Given an array A consisting of natural numbers, and a number P, find the
length of the minimum length subarray whose product is >= P.
Example: Array : 1 2 3 4 5 6
P: 20
Output: 2
Explanation: The subarray 5-6 is the minimum length subarray with product >=20.
Approach:
1
We know that the product of the subarray is monotonically increasing as the size of the subarray
increases. Therefore, we place a ’window’ with left and right as i and j at the first item first. The
steps are as follows:
● Get the optimal subarray starting from current i, 0: Then we first move the j pointer to
include enough items that product[0:j+1]>=P, this is the process of getting the optimal
subarray that starts with 0. And assume j stops at ed
● Get the optimal subarray that ends with current j, e0: we shrink the window size by
moving the i pointer forward so that we can get the optimal subarray that ends with
current j and the optimal subarray starts from s0.
● Now, we find the optimal solution for subproblem [0:i,0:j](the start point in range [0, i]
and endpoint in range [0,j]. Start from next i and j, and repeat steps 1 and 2.
n = A.length
/*
maintain 2 pointers one for the left end and the other for the right end
of the subarray
*/
int i = 0, j = 0
curP = 1
// final answer
ans = infinity
2
while(i < n)
/*
window
*/
curP *= A[j]
j++
if(curP >= P)
ans = min(ans, j - i + 1)
// move the left pointer and update its contribution to the product
curP /= A[i]
i++
return ans
The above process is a standard flexible window size algorithm, and it is a complete search that
searches all the possible result space. Both j and i pointer move at most n, it makes the total
operations to be at most 2*n, which we get time complexity as O(n).
● TWO SUM
Problem Statement: Given a sorted array and a number S, find the indices of any two elements
which sum up to exactly S, or report that such a pair does not exist.
3
Example: Array: 1 2 3 4 5 6
Target Sum: 6
Output: 1, 3
Explanation: Elements at indices 1 and 3 (i.e 2 and 4 respectively sum upto the target sum = 6).
Approach:
Due to the fact that the array is sorted which means in the array [s,s1 ..., e1, e], the sum of any
two integers is in the range of [s+s1, e1+e]. By placing two pointers, v1 and v2, that start from s
and e, we started the search space from the middle of the possible range. [s+s1, s+e, e1+e].
Compare the target t with the sum of the two pointers v1 and v2:
1. S == v1 + v2: found
2. v1 + v2 < S: we need to move to the right side of the space from v1, i.e we increase v1 to get a
larger value.
3. v1 + v2 > S: we need to move to the left side of the space from v2, i.e we decrease v2 to get a
smaller value.
n = A.length
/*
4
maintain 2 pointers one for the left end and the other for the right end of
the subarray.
*/
int i = 0, j = n - 1
while(i <= j)
if(curSum == S)
ans = {i, j}
return ans
j -= 1
else
i += 1
return ans
Since both the pointers move to the right and left at most n times the total increment and
decrement operators are bounded by 2*n. Hence the Time complexity is O(n). The space
complexity is O(1) since we are using constant space to maintain the pointers.