Search⌘ K
AI Features

Solution Review: Big (O) of Nested Loop with Subtraction

Explore how to analyze the time complexity of nested loops by examining each statement's execution frequency in C#. Learn to calculate running time and simplify to Big O notation, focusing on O(n squared) complexity.

We'll cover the following...

Solution

C#
namespace Chapter_1
{
class Challenge2
{
static void Main(string[] args)
{
int n = 10;
int sum = 0;
float pie = 3.14f;
for (int i = n; i >= 1; i -= 3) // O(n/3)
{
Console.WriteLine(pie); // O(n/3)
for (int j = n; j >= 0; j--) // O((n/3)*(n+1))
{
sum += 1; // O((n/3)*(n+1))
}
}
Console.WriteLine(sum);
return;
}
}
}

On line 10 in the outer loop, int i=n; runs once. i>=1; gets executed n3+1 ...