Lecture-1-Prefix SumsBit ManipulationTime Complexity
Lecture-1-Prefix SumsBit ManipulationTime Complexity
Programming and
Algorithms Group
INTRO TO COMPETITIVE
PROGRAMMING
Time
Compl
exity
Time Complexity
Analysis
We want a method to calculate how many operations it
takes to run each algorithm, in terms of the input size
n.
Fortunately, this can be done relatively easily using Big O
notation, which expresses worst-case time complexity as
a function of n as n gets arbitrarily large.
^ Assignment Operators
= += -= Unary Operators
*= /= %= ++
Complexity in
loops
Time complexity of each iteration of a loop gets multiplied
by the number iterations the loop performs.
void f ( int n ) {
if ( n == 1 )
O(n) operation
{ return ; }
f(n-1);
}
More Recursion
void g ( int n ) {
if ( n == 1 )
{ return ; } O(2n) operation
g ( n - 1 ) ;
g ( n - 1 ) ;
}
n-1 n
1+2+4+ = 2n - 1 → O(2 )
…… + 2 operation
For large values of n
For large values of n the difference in different time
complexities becomes significant. For n = 10^6 ,
O( 1 ) - 1 operation
O( log(n) ) - 20 operations
O( √n ) – 10^3 operations
O( n ) – 10^6 operations
while
n2 , 2n , n! will fail.
Here are some conservative upper bounds on the value of
n for each time complexity. You might get away with more
than this, but this should allow you to quickly check
whether an algorithm is viable.
Formal definition of
big O notation
A function f(n) is of the order g(n)
{ written as f(n) = O( g(n) ) }
if and only if there exists positive constants c and n0
exist such that f(n) ≤ c * g(n) ∀ n , n ≥ n0 .
So f(n) = O(n^2)
Quiz
!
What's time complexity?
O(1)
O(n)
O(n^2)
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 ) + ( 2 1 * 0 ) + ( 2 0 000
*)
1 = (22 * 0) + (21 * 0) +(20 * 1) 00
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
19 =
= 111001
010011
57 | 19 = 59
59 = 111011
57
19 =
= 111001
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 ⌋
Implementation int n ;
cin >> n ;
int a[n] ;
Naively, we can iterate over for ( int i = 0 ; i < n ; i++ ) {
all the elements from the cin >> a[i];
}
index int q;
L to R, for every query and cin >> q;
print the sum. while ( q--) {
int l , r ;
cin >> l >> r ;
int sum = 0 ;
Time Complexity = O(n*q) for ( int i = l ; i <= r ; i++ ){
Too slow for sum += a[i];
}
constraints like cout<<sum<<endl;
n<=10^5 }
q<=10^5 return 0;
}
Prefix Sum
Optimisation
We create a prefix sum array (Pref) such that
Pref[i] = A[0] + A[1] + ……………… + A[i-1] + A[i] for 0<=i<=n-
1.
the
elements of the subarray [L:R] i.e.
A[L] ⨁ A[L+1] ⨁ …………. ⨁ A[R-1] ⨁A[R].
Soluti
on
Create Prefix XOR Array instead of Prefix Sum Array,
where Pref[i] = A[0] ^ A[1] …………. ^ A[i-1] ^ A[i] for all
0<=i<=n-1
by this algorithm.
AC
TIM
https://codeforces.com/problemset/problem/1
527/A
https://leetcode.com/problems/find-pivot-
https://codeforces.com/problemset/problem/1
index/
097/B
@pag_iitr discord.gg/dlulsll5Du