Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views2 pages

Assignment No 1 CS502

The document provides solutions for two questions related to time complexity in programming. It details the time complexity for various statements in a code snippet, concluding that the total time complexity is O(n*m). The second question appears to involve a sequence of numbers, but lacks context for further analysis.

Uploaded by

faizanali107f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Assignment No 1 CS502

The document provides solutions for two questions related to time complexity in programming. It details the time complexity for various statements in a code snippet, concluding that the total time complexity is O(n*m). The second question appears to involve a sequence of numbers, but lacks context for further analysis.

Uploaded by

faizanali107f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

]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

You might also like