One Pager Cheat Sheet
- The
Two Pointer Technique
is an essential tool for software developers to use, especially when faced with technical interviews. - Pointers are
references
to objects, and the two pointers technique is used to track and compare array or string indices to save time and space. - The
two-pointer technique
is an efficient approach to processing two elements of adata structure
, such as an array or list, per loop in order to solve problems involving collections. - The two-pointer technique is a
search algorithm
used to solve problems involving collections such as arrays and lists by comparing elements pointed by two pointers and updating them accordingly. - By initializing two variables
pointer_one
andpointer_two
and evaluating their values with respect to the giventarget
, we can find pairs in an array that sum up to a certain number with aO(n)
time complexity. Two pointers are used in different programming languages to start from the ends of an array and iteratively narrow in to find the
target` more efficiently than other techniques.- We can use two pointers starting from the beginning and end of an already sorted array to check if they 'sum up' to the target, which is done by a simple comparison statement
if sum == target
. - The logic applied is to
increment the left pointer
if the sum of values isless than the target value
anddecrement the right pointer
if it ishigher than the target value
. - Understand that if
arr[pointer_one]
<target-arr[pointer_two]
,pointer_one
should be moved forward to get closer to the desired magnitude. - The process of using a fast pointer and a slow pointer is another way to apply the two pointer technique, which can have
O(n)
time complexity andO(1)
space complexity. - Using
Slow and Fast Pointers
can detect cycles in alinked list
, such as when a node points back to a previous node. - By setting
fast
to traverse twice as quickly asslow
, the distance between them increases at each step. - If both
pointers
reach the same node, then there is a cycle present in thelinked list
. - The
code
attached provides anO(n)
or linear time complexity. - Yes, the two pointer technique can reduce both time and space complexity down to
O(n)
, thus improving overall performance. - Two-pointers are
useful
for iterating over and combining two sorted arrays. - The two-pointer technique involves placing two pointers at opposite ends of an
array
andcomparing
their values toreverse
its order by swapping or shifting items.