● You havean array of length n (< 1e5) which
comprises of integers between 1 and 1e9.
Your task is to find whether a number x
exists in this array or not ?
● What is the best time complexity you can
think of ?
Let's start with a question
5.
Let the arraybe given by
● a = [2, 7, 5, 4, 8, 3, 2]
● And we have x = 3
How would you find x = 3 manually? Can you do better?
Let's take a case!
6.
The best thingyou can do for an array with no other property is to
manually compare with each number.
Hence giving a search time complexity of O(n).
No! The best algorithm is Linear Search
7.
What if thearray was sorted in
increasing order?
• a = [2, 2, 3, 4, 5, 7, 8]
• And we have x = 3
Linear Search:
• Imagineyou have an array: [2, 7, 5, 4, 8, 3, 2] (can be unsorted too).
• To find the number 9, you start at the beginning and check each element:
2 (nope), 7 (nope), 5 (nope), 4 (nope), 8(nope), 3(yes!).
• Worst-case: you might need to check all elements. With n elements,
that’s n checks.
Binary Search:
• Now, the array is sorted: [2, 2, 3, 4, 5, 7, 8] .
•To find the number 3:
1. Check the middle element (4). Since 3 is smaller than 4, you ignore the right half.
2. Now you check the middle of the left half (2). Since 3 is greater than 2, you ignore the
left half.
3. Now you check the middle of the active region(3). Bingo!
• Worst-case: You halve the search area each time. With n elements, that’s significantly
faster for large arrays (will be discussed later).
10.
Binary search isan algorithm to quickly search something in a
monotonous collection.
Intuition for Binary Search
It is based on the observation that once we compare a
value to the required value, we can determine on which
side of this value does our required value lie. So, we can
skip checking that side for our value.
So, how do we actually apply this observation ?
11.
Implementation
Compare the middleelement (𝑚𝑖𝑑 ) of active region with 54.
If it’s more than 54 , change end to 𝑚𝑖𝑑 − 1
If it’s less than or equal to 54, change start to 𝑚𝑖𝑑 + 1.
Repeat till your active region has only one element (𝑠𝑡𝑎𝑟𝑡 ==
𝑒𝑛𝑑),
Congratulations! You have found 54 or not.
Consider a sorted array containing 54.
You want to find its position.
We’ll maintain an active region where it can be.
Initially, that’s the whole array.
Active Region is
generally
marked by two
variables, start
and end.
12.
Pseudocode
Can you guessthe
complexity?
low = 0, high = n - 1;
ans = 0;
while (low <= high) {
mid = (low + high) / 2;
if (a[mid] <= 54) {
ans = mid;
low = mid + 1;
}
else {
high = mid - 1;
}
}
return (a[ans] == 54);
13.
Complexity Calculation
Then, wecheck the middle element of this region. Upon seeing
it, either we would have found what we were searching for or
we can determine on which side of this region does it lie ?
Hence in one step we effectively half the size of our active
region.
So, the number of steps to obtain an active region of size 1 is
about log(n).
So, first of all let’s imagine an active region in our array (At the
beginning the entire array would be the active region ).
14.
How Fast IsIt?
Find 37!
For 𝑛 = 1010, the time complexity of linear is 1010
While for binary search is around 34.
15.
Problem I
Find thesquare root of a number 1 ≤ 𝑛 ≤ 1016 using
binary search.
(The absolute error shouldn’t be greater than 10−6)
Floor(sqrt)
Binary Search availablein STL?
Direct Functions available in STL library of C++ so you don’t have
to write the implementation.
Lower Bound:
Lower Bound of x returns the index of element greater than or
equal to x in a sorted array or vector.
Upper Bound:
Upper Bound of x returns the index of element greater than x in a
sorted array or vector.
20.
Example
Consider the arrayA = [1,2,3,3,4,5,6,8]
Lower Bound of 3 = 2
Upper Bound of 3 = 5
Lower Bound of 9 = 8
Upper Bound of 8 = 8
Lower Bound of 0 = 0
Upper Bound of 0 = 0
21.
Predicate Functions
1. PredicateFunction:
• This function helps determine whether a given value is a valid answer.
• It maps the problem's constraints to a boolean value (true/false).
• For example, given a value 𝑥, the predicate function 𝑃(𝑥) will return true
if 𝑥 meets the required condition, otherwise false.
2. Concept of 0000111 or 1110000:
• The key insight is that, for many problems, the sequence of possible
answers evaluated through the predicate function forms a pattern of
consecutive 0s followed by consecutive 1s or vice versa.
• Think of it like this: if the predicate function returns false up to a certain
point and then true for all subsequent values, you'll get a sequence like
00000111111. The reverse, 1111000000, is also possible.
•You need to find the first or the last occurrence of ones.
Some more Questions(Homework)
Problem - 492B
Problem - 1985F
https://codeforces.com/problemset/problem/2032/C
https://codeforces.com/contest/1480/problem/C
The Two PointersMethod is an important technique that is often
used in competitive programming. It is a broad concept which helps
us deal with a multitude of problems.
Introduction
As the name suggests, we will be using two pointers to approach the
question in hand. This may be applied whenever:
● Dealing with properties of contiguous subarrays
● Dealing with sorted arrays and searching
● Dealing with specifically two values in a given set
29.
Problem IV
Given anarray of 𝑛 𝑛 ≤ 106
integers 𝑎 𝑖 ≤ 109
, determine
if there exists a pair of indices 𝒊, 𝒋 such that 𝑎 𝑖 + 𝑎 𝑗 = 𝑥
where 𝑖 ≠ 𝑗.
Example
a = 1, 4, 8, 2, 6 , 𝑥 = 3 → Output: YES
a = 1, 4, 8, 2, 6 , 𝑥 = 2 → Output: NO