0% found this document useful (0 votes)
20 views

Lecture-1-Prefix SumsBit ManipulationTime Complexity

Uploaded by

nabaye3390
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture-1-Prefix SumsBit ManipulationTime Complexity

Uploaded by

nabaye3390
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

HEADSTART

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.

Complexity is an upper bound for the number of steps


algorithm requires as a function of the input size. an
O(1)
Operations
Basic operations are those which are performed by the
computer in a constant time.

+-*/% Arithmetic Operators

< <= > >= Relational Operators

== != && || ! Logical Operators

& | << >> ~ Bitwise Operators

^ Assignment Operators

= += -= Unary Operators

*= /= %= ++
Complexity in
loops
Time complexity of each iteration of a loop gets multiplied
by the number iterations the loop performs.

for (int i = 1; i <= n; i++)


{ O(n) operation
// constant time code
here
}
Nested Loops

for (int i = 1; i <= n; i++) {


for (int j = 1; j <= m; j+
+) { O(n*m)
// constant time code here operation
}
}
More Loops

for (int i = 1; i*i <= n; i+


+) { O(√n) operation
// constant time code
here
}

As i only goes upto √n , the loop iterates only √n times


Recursion

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

O( n log(n) ) - 2 * 10^7 operations


O( n √n ) – 10^9 operations

O( n^2 ) – 10^12 operations


O( 2^n ) – 2^1000000 operations
Time Complexity for
Competitive Programming
Most sites allow upto 2 * 10^8 operations in 1
second.

So, for n = 2 * 10^5

n, n log(n) , n√n will pass

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 .

f(n) = 10*n^2 +5*n +1


For c = 11 and n0 = 6, f(n) ≤ c*(n^2) ∀ n , n ≥ n0

So f(n) = O(n^2)
Quiz
!
What's time complexity?

1. Measuring the the amount of memory an algorithm consumes

2. Measuring the number of operations an algorithm performs

3. The time it takes you to solve a problem


Quiz !!

What does big O notation express?

1. The best case scenario

2. The worst case scenario

3. The average scenario


Quiz !!
!
What time complexity would it take to find the
sum of all numbers in an array?

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.

Decimal Representation Binary Representation

0 = ( 22 * 0 ) + ( 2 1 * 0 ) + ( 2 0 000

*)
1 = (22 * 0) + (21 * 0) +(20 * 1) 00

2 = (22 * 0) + (21 * 1) +(20 1


* 0)
3 = (22 * 0) + (21 * 1) +(20 01
* 1)
3 = (22 * 1) + (21 * 0) +(20 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
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 !! )

<< shifts bits to the left.


Ex. 26 << 2 = 104
As 26 = 11010, shifting it left twice gives 1101000
= 104.

>> shifts bits to the right.


Ex. 26 >> 2 = 6
As 26 = 11010, shifting it right twice gives
110 = 6.
Left vs Right
Left Shift ( n << k ) Right Shift ( n >> k )
k
Equivalent to multiplying n by 2k Equivalent to dividing n by2
. .

n << k = n * 2k n >> k = ⌊ n / 2k ⌋

We are appending k new zeroes at We are cutting away k digits from


the end. the end.
Useful Techniques

string to_binary(int n){


string ans = "";
for (int bit = 30; bit > -1;
bit--) {
if((1<<bit)&n) ans+='1';
else ans+='0';
}
return ans;
}
Prefix
Sum
Proble
m
Given an array A of n integers, you will be asked q
queries. In each query, you will be given two integers L
and R such that 0<=L<=R<=n-1.

For each query, you have to evaluate the sum of


the elements of the subarray [L:R] i.e.
A[L] + A[L+1] + … + ………… + … + A[R-1] + A[R].
Naive
int main() {

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.

Thus, Pref[L-1] = A[0] + A[1] + ……… + A[L-1] - 1)


Pref[R] = A[0] + A[1] + ………. + A[R-1] + A[R] - 2)

Subtracting 1) from 2) , we get:


A[L] + A[L+1] + …………………. + A[R-1] + A[R] = Pref[R] -
Pref[L-1]

If we have a precalculated prefix sum array, we can answer


each subarray sum query without iterating over the elements.

Time Complexity = O(n) + O(q)


int main () {
int N ;
cin >> N;
for ( int i = 0 ; i < N ; i++ ) {
cin >> a[i] ;
}
int pref[N];
// pref[i] = a[0] + a[1] + ...... + a[i]
pref[0] = a[0] //Initialising pref[0] with a[0]
for ( int i = 1 ; i < N ; i++ ) {
pref[i] = pref[i-1] + a[i] ; // Important !!
}
int q;
cin >>q;
while ( q-- ){
int l , r ;
cin >> l >> r ;
if ( l == 0 ) { cout << pref[r] << endl; }
else { cout << pref[r] - pref[l-1] <<
endl; } }
return 0 ;
}
Proble
m
Given an array A of n integers, you will be asked q
queries. In each query, you will be given two integers L
and R such that 0<=L<=R<=n-1.

For each query, you have to evaluate the bitwise xor of

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

Now, for any whole number x,


x ^ x = 0 and x ^ 0 = x.

Using these two properties, we can say that


A[L] ^A[L+1] …………………. ^ A[R-1] ^ A[R] = Pref[R] ^
Pref[L-1]
Variatio
● ns
Prefix XOR Array

● Similarly, prefix sum approach can be applied to

subarray multiplication queries as well.

● In fact, any invertible operation can be optimized

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

You might also like