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

Practice Set Loop Complexities

This document discusses the time complexities of various nested loops. It contains 20 questions about loop complexities, each with the answer provided. Common complexities discussed include O(n), O(log n), O(n log n), and O(n^2).

Uploaded by

panchaldev.1454
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Practice Set Loop Complexities

This document discusses the time complexities of various nested loops. It contains 20 questions about loop complexities, each with the answer provided. Common complexities discussed include O(n), O(log n), O(n log n), and O(n^2).

Uploaded by

panchaldev.1454
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Practice Set: Loop Complexities

Takeaway from class-

Increment => 𝑂(𝑛) (𝑖 = 𝑖 + 𝑐)


Doubling => 𝑂(log 𝑛) (𝑖 = 2 ∗ 𝑖)
Exponentiation => 𝑂(log log 𝑛) (𝑖 = 𝑖 ∗ 𝑖)
Question 1: What will be the time complexity of following nested loop ?
Question 2

What will be the time complexity of following nested loop ?


Answer: 𝜃(𝑛 𝑙𝑜𝑔𝑛 ! )
Question 3

What will be the time complexity of following nested loop ?


Answer: 𝜃(𝑛𝑙𝑜𝑔𝑛)
Question 4

What will be the time complexity of following nested loop ?


Answer: 𝜃(𝑛𝑙𝑜𝑔𝑛)
Question 5

What will be the time complexity of following nested loop ?


Answer: 𝜃(𝑛 𝑙𝑜𝑔𝑛 ! ) Inner most while loop will run log 𝑖 times.

For i = 1, inner most loop will run log 1 times


For i =3, inner loop will run log 3 times
For i = 3! , inner loop will run log 3! times
For i = 3" , inner loop will run log 3" times

.
.
For i = 3# = 𝑛, inner loop will run log 3# times (here k = log 𝑛)

(We will consider outermost for loop after a while, lets focus on inner for loop)
Inner for loop will run log1 + log3 + log 3! + log 3" + …..+ log 3#
= log 1.3.3! . 3" . 3 … .3# = log 3$%!%"%⋯#
!
= log 3# = 𝑘 ! = 𝑙𝑜𝑔𝑛 !
Question 6

What will be the time complexity of following nested loop ?


Answer: 𝜃(𝑛" )
Question 7

What will be the time complexity of following nested loop ?


Answer: 𝜃(𝑛# )
Question 8

What will be the time complexity of following loop ?


Answer: 𝜃( 𝑛)
Question 9

What will be the time complexity of following loop ?


Answer: 𝜃(𝑛𝑙𝑜𝑔𝑛) (
Inner loop will run !) times.

For i = 1, inner loop will run n/2 times


For i =2, inner loop will run n/(2x2) times
For i =3, inner loop will run n/(2x3) times
For i =4, inner loop will run n/(2x4) times

.
.
For i =n, inner loop will run n/(2xn) times

Time Complexity :
( ( ( ( ( (
= ! + !×! + !×" + !×+ + !×, + … … . . + !×( =
= 𝑛/2 [1/2 + 1/3 + 1/4 + ⋯ . . 1/𝑛] = 𝑛𝑙𝑜𝑔𝑛
Question 10
What will be the time complexity of following loop ?
Answer: 𝜃(𝑙𝑜𝑔𝑛)
Question 11

What will be the time complexity of following loop ?


Answer: 𝜃(𝑛𝑙𝑜𝑔𝑛) Inner loop will run
(-!. ( (
= − 2 = times.
. . .

For j = 1, inner loop will run n/1 times


For j =2, inner loop will run n/2 times
For j =3, inner loop will run n/3 times
For j =4, inner loop will run n/4 times

.
.
For j =n, inner loop will run n/n =1 time

Time Complexity :
( ( ( ( ( (
= $ + ! + " + + + , + ……..+(
= 𝑛 [1 + 1/2 + 1/3 + 1/4 + ⋯ . . 1/𝑛] = 𝑛𝑙𝑜𝑔𝑛
Question 12

What will be the time complexity of following loop ?


Answer: 𝜃(𝑛𝑙𝑜𝑔𝑛)
Question 13

What will be the time complexity of following loop ?


Answer: 𝜃(𝑛)
For i = 1, inner most loop will run 1 times
For i =2, inner loop will run log 2 times
For i = 2! , inner loop will run 2! times
For i = 2" , inner loop will run 2" times

.
.
For i = 2# = 𝑛, inner loop will run 2# times (here k = log 𝑛)
Question 14

What will be the time complexity of following loop ?


Answer: 𝜃(𝑛)
Question 15

What will be the time complexity of following loop ?


Answer: 𝜃(𝑛 log 𝑛)
Question 16
What will be the time complexity of following loop ?
Answer: 𝜃(1)
Question 17
What will be the time complexity of following loop ?

https://courses.cs.washington.edu/courses/cse332/21au/exams/oldExams/cse332-midterm-12wi-soln.pdf
Answer: 𝜃(𝑛)
Question 18 What will be the time complexity of following nested loop ?
Answer: 𝜃(𝑛" )
Question 19

What will be the time complexity of following loop ?


Answer: 𝜃( 𝑙𝑜𝑔𝑛 ! )
Question 20

What will be the time complexity of following loop ?


Answer: 𝜃(𝑛 𝑙𝑜𝑔𝑛 ! )
Thank you

You might also like