]Assignment # 1 CS 502
BC210414671
Question # 01 Solution:
Time Complexity against each statement:
Time
Statement How Many Times it Executes
Complexity
cin >> n; 1 time O(1)
cin >> m; 1 time O(1)
for (int i = 1; i <= n; i++) n times O(n)
O(1) (inside
cout << "Row " << i << ": "; n times (once per row)
O(n))
O(1) (inside
if (i % 2 != 0) n times (checked every row)
O(n))
Inside Odd Rows (if true)
for (int j = 0; even <= m && j Maximum m/2 times across all odd rows
O(m)
< m; j++) combined
cout << even << " "; even += O(1) per
Up to m/2 times per iteration
2; iteration
Inside Even Rows (else)
Approximately O(log m) times total
while (fib <= m) O(log m)
across all even rows
All statements inside while Each O(1) per iteration O(log m) total
Total Time Complexity:
O(1)+O(n)+O(m)+O(log m)
Ignoring smaller terms (since m is bigger than log(m) usually), and assuming m could be
large compared to n:
Total Time Complexity: O(n*m)
Question #2 Solution:
78 45 89 32 90 12 67 55 99 21
78 45 89 32 90 12 67 55 99 21
78 45 89 32 90 12 67 55 99 21
78 45 89 32 90 12 67 55 99 21
78 45 12 67