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
3
votes
1 answer

Find greatest amount using dynamic programming

Given a coin n(<=10^9), I can exchange it for 3 coins:n/2,n/3 and n/4 (where / represents floor division). What is the greatest amount I can make? My code is: #include using namespace std; int a[10000000]; long int coin(long int n){ …
3
votes
2 answers

Proof: Check if two integer arrays are permutations of each other using linear time and constant space

I was interested in creating a simple array problem with running time and space constraints. It seems that I have found a solution to my problem. Please read the initial description comment of the problem in the following java code: /* * Problem:…
3
votes
3 answers

Space Complexity Example

So I am wondering when objects (or primitives) are created inside a for loop, how does that contribute to the space complexity ? For instance, heres a code example: public boolean checkUnique(String p){ int term = -1; int len = p.length(); …
Aerole
  • 105
  • 2
  • 10
3
votes
1 answer

Would serializing to disk count as aux space complexity?

I have a simple program to serialize a binary tree. Code: public static void serialize(TBST tree, String fileName) throws FileNotFoundException, IOException { /* * using only 1 file will create a lot of confusion in coding. …
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
3
votes
2 answers

What is the space complexity of enumerating subsets?

This is based off other question I had on space complexity. Permutation Space Complexity This is a solution to my problem of enumerating(naming) all sets. (Tested it, it works) public static void subsets(Set s) { Queue
committedandroider
  • 8,711
  • 14
  • 71
  • 126
3
votes
2 answers

What is the space complexity of this algorithm?

This is problem 9.6 from Cracking the Coding Interview (5th edition) Implement an algorithm to print all valid combinations of n-pairs of parenthesis EXAMPLE Input: 3 Output:"((())), (()()), (())(), ()(()), ()()()" Here is the algorithm I…
3
votes
2 answers

Trie data structure space usage in Java

I just want to double check the total space that a Trie data structure could have in the worst case. I thought it would be O(N*K) where N is total number of nodes and K is the size of the alphabet (points to other tries), but people keep telling me…
peter
  • 8,333
  • 17
  • 71
  • 94
3
votes
1 answer

Median of Medians space complexity

I implemented an nth_number selection algorithm using Medians of Medians. On wikipedia, it states that it's space complexity is O(1) I had to store the medians in a temporary array in order to find the median amongst those medians. How would you be…
Kamran224
  • 1,584
  • 7
  • 20
  • 33
3
votes
1 answer

If a>=b then O(a+b)=O(a)?

I'm trying to understand better the idea of O(n), so I'm wonder about this: If we know that a>=b so O(a+b)=O(a)? I know that O(a)+O(a)=O(2a)=O(a), but I'm wondering if it's true for something that it's smaller then a, I mean - if O(a+b)=O(a). I…
3
votes
1 answer

Space complexity of a function which builds n-sized array after calling it

I'm trying to learn how to estimate memory complexity of certain functions. And now I'm having a problem with one peculiar case. So let's say we're building a function, something like this: let f x = (fun x -> x :: [1;2;3]);; And we're never…
qiubit
  • 4,708
  • 6
  • 23
  • 37
3
votes
3 answers

Space complexity of a given recursive program

Consider the following C-function: double foo (int n) { int i; double sum; if (n == 0) return 1.0; else { sum = 0.0; for (i = 0; i < n; i++) sum + = foo (i); return sum; } } The space complexity…
POOJA GUPTA
  • 2,295
  • 7
  • 32
  • 60
3
votes
1 answer

Is there a mathematical way to calculate the optimal growth factor for a dynamic array?

For a class project we need to find the optimal growth factor of a priority queue based on cyclic dynamic array. I've ran some tests but they were inconclusive. We were told there is a mathematical way of showing it but I couldn't find it…
talki
  • 194
  • 1
  • 8
3
votes
4 answers

Reducing space complexity of Sieve of Eratosthenes to generate primes in a range

After getting through some of the SO posts, I found Sieve of Eratosthenes is the best & fastest way of generating prime numbers. I want to generate the prime numbers between two numbers, say a and b. AFAIK, in Sieve's method, the space complexity is…
3
votes
1 answer

Redis Data Structure Space Requirements

What is the difference in space between sorted sets and lists in redis? My guess is that sorted sets are some kind of balanced binary tree, and lists are a linked list. This means that on top of the three values that I'm encoding for each of them,…
nnythm
  • 3,280
  • 4
  • 26
  • 36
3
votes
1 answer

breaking vs returning from inside a for loop

Below are two snippets. Notice the ONE AND ONLY difference between the programs is that one break to return and the other return immediately. I understand it's good design practice to have one point of exit inside a method. But I am not worrying…
kasavbere
  • 5,873
  • 14
  • 49
  • 72