1

In Java - Assuming i have a while loop that runs log n times, so time complexity is O(logn), and inside that loop i declare an array of size 2 with each iteration:

While (high > low)
Int[] arr = arrFunc(mid, 0);

So in each iteration we create an array that runs over the previous array, since it is "destroyed" by the java garbage collector. So is the space complexity o(1)? Or o(logn)?

  • It would be O(1) since you actually only need that space, wether or not java deallocs is irrelevant. You could create the array outside the loop and reuse it. Of course the O(1) only applies if the array size iteself is unrelated to `n` which is not clear from your snippet. – luk2302 Apr 23 '21 at 11:10
  • Assuming the function arrFunc(int x, int y) is of time and space complexity o(1) and returns an array of constant size 2. – Matan Amitai Apr 23 '21 at 11:24

1 Answers1

0

The space complexity concept is language agnostic.

And here, since you're creating a new structure on each iteration, you're adding as many space complexity.

If your algorithm is O(log n) in time complexity, your space complexity for this part is also O(log n)

Also, do not forget that O is the upper boundary an algorithm may reach. It is not the actual space the algorithm will use.


As noted correctly by @luk2302, if, instead of creating a new array each time, your algorithm makes it possible to reuse the existing one, then your space complexity drops to O(1)


You can confirm the assumptions made hereabove by running the following code snippets with the following VM options

-Xmx2048M -Xms64M -XX:+UseZGC

Constant Space Complexity

Here, the array is instantiated before the loop

Set<Long> set = new HashSet<>();
int[] a = new int[1_000_000];
while (true) {
    long memory = Runtime.getRuntime().totalMemory();
    set.add(memory);
    Arrays.fill(a, Integer.MAX_VALUE);
    System.out.println(set);
}

Prints

[67108864]
[67108864]
[67108864]
[67108864]
[67108864]
[67108864]
[67108864]
...

Linear Space Complexity

Set<Long> set = new HashSet<>();
while (true) {
    long memory = Runtime.getRuntime().totalMemory();
    set.add(memory);
    int[] a = new int[1_000_000];
    Arrays.fill(a, Integer.MAX_VALUE);
    System.out.println(set);
}

Prints

[67108864]
[67108864, 100663296]
[67108864, 100663296, 134217728]
[67108864, 100663296, 134217728, 167772160]
[67108864, 100663296, 134217728, 167772160, 201326592]
[67108864, 100663296, 134217728, 167772160, 201326592, 234881024]
[67108864, 100663296, 134217728, 167772160, 201326592, 234881024, 268435456]
[67108864, 100663296, 134217728, 167772160, 201326592, 234881024, 268435456, 301989888]
[67108864, 100663296, 134217728, 167772160, 201326592, 234881024, 268435456, 301989888, 335544320]
[67108864, 100663296, 134217728, 167772160, 201326592, 234881024, 268435456, 301989888, 335544320, 369098752]
[167772160, 67108864, 100663296, 134217728, 201326592, 234881024, 268435456, 301989888, 335544320, 369098752, 402653184]
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Why? If the algorithm does not need the different arrays at once but actually only needs one array of constant size and repurposes it within each iteration the space complexity is still constant, is it not? – luk2302 Apr 23 '21 at 11:11
  • Because of this `So in each iteration we create an array`. Indeed, if the array is reused, then it is `O(1)` since it is constant space no matter what the actual time complexity is. – Yassin Hajaj Apr 23 '21 at 11:12
  • But isn't the array reused if we "forget" the old array after each iteration, the space needed does not add up, we only need the storage for one array so the space complexity is actually the max of all the arrays and if all of them contain 2 values the space complexity is O(1) even if we create multiple arrays but only have one alive at each point in time. Wether or not the gc does the cleanup is of course irrelevant / language agnostic as you say. Not trying to state that you are explicitly wrong, I am just not sure / confused. – luk2302 Apr 23 '21 at 11:15
  • I am really wondering if the explicit reusage of the array is needed to state that the space complexity is constant or not. – luk2302 Apr 23 '21 at 11:17
  • 1
    Well, you're making me doubt now in fact, I'll do some research and come back if it's ok for you? In the mean time I'll delete the answer, until I'm sure. You should still be able to see it of course. – Yassin Hajaj Apr 23 '21 at 11:17
  • @luk2302 did some research and finally got the answer :D. It would be `O(1)` if the same memory address would be reused, which is not the case when creating a new array. See [this](https://cs.stackexchange.com/questions/128990/what-will-be-space-complexity-for-snippet-fori-1-to-n-int-x-10) for more information – Yassin Hajaj Apr 23 '21 at 11:33
  • I'd say both the answer and all the comments basically state that it in fact is O(1), regardless of wether or not space *actually* gets reused. The space *could* be reused since we do not reference it / pass it around / store it externally - depending on the underlying language implementation, which is irrelevant. See additionally https://stackoverflow.com/q/34487306/2442804 where it is stated much more explicitly. – luk2302 Apr 23 '21 at 11:52