adplus-dvertising
frame-decoration

Question

What is the time, space complexity of following code:
int a = 0, b = 0;
for (i = 0; i < N; i++) 
{
a = a + rand();
}
for (j = 0; j < M; j++) 
{
b = b + rand();
}

a.

O(N * M) time, O(1) space

b.

O(N + M) time, O(N + M) space

c.

O(N + M) time, O(1) space

d.

O(N * M) time, O(N + M) space

Answer: (c).O(N + M) time, O(1) space

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What is the time, space complexity of following code: