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?