Loop & Time
complexity
Prepared by :
Youssef Ebrahim AKA EngJoe
14 Oct. 2024
8:30 p.m.
Outline
Loops
while Loop
do-while
Loop
for Loop
Nested
loops
Statements
break
continue
return
Time complexity
O(n)
O(n^2)
Loops
Loops, or iterative statements in C++, allow you to execute
a block of code repeatedly until a specific condition is met.
while Loop
while Loop
The while loop is used when the number of iterations is not known
beforehand, and the code block is executed as long as the specified
condition is true.
The simplest kind of loop is the while-loop. Its syntax is:
Problem A
Problem B
do while Loop
The do-while loop is similar to the while loop, but the
condition is checked after the execution of the code block,
ensuring the block runs at least once.
The simplest kind of loop is the while-loop. Its syntax is:
do while Loop
for Loop
The for loop is used when the number of iterations is known
before entering the loop. It consists of three parts:
initialization, condition, and increment/decrement
for Loop
Problem C
Problem D
loops can be nested within one another to create more
complex iteration patterns, allowing for multi-dimensional
processing or hierarchical data structures .
Nested loops
How to get this output ?
Problem E
Problem F
Break
continue
VS
"In C++, loops can be controlled using 'break' and 'continue' statements:
- 'break': Exits the loop prematurely.
- 'continue': Skips the current iteration and proceeds to the next."
Break
continue
VS
linear search
Answer
Time complexity
C++ is known for its efficiency and speed.
Typically can handle up to 10^8
operations in a reasonable time frame.
Time complexity
Time complexity
Generally, the time limit for running your code on platforms like
Hackerrank, Atcoder, Codeforces, etc. is 1-2 seconds. So, we need
to write an efficient code that passes this time limit constraint.
It represents the upper bound of a function
Used to approximate time complexity of a code
Big Oh (O)
Time complexity
If you have function f(x), then consider a function g(x) such that
f(x) <= c.g(x)
For all values of x>=x0 for some value of x0
Then we say, f(x) = O ( g(x) )
Eg. 1:
If f(n) = n - 2
f(n) <= 8.n
f(n) <= 8.g(n)
Where g(n) = n
By definition, f(n) = O(n)
Eg. 2: If f(n) = 3n^2 + 5n + 8
By definition of Big Oh, f(n) = O(n^2)
Eg. 3: If f(n) = 3n^3 + 2n
By definition of Big Oh, f(n) = O(n^3)
In polynomial functions, only see the highest degree term to find Big Oh
Time complexity
Find time complexity
of this code.
i=0; will run only 1 time
i<n; will be run n+1 times
i++ will be run n times
cout<<A[i]; will be run n times
Time complexity
= 1 + n+1 + n + n = 3n + 2
= O(n)
Time complexity: O(1) [constant]
Find time complexity
of this code.
i=0; inner loop will run 0 times
i=1; inner loop will run 1 times
i=2; inner loop will run 2 times
….
i=n-1 ; inner loop will run n-1 times
Total steps = 0 + 1 + 2 + …. + n-1
= (n * (n-1)) /2
= O(n^2)
Problem G
Find time complexity
of this code.
For i=1, 2, 4, 8, 16, ….., 2k
Let us assume that it breaks out of loop in k steps
1.(2k) > n
2k > n
k approximately log2 (n)
Time complexity: O( log2 (n) )
What is the output ?
for more
Compiler error (CE)
Indicated by compiler itself with the line number in which there is error
Wrong Answer (WA)
eg. Yes not = YES
Read the input and output format in the question very carefully
Time Limit Exceeded (TLE)
Time limit is generally 1 second. And if your code is slow to pass this time limit, you will
get this error.
Common errors in online platforms