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

How to get size of array without calling the function again?

I have the code below, two classes, one is the main class and the other is class1 which has the function foo1() this function make too many iterations over an ArrayList<> . the foo1() function is called once in the main class, and then the size…
Alex
  • 267
  • 1
  • 3
  • 18
-1
votes
1 answer

Calculating the space complexity of for loops

I have a piece of code of which space complexity is to be calculated in Big-O notation int a = 0, b = 0; for (i = 0; i < N; i++) { a = a + rand(); } for (j = 0; j < M; j++) { b = b + rand(); } The rand() is an…
natzelo
  • 66
  • 7
-1
votes
1 answer

Search Insert Position Good Approach

I was doing this problem on leetcode and have created this solution for it in java and it was submitted successfully. class Solution { public int searchInsert(int[] nums, int val) { for(int i=0;i
-1
votes
1 answer

What is the space complexity of a recursive fibonacci algorithm without considering stack calls?

If we don't consider stack call memory, then what is the space consumed by the recursive fibnonacci? I read it here and it says 0(N) but I am confused whether we should include stack memory or not while considering the space. The pseudocode: int…
Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98
-1
votes
1 answer

How is the space complexity O(m+n) here?

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. class Solution: def mergeTwoLists(self, l1, l2): if l1 is None: return l2 …
user11138349
-1
votes
1 answer

Java: Reading data from a queue and convert into desired output

I have a fixed size blocking queue A which contains Customer object. The queue size is 1000. public class Customer { private final String accountId; private Double value = null; private Customer(final String accountId, Double value) { …
codemaster001
  • 183
  • 1
  • 16
-1
votes
1 answer

Space complexity of finding non-repeating character in string

Here is a simple algorithm exercise. The problem is to return the first non-repeating character. For example, I have this string: 'abbbcdd' and the answer is 'a' because 'a' appears before 'c'. In case it doesn't find any repeated characters, it…
claudiopb
  • 1,056
  • 1
  • 13
  • 25
-1
votes
1 answer

Space complexity of algo

Is the space complexity of this function N^2 as the output is a linked list? I am learning about space complexity in school and am stuck on this question. def myHealthcare(record): l2=[] count=0 # num of records generated and the specific…
Ali
  • 3
  • 1
-1
votes
1 answer

Longest Subsequence problem if the lengths are different

Let the input sequences be X[0..m-1] and Y[0..n-1] of lengths m and n respectively. And let L(X[0..m-1], Y[0..n-1]) be the length of LCS of the two sequences X and Y. Following is the recursive definition of L(X[0..m-1], Y[0..n-1]). If last…
-1
votes
1 answer

Python generator time complexity log(n)

In python3 range built with help of generators Logarithmic Time — O(log n) An algorithm is said to have a logarithmic time complexity when it reduces the size of the input data in each step. example if we are printing first 10 digits with help of…
user6882757
-1
votes
2 answers

Fastest Method for Find Multiplication Array ( by Minimum Number of Operations)

An interview question: I have an array: for example this one: a,b,c,d,e,f,g!=0 List_1=[a,b,c,d,e,f,g] and I wan to find an optimum method with least operators to find this…
-1
votes
1 answer

Is there an easy way to work out the Big O value for time and space complexity?

I have been learning about sorting algorithms and understand the idea of time and space complexity. I was wondering if there was a fairly quick and easy way of working out the complexities given the algorithm (one that may even be quick enough to do…
-1
votes
1 answer

Runtime and space complexity of the recursive determinant algorithm for a n x n matrix

I am trying to figure out the runtime and space complexity of the algorithm below. Some say that the runtime complexity of this is O(n!) and I am guessing it is because there are n! recursive calls for a recursive algorithm that solves for a n*n…
-1
votes
3 answers

Count frequencies of all elements in array in O(1) extra space and O(n) time

Problem Description: I found this problem in my Algorithms Assignment . It wants me to find the frequencies of all the elements of an array in O(n) time and O(1) space. Array Can be anything like Ar[]={1,6,3,78,4,6,1} After Thinking a little bit i…
-1
votes
2 answers

what's the Time Complexity and Auxiliary space for this method?

What's the time complexity and auxiliary space for this method? Can anyone please tell and explain me the results? public Set findRepeatedValues(List list){ Set set = new HashSet<>(); Set repeatedValues = new HashSet<>(); for…