Lecture-2-Binary SearchTwo Pointers (1) (1)
Lecture-2-Binary SearchTwo Pointers (1) (1)
By Programming and
Algorithms Group
Search
&
TWO
POINT
Searching
So,
Implementation generally
marked by two
variables, start
and end.
Consider a sorted array containing 54.
You wanna find its position.
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
Example
Consider the array A = [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
Predicate Functions
1. Predicate Function:
• 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.
Problem - 492B
Problem - 1985F
https://codeforces.com/problemset/problem/20
32/C
https://codeforces.com/contest/1480/
problem/C
Two
pointe
r
metho
Introduction
The Two Pointers Method 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.
Example
a = Output: YES
a = Output: NO
Solution
Problem V
https://codeforces.com/problemset/problem/1995/B1
Soluti
on
Problem VI
https://codeforces.com/problemset/problem/1941/F
Hints
1. Finding differences
a. As we can only minimise one difference, we greedily take
the maximum difference
b. If the frequency of is more than 1, we can’t make any
optimisations and the answer is . Let the index where max diff occurs
is ()
c. We also store the second maximum difference (call it )
4. The answer
a. Let’s consider the minimum of these maximum
imbalances, which is .
b. So the answer is .
Hints
Solution
https://codeforces.com/contest/1941/submission/290184294
Hints
Bit
Manipul
ation
Binary Number
System
Binary number system is the system of writing numbers in
base 2 instead of the usual base 10. Here, every digit can
either be 0 or 1. This binary digit is called a bit.
0 = ( 22 * 0 ) + ( 21 * 0 ) + 000
( 20 * )
1 = (22 * 0) + (21 * 0) +(20 * 1) 00
2 = (22 * 0) + (21 * 1) 1
+(20 * 0)
3 = (22 * 0) + (21 * 1) 01
+(20 * 1)
3 = (22 * 1) + (21 * 0) 0
Bitwise Operations
There are 4 bitwise operators. As the name suggests,
they work bitwise, ie independently on each bit.
Not ~
Or |
And &
Xor
Bitwise Not
It changes the bits from 0 to 1 and vice
versa.
A ~A
1 0
0 1
Bitwise Or
It results in 1 if either of the bits is
1.
A B A|B
0 0 0
1 0 1
0 1 1
1 1 1
Bitwise And
It results in 1 if both the bits are 1.
A B A&B
0 0 0
1 0 0
0 1 0
1 1 1
Bitwise Xor
It results in 1 if one of the bit is 0 and the other is
1.
A B A&B
0 0 0
1 0 1
0 1 1
1 1 0
Properties of XOR
• 0 is the identity element.
X^0=X
Any element is its own inverse element
X^X=0
• XOR operation is invertible
Example
57 = 111001
19 = 010011
57 = 111001
57 & 19 = 17 19 = 010011
17 = 010001
57 = 111001
57 | 19 = 59 19 = 010011
59 = 111011
57 = 111001
19 = 010011
57 ^ 19 = 17 42 = 101010
Bitwise Shift
We can shift all bits in a number to the left or right with
<<
and >> ( unrelated to cin and cout !! )
n << k = n * 2k n >> k = ⌊ n / 2k ⌋