Search⌘ K
AI Features

Solution Review: Nested Loop with Multiplication (Basic)

Explore how to analyze the time complexity of nested loops with multiplication in C#. Learn to determine the number of executions and calculate the overall Big O notation, helping you assess and optimize algorithm efficiency.

We'll cover the following...

Solution

The code for this challenge is reproduced below:

C#
namespace Chapter_1
{
class Challenge_4
{
static void Main(string[] args)
{
int n = 10;
int sum = 0;
int counter = 1;
float pie = 3.14f;
while (counter < n) // O(log3 n)
{
Console.WriteLine(pie); // O(log3 n)
for (int j = 1; j < n; j += 2)// O((log3 n)*(n/2))
{
sum += 1;// O((log3 n)*(n/2))
}
counter *= 3;// O(log3 n)
}
Console.WriteLine(sum);
return;
}
}
}

Time complexity

The outer loop in this problem runs log3(n)log_3(n) ...