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

Why is the space complexity of this algorithm O(n)?

This is the algorithm for finding the maximum contiguous sub-sequence sum using the DP approach. The algorithm seems to be quite okay but it was mentioned that this has space complexity O(n). Why? To me it seems this algorithm has O(1) space…
bigbong
  • 541
  • 4
  • 12
5
votes
1 answer

The space complexity of this algorithm that loops over an array

I am asked to provide the asymptotic space and time complexity of the below algorithm in appropriate terms for an initial input number of arbitrary length (as distinct from a constant 12 digits). 1 for i = 2 to 12 2 if d[i] == d[i-1] 3 …
Hien Tran
  • 1,443
  • 2
  • 12
  • 19
5
votes
2 answers

How to optimize space for SPOJ AIBOHP

I was doing this particular problem AIBOHP and used a dp approach based on checking the ends of a substring of length i starting from 1. Although my time complexity is fine at O(n^2) but space is taking too much because of which I am getting RTE if…
Kavish Dwivedi
  • 735
  • 6
  • 24
5
votes
3 answers

Expected space consumption of skip lists

What is the expected space used by the skip list after inserting n elements? I expect that in the worst case the space consumption may grow indefinitely. Wikipedia says “Space O(n)”. How can this be proven one way or another?
Lunatech
  • 197
  • 2
  • 8
4
votes
4 answers

Calculating the space complexity of a C-function?

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
Prashant Bhardwaj
  • 1,203
  • 2
  • 18
  • 26
4
votes
2 answers

Is there a more efficient way to do all(is.finite())?

For logical and integer vectors, there is !anyNA(x). For double vectors, there is length(x) == 0L || all(is.finite(range(x))). For complex vectors, is there an analogous, O(1)-allocating expression equivalent to all(is.finite(x)), in base R? I…
Mikael Jagan
  • 9,012
  • 2
  • 17
  • 48
4
votes
3 answers

What factors determine the memory used in lambda functions?

=SUM(SEQUENCE(10000000)) The formula above is able to sum upto 10 million virtual array elements. We know that 10 million is the limit according to this question and answer. Now, if the same is implemented as Lambda using Lambda helper function…
4
votes
2 answers

What is a succinct rank data structure? How does it work?

I've heard of a class of data structures called succinct rank data structures. What do these data structures do? What does "succinct" mean here? And how do they work?
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
4
votes
3 answers

What is the time complexity of this function that iterates through a list a creates a dictionary?

I have a function that rearranges an input list in a certain way and returns the output list. I am confused about what the time and space complexity of the function will be. Below is the code: def rearrange_list(inp_list): d = {} output_list…
4
votes
1 answer

Recursive Runtime - Space Complexity (pg.44 of Cracking the Coding Interview)

On pg. 44 of Cracking the Coding Interview, there is the following algo: int f(int n) { if (n <= 1) { return 1; } return f(n - 1) + f(n - 1); } The book says that this has time complexity of O(2^n) and space-complexity of O(n).…
4
votes
3 answers

Reversing a singly linked list when a block size is given

There is a singly connected linked list and a block size is given.For eg if my linked list is 1->2->3->4->5->6->7->8-NULL and my block size is 4 then reverse the first 4 elements and then the second 4 elements.The output of the problem should be…
Poulami
  • 1,047
  • 3
  • 13
  • 27
4
votes
4 answers

Linked List v.s. Binary Search Tree Insertion Time Complexity

Linked List A linked list’s insertion time complexity is O(1) for the actual operation, but requires O(n) time to traverse to the proper position. Most online resources list a linked list’s average insertion time as…
4
votes
0 answers

Read numbers in line one by one with O(1) space

Many coding challenges have multiple numbers in the same line, often with a first line telling how many numbers there are in the multi-number line: 4 31 415 9 26 Usually I just read the entire line, then .split() it and map the strings to…
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
4
votes
0 answers

Sieve of Euler space complexity in Haskell

I was given 2 different algorithms written in Haskell aimed to generate the first k primes. As the title suggests, they are the Sieve of Eratoshenes and Euler. I am trying to understand why Euler implementation uses so much memory. What I thought so…
4
votes
2 answers

Why is there no 2* in the space complexity recurrence S(n) = 2*S(n/2)?

from typing import List def recfunc(xs: List[int]) -> List[int]: if len(xs) < 2: return xs a = list() b = list() for x in range(len(xs)): if x < len(xs) // 2: a.insert(0, x) else: …
TWstud
  • 81
  • 6