Professional Documents
Culture Documents
Introduction To Competitive Programming (Week 4)
Introduction To Competitive Programming (Week 4)
Introduction To Competitive Programming (Week 4)
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):
● 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.)