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 number inside the inner loop.
- Observe symmetry in the pattern or check if a pattern is a combination of two or more similar patterns.
To analyze the time complexity of this program, let's break it down step by step:
- The outer loop runs
N
times, whereN
is the input to the function. - For each value of
i
from1
toN
, the inner loop runsi
times.
Inside the inner loop, the operations are constant time operations: assigning values to start
, printing, and toggling start. These operations do not depend on the input size N
.
So, the time complexity of this program can be approximated by considering the number of iterations of the inner loop:
1 + 2 + 3 + ... + N
This is an arithmetic series, and the sum of the first N
positive integers can be computed using the formula:
sum = N * (N + 1) / 2
.
Therefore, the time complexity of the program is
where N is the number of rows/lines (horizontally).
- The main factor contributing to space complexity is the storage of variables that hold temporary values or data that changes with the input size.
- The
i
variable takes constant space as it's just an integer index for the outer loop. - The
start
variable is used to store either 0 or 1, which is constant space. - The
j
variable is similar to i, just an integer index for the inner loop.
Overall, the space complexity of the program remains constant regardless of the input size N
.
Therefore, the space complexity is