You are on page 1of 3

06/11/2021, 20:53 Challenge 1: Big (O) of Nested Loop with Addition - Data Structures for Coding Interviews in C#

Challenge 1: Big (O) of Nested Loop with


Addition
Compute the Big O complexity of an algorithm that involves nested loops where the loop
variables increase with addition.

We'll cover the following

• Problem statement
• Code snippet

Problem statement#
Compute the Big O complexity of the code snippet given below. It is better
to solve it on a piece of paper, and then see if your answer matches the
correct option!

Code snippet#

1 namespace Chapter_1
2 {
3     class Challenge_1
4     {
5         static void Main(string[] args)
6         {
7             int n = 10;
8             int sum = 0;
9             float pie = 3.14F;
10             for (int i = 0; i < n; i += 3) // O(n/3)
11             {  
12                 Console.WriteLine(pie);        // O(n/3)
13                 for (int j = 0; j < n; j += 2) // O((n/3)*(n/2))
14                 {    
15                     sum += 1;        // O((n/3)*(n/2))
16                     Console.WriteLine(sum);   // O((n/3)*(n/2))
17 }
https://www.educative.io/courses/data-structures-interviews-cs/m2MMRvzkxDO 1/3
06/11/2021, 20:53 Challenge 1: Big (O) of Nested Loop with Addition - Data Structures for Coding Interviews in C#
17                 }
18             }
19         }
20     }
21 }

A nested loop with addition


(/learn)

If you have computed the time complexity of the code snippet above,
answer the following question, and see if your result matches the correct
answer!

Q What’s the Big(O) of the program written above?

A) O(n)

B) O(nlog3 n)

C) O(log3 n)

D) O(n2 )

Submit Answer

Reset Quiz

https://www.educative.io/courses/data-structures-interviews-cs/m2MMRvzkxDO 2/3
06/11/2021, 20:53 Challenge 1: Big (O) of Nested Loop with Addition - Data Structures for Coding Interviews in C#

Back Next

Common Complexity Scenarios Solution Review: Big (O) of Nested Lo…

Completed

Report an Issue

https://www.educative.io/courses/data-structures-interviews-cs/m2MMRvzkxDO 3/3

You might also like