I have a piece of code of which space complexity is to be calculated in Big-O notation
int a = 0, b = 0;
for (i = 0; i < N; i++) {
a = a + rand();
}
for (j = 0; j < M; j++) {
b = b + rand();
}
The rand()
is an O(1) space.
I think the answer should be O(max(M,N)) but the answer in my textbook is O(1). Shouldn't the space depend on N and M?
Space complexity here does not depend on N and M. Space complexity depends on int
so the Space Complexity is O(1)
.
Time Complexity is O(max(M,N))
and O(n)
is just enough, no need for too much details (i mean here on max(M,N))