algorithm analysis - complex dependant nested loop First of all, I know there are many questions like this on the site. But I think this case is a bit different.

Consider the following code:

int i, j, k;

for (i = 1; i &lt;= n; i++){   
    for (j = 1; j &lt;= (n-i); j++) {
        System.out.print(" ");
    }

    for (k = 1; k &lt;= (i-j); k++) {
        System.out.print(i);
    }

    System.out.println();
}


What would be the time complexity of this code? It seems like O(n^2) to me but I can't justify it properly.

How our professor told us to compute complexity is just by adding all the summations together from every line. 

$$
(n+1) + \sum_{j=1}^{N-i} + \sum_{j=1}^{N-i} + \sum_{k=1}^{i-j} + \sum_{k=1}^{i-j} + n
$$

However, I've never seen examples with summations like $\sum_{j=1}^{N-i}$, It's always either $\sum_{j=1}^{N-1}$ or some constant number being subtracted from N, which is easier to sum. So, How would I solve this? And is it O(n^2)?
The number of print statements executed is
$$
\sum_{i=1}^n \left[ \left(\sum_{j=1}^{n-i} 1\right) + \left(\sum_{k=1}^{i-(n-i+1)} 1\right) + 1 \right],
$$
where the second sum is zero if $1 &gt; i-(n-i+1)$.

The running time is proportional to the number of print statements, so it remains to compute the value of the expression above, or at least to give a good estimate. This I leave to you.