We always use nested loops for printing the patterns.
- For the outer loop, we count the number of lines/rows and loop for them.
- Next, for the inner loop, we focus on the number of columns and somehow connect them to the rows by forming a logic such that for each row we get the required number of columns to be printed.
- We print the
*
inside the inner loop. - Observe symmetry in the pattern or check if a pattern is a combination of two or more similar patterns.
The time complexity can be estimated by analyzing the number of iterations performed by the nested loops.
The inner loop runs a maximum of N
times in the first iteration of the outer loop, then N-1
times in the second iteration, and so on. Therefore, the total number of iterations for the inner loop is:
This is an arithmetic series whose sum is given by the formula:
Hence, the time complexity of the nested loops is
where N is the number of rows/lines (horizontally).
The space complexity of the program is determined by the amount of memory required to store the variables used in the program.
In this case, the program only uses a few integer variables (N, i, j)
, and their memory usage is constant regardless of the input value N
.
Therefore, the space complexity of the program is