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

Cover a given range of an array

I'm trying to figure out the algorithm to implement a method and I would need some advice in the regard. I have given an array and range, A = [1, 15, 30, 40, 50] range = 10 I need to figure out the minimum number of points that would cover all the…
Arefe
  • 11,321
  • 18
  • 114
  • 168
-1
votes
1 answer

How do I calculate the number of Lines produced for the largest test-case when k = 12?

Problem: Given 6 < k < 13 integers, enumerate all possible subsets of size 6 of these integers in a sorted order. Since the size of the required subset is always 6 and the output has to be sorted lexicographically (the input is already sorted), the…
-1
votes
1 answer

How to calculate binomial coefficient time and space complexity? (iterative vs. recursive)

This is the iterative version, but it calls a recursive function. Would that have an effect on its time/space complexity? int factorial(int n) { if (n == 1) { return 1; } else { return n * factorial(n - 1); } } int…
NeVada
  • 127
  • 9
-1
votes
1 answer

How the memory requirements of the following codes are different irrespective of the code-length?

// An efficient program to randomly select a number from stream of numbers. #include using namespace std; #include #include /* A program to randomly select a item from stream[0], stream[1], .. stream[i-1]*/ int…
scoder
  • 3
  • 5
-1
votes
1 answer

Which of the following code have lesser time complexity? Please explain how to calculate it

Following is the code to calculate subsets of a given array: Bit Manipulation Method: How to analyse it? vector> subsets(vector& nums) { sort(nums.begin(), nums.end()); int num_subset = pow(2, nums.size()); …
-1
votes
2 answers

Is it better to reduce the space complexity or the time complexity for a given program?

Grid Illumination: Given an NxN grid with an array of lamp coordinates. Each lamp provides illumination to every square on their x axis, every square on their y axis, and every square that lies in their diagonal (think of a Queen in chess). Given…
user2020493
  • 129
  • 6
-1
votes
1 answer

Worst case complexity for resursive algorithm

Shouldn't all recursive algorithm have worst case (Space) of O(inf) due to potential overflow? Take Fibonacci algorithm What is the space complexity of a recursive fibonacci algorithm? The answer is O(N) but what if the input is large and stack…
Zanko
  • 4,298
  • 4
  • 31
  • 54
-1
votes
1 answer

Are there problems that cannot be solved in polynomial space?

There are problems that can't be solved in polynomial time, but are there problems that can't be solved using polynomial space?
Cauchy Schwarz
  • 747
  • 3
  • 10
  • 27
-1
votes
4 answers

Is it efficient to declare the string variable inside the loop or outside the loop?

public void see_following_values_match_with_cache_values(DataTable table){ String actual = null; String expected = null; List > maps = table.asMaps(String.class, String.class); for (Map
sophist_pt
  • 329
  • 1
  • 8
  • 19
-1
votes
1 answer

fibonacci series nth number in php with O(log N)

Looking for a solution in which complexity code is O(log N). space complexity will be O(1) I tried following function fib($a, $b, $N) { $c = ""; if ($N == 0) { return intval($a); } else if ($N == 1) { return intval($b); …
-1
votes
2 answers

Which is a better way of finding the max of no.s?

Which is a better way to write a program to find the max of 4 no.s in c/C++: using a fifth variable & comparing it to all inputs using max() function and comparing the inputs using if or suggest any other, if it has a better approach(in terms of…
dj1
  • 25
  • 3
-1
votes
1 answer

Space Complexity of merge sort

I have some confusion related to the space complexity of merge sort. It says it is O(n). However, if I look at the implementation in the wiki, it looks O(nlogn). This is because at each recursive call, I split the array into left and right. For…
user12331
  • 486
  • 7
  • 22
-1
votes
1 answer

How to determine space and time complexity of an algorithm?

I have an algorithm, I am not sure about time and space complexity. , for (i = 1 to n ) do begin R(xi) = random(o, xi, r) i++ end What is the time and space complexity in above algorithm and why? Thanks
-1
votes
1 answer

Verifying space complexity of code to eliminate duplicates

@SuppressWarnings("unchecked") public static List eliminateDuplicate(List list) { Set set = new HashSet(list); return (List) Arrays.asList(set.toArray()); } Wanted check the space complexity of the simple code above to…
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
-2
votes
2 answers

time complexity of the acc function in scheme?

I have been trying to find a tight bound time complexity for this function with respect to just one of the arguments. I thought it was O(p^2) (or rather big theta) but I am not sure anymore. (define (acc p n) (define (iter p n result) (if (<…