What is the Big-O of a nested loop, where number of iterations in the inner loop is determined by the current iteration of the outer loop?

mmcdole picture mmcdole · Dec 12, 2008 · Viewed 58.2k times · Source

What is the Big-O time complexity of the following nested loops:

for(int i = 0; i < N; i++) 
{
    for(int j = i + 1; j < N; j++)
    {
        System.out.println("i = " + i + " j = " + j);
    }

}

Would it be O(N^2) still?

Answer

Alex Gaynor picture Alex Gaynor · Dec 12, 2008

Yep, it's still O(n^2), it has a smaller constant factor, but that doesn't affect O notation.