Search⌘ K
AI Features

Solution: Big O of a Nested Loop with Subtraction

Explore how to analyze the time complexity of nested loops with subtraction using Big O notation. Understand the process of calculating iterations and see why the complexity results in O(n squared). This lesson helps you confidently evaluate algorithm efficiency in coding interviews.

We'll cover the following...

Solution #

C# 9.0
int n = 10; // 'n' can be anything, this is just an example
int sum = 0;
double pie = 3.14;
for (int i = n; i > 1; i -= 3)
{
System.Console.WriteLine(pie);
for (int j = n; j > 0; j--)
{
sum += 1;
}
}
System.Console.WriteLine(sum);

Explanation

The variable var gets set to nn, then n3n-3 ...