Introduction To Competitive Programming (Week 4)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Introduction to Competitive Programming 

Week-4 
 
Binary Search:
● Binary search is an efficient search algorithm used for efficient lookups of keys in
a ​sorted list (array) of data​. Data can be in the form of integers, strings etc. The
comparators used for sorting should be the same comparators used for binary
search.
● Another searching algorithm is linear search which is basically traversing the list
element by element from the beginning to the end and checking if the current
element is the element we are looking for. The problem with linear search is that
it is very inefficient and in the worst case, it takes​ ​O(n)​ ​time. If the list is sorted,
we would rather use binary search.
● Binary search runs in ​O(log(n))​ time. The idea is to eliminate ranges in which we
know the element we are looking for doesn’t exist. For example if the center
element of the list is 6 and we are looking for 9, we know that the element we are
looking for could only lie in the second half. So we eliminate the possibility of the
element existing in the first half.
● Simple Binary Search code (looks for any index of required element. Not
necessarily first index):
● Lower Bound code (first index with element greater than or equal to required
element):

● Additional reading resource: ​GeeksforGeeks


● Some interesting binary search problems:
○ (WARM UP): ​Search Insert Position​ (easy problem to start with)
○ (EASY): ​Floor Square Root​ (Make an interviewBit account. Sorry :P)
○ (MEDIUM DIFFICULTY): ​Search in Rotated Sorted Array
■ Hint: ​Look for the pivot using binary search.
○ (MEDIUM DIFFICULTY): ​Find First and Last Position of Element in Sorted
Array
■ Hint: ​Refer to provided lower bound code.
○ (MEDIUM DIFFICULTY): ​Find Peak Element​ (ARRAY IS NOT SORTED
HERE. Think how to use binary search here.)
○ (HARD): ​Median of Two Sorted Arrays​ (Take your time with this one. It’s
really interesting)
Greedy:
● Greedy algorithms are used in some cases where one needs to arrive at an
optimal solution.
● The strategy is to make a ​greedy choice ​that seems best at the time and then
solve the subproblem that remains using the same strategy. For example, if you
have n sticks and you need to pick m sticks from them such that the sum of the
lengths of these sticks is maximum, you would first pick the longest stick and
then you are left with a subproblem consisting of n-1 sticks where you are left
with m-1 chances. Solve this subproblem with the same strategy and combine
the solution of this subproblem with the original greedy choice to produce a
solution to the whole problem.
● You can think of it recursively:

● It’s inefficient to use a recursive approach because recursive algorithms are less
efficient than iterative algorithms. We could convert the above code into an
iterative code.
● Think of a solution that runs in ​O(1)​ space. The above solution runs in ​O(m)
space.
● This was just a basic introduction to greedy algorithms. You should read the rest
from ​handbook​(Antti Laaksonen):
○ Greedy Algorithms starts at page 57.
○ Read through everything that comes before Data Compression (page 62).
You can skip Data compression/Huffman coding for now​. However, if
you want to train yourself further, read through it too.
● Some theory (skip if you are comfortable with greedy):
○ Greedy algorithms are used to solve problems which have two properties:
■ Greedy choice property: ​A globally optimal solution can be
reached by making locally optimal choices. These choices are
called greedy choices.
■ Optimal substructure property: ​The optimal solution to the
problem must contain the optimal solution to subproblems created
by making a greedy choice.
● Greedy problems:
○ (EASY/WARM UP): ​is Subsequence
○ (EASY): ​Bulbs​ (Please sign up for interviewBit.)
○ (MEDIUM DIFFICULTY): ​Jump Game​ (Spend some time thinking of a
greedy solution. If you can’t come up with a solution, refer to the solution.
This is more of a learning problem.)
○ (MEDIUM DIFFICULTY): ​Non-overlapping Intervals​ (Again, more of a
learning problem. If you can’t come up with a solution in 10 minutes, refer
to the solution.)
○ (MEDIUM DIFFICULTY): ​Meeting rooms
○ (HARD): ​Seats​ (If you are finding it tough, read the solution. The greedy
solution alone is good enough for now. Also, don’t feel bad if you can’t
think of a solution. I couldn’t solve it either, lol.)

You might also like