You are on page 1of 2

Assignment No.

01
Student ID:
Fall 2023
BC210424626
CS502- Fundamentals of Algorithms

Question No. 1
Solution:
Here is the simple Algorithm
1. Read the values of a, d, and n
2. Initialize a variable sum and assign 0 to it.
3. Initialize a loop from i = 0 to n-1
4. Calculate the ith term, term_i = a + i * d
5. Add term_i to the sum
6. Print term_i
4. Print the sum of the n terms
Question No. 2
Solution:

1. Outermost Loop i started from 1 to n. So, it has a time complexity of O(n).

2. The middle loop j starts from 1 to 4 for each value of outerloop i. So, the number of iterations is
constant, that’s why it is O(1).

3. The innermost loop k starts from 10 to 2 and deduct 2 in each iteration of i and j. The number of
iterations is also constant here, so its time complexity is also O(1).

4. The print statement also has time complexity O(1), because it has constant number of iterations
respective of each loop.

5. The overall time complexity is calculated by product of time complexities of each loop. So,

O(n) * O(1) * O(1) * O(1) = O(n).

Therefore, the overall time complexity of the provided pseudocode is O(n).

You might also like