Questions tagged [space-complexity]

The space complexity of an algorithm quantifies the amount of memory taken by an algorithm to run as a function of the size of the input to the problem. The space complexity of an algorithm is commonly expressed using big O notation, which suppresses multiplicative constants and lower order terms.

The space complexity of a program (for a given input) is the number of elementary objects that this program needs to store during its execution. This number is computed with respect to the size n of the input data.
Formally, for an algorithm T and an input x, DSPACE(T, x) denotes the number of cells used during the (deterministic) computation T(x). We will note DSPACE(T) = O(f (n)) if DSPACE(T, x) = O(f (n)) with n = |x | (length of x).

923 questions
4
votes
1 answer

Do recursive calls count into space complexity?

When an algorithm doesn't use more than a constant amount of auxiliary memory but does have O(log(N)) recursive calls (each one taking some extra space on the stack), is that algorithm's space complexity O(1) or O(log(N))?
NPS
  • 6,003
  • 11
  • 53
  • 90
4
votes
3 answers

Time complexity to invoke virtual functions in C++

Assume an inheritance hierarchy with type Base that has F virtual functions, D different derived types, and assuming that each derived type overrides all of the virtual functions. What is the time complexity to invoke one of the virtual functions…
G Gill
  • 1,087
  • 1
  • 12
  • 24
4
votes
3 answers

How to find time and space complexity for this code?

I have implemented a code to construct pattern count tree. How do i find its time and space complexity? class PCTree { public static void main(String args[])throws IOException { BufferedReader input=new BufferedReader(new…
GkWizZ
  • 33
  • 1
  • 4
4
votes
1 answer

Puzzle: Who wins the game?

A group of children form a ring. The first child is selected and they start counting clockwise from that child until a fixed number (n, which is given at the starting of the game) is reached. When the count reaches n, the child on the nth spot is…
Vishnu Vivek
  • 1,819
  • 1
  • 20
  • 30
4
votes
1 answer

Is there a space efficient implementation of mergesort?

I just coded up this working version of mergesort: static int[] merge(int[] first, int[] second){ int totalsize = first.length + second.length; int[] merged_array = new int[totalsize]; int i = 0, firstpointer = 0, secondpointer = 0; …
ElectronAnt
  • 2,115
  • 7
  • 22
  • 39
3
votes
1 answer

when is Big-Oh(n) = Omega(n) ? Is it same as theta(n)?

This question looks simple to me, but just wanted to see whether i am moving in the right direction. Is it as simple as saying when n =1 ??
pa1geek
  • 258
  • 7
  • 19
3
votes
1 answer

Auxilary Space Complexity Across Iterations

Suppose we have the function below: def ReverseStr(s, k): """ s: list of characters (length n) k: integer """ for i in range(0, len(s), 2*k): s = s[:i] + s[i:i+k][::-1] + s[i+k:] return s In this function, each…
LateGameLank
  • 113
  • 5
3
votes
2 answers

Find palindrome python space complexity?

Given the following code in python which checks whether an string of n size is palindromic: def is_palindromic(s): return all(s[i] == s[~i] for i in range(len(s) // 2)) What is the space complexity of this code? Is it O(1) or O(n)? The all…
KaliTheGreat
  • 579
  • 6
  • 17
3
votes
2 answers

Are all O(n) algorithms O(n²) too?

The formal definition of the Big O notation is that if we have a function f(n) (for time and space of an algorithm) and another function g(x), and there are constants c and no such that c*g(n) > f(x) for all n > no, then f(n) = O(g(n)). But using…
3
votes
2 answers

Why does method chaining vs methods in each line cause the process to be killed or run out of memory?

Recently, I was handling a large text file (~10GB) and trying to replace some characters in Python. I tried these two versions: f = open('myFile.txt', 'r') filedata = f.read() filedata = filedata.replace(',', ' ').replace('-', ' ').replace('_', '…
Ricky
  • 61
  • 8
3
votes
1 answer

How to efficiently reuse released ids in id sequence

Lets say we have PID generator that generated 7 ids for 7 processes {1,2,3,4,5,6,7}. Process 3 and 6 have finished and id 3 and 6 are up for grabs. Now we start three new processes. How to implement efficient algorithm that will first assign them …
3
votes
1 answer

What's the worst-case space complexity for the All Paths Sum problem for an unbalanced tree?

Here's the problem statement as stated on educative.io. Given a binary tree and a number ‘S’, find all paths from root-to-leaf such that the sum of all the node values of each path equals ‘S’. I understand the solution given to the problem and the…
3
votes
3 answers

Why O(n log n) is greater than O(n)?

I read that O(n log n) is greater than O(n), I need to know why is it so? For instance taking n as 1, and solving O(n log n) will be O(1 log 1) = O(0). On the same hand O(n) will be O(1)? Which actually contradicts O(n log n) > O(n)
user14673850
3
votes
1 answer

Space Complexity with O(n^2)

So, Everbody is familiar with constant O(1) or Linear O(N) Space Complexity. But I have a question that, is there any case when Space Complexity of an Algorithm is proportional to O(NLogn) or O(N^2). If that's possible, what is the advantage of…
Vaibhav Angi
  • 57
  • 1
  • 4
3
votes
1 answer

Does there exist a stable sorting algorithm that can sort a binary array in O(n) time complexity and O(1) auxiliary space complexity?

I know how to write an algorithm that sorts a binary array in O(n) time and O(1) auxiliary space complexity, but it doesn't seem to be stable. void binarySort(int arr[], int arr_size) { int i; int count_zeros=0; for (i=0 ; i
Mathphile
  • 185
  • 1
  • 9