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
19
votes
2 answers

How to calculate the space complexity of function?

I understood the basic that if I have a function like this: int sum(int x, int y, int z) { int r = x + y + z; return r; } it requires 3 units of space for the parameters and 1 for the local variable, and this never changes, so this is…
Ankur Anand
  • 3,873
  • 2
  • 23
  • 44
18
votes
2 answers

Is imperative Quicksort in situ (in-place) or not?

Quicksort is often described as an in situ (in-place) algorithm, despite the fact that it requires O(log n) stack space. So does in situ mean "requires less than O(n) additional space", or does stack space generally not count as space complexity…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
17
votes
6 answers

How to make space complexity as O(1)

I am trying to answer for the below question : You have an array of integers, such that each integer is present an odd number of time, except 3 of them. Find the three numbers. so far I came with the brute force method : public static void…
17
votes
8 answers

Algorithm with O(n log n) time and O(1) space complexity vs O(n) time and O(n) space complexity

I am curious to know which algorithm is better : Algorithm with O(n log n) time and O(1) space complexity Algorithm with O(n) time and O(n) space complexity Most of the algorithm which are solved in O(n long n) time and constant space can be…
Anil Kumar K K
  • 1,395
  • 1
  • 16
  • 27
17
votes
2 answers

Suffix Arrays vs Suffix Trees

I just wanna know, when a suffix tree is superior to an enhanced suffix array. After reading Replacing suffix trees with enhanced suffix arrays i don't see a reason to use suffix trees anymore. Some methods can get complicated, but you can do…
17
votes
5 answers

Microsoft Interview: transforming a matrix

Given a matrix of size n x m filled with 0's and 1's e.g.: 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0 if the matrix has 1 at (i,j), fill the column j and row i with 1's i.e., we get: 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 Required complexity: O(n*m)…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
16
votes
9 answers

Finding contiguous ranges in arrays

You are given an array of integers. You have to output the largest range so that all numbers in the range are present in the array. The numbers might be present in any order. For example, suppose that the array is {2, 10, 3, 12, 5, 4, 11, 8, 7, 6,…
garima
  • 5,154
  • 11
  • 46
  • 77
16
votes
3 answers

Regarding in-place merge in an array

I came across the following question. Given an array of n elements and an integer k where k < n. Elements {a0...ak} and {ak+1...an} are already sorted. Give an algorithm to sort in O(n) time and O(1) space. It does not seem to me like it can be done…
Sid
  • 1,093
  • 1
  • 9
  • 11
14
votes
2 answers

Space-efficient algorithm for checking if strings with backspaces are equal?

I was recently asked this question in an interview: Given two strings s and t, return if they are equal when both are typed into empty text editors. # means a backspace character. Input: S = "ab#c", T = "ad#c" Output: true Explanation: Both S…
flash
  • 1,455
  • 11
  • 61
  • 132
14
votes
2 answers

How to determine memory and time complexity of an algorithm?

I am not good at determining time and memory complexities and would appreciate it if someone could help me out. I have an algorithm, here and I am not sure what its time and memory complexities would be. Function sample(k) IF k < 2 Return…
user3138212
  • 211
  • 1
  • 3
  • 8
14
votes
3 answers

Find a number with even number of occurrences

Given an array where number of occurrences of each number is odd except one number whose number of occurrences is even. Find the number with even occurrences. e.g. 1, 1, 2, 3, 1, 2, 5, 3, 3 Output should be: 2 The below are the…
Green goblin
  • 9,898
  • 13
  • 71
  • 100
13
votes
4 answers

Find a duplicate in array of integers

This was an interview question. I was given an array of n+1 integers from the range [1,n]. The property of the array is that it has k (k>=1) duplicates, and each duplicate can appear more than twice. The task was to find an element of the array that…
13
votes
3 answers

Why does radix sort have a space complexity of O(k + n)?

Consider an array with n numbers that has maximum k digits (See Edit). Consider the radix sort program from here: def radixsort( aList ): RADIX = 10 maxLength = False tmp, placement = -1, 1 while not maxLength: maxLength = True #…
skr
  • 914
  • 3
  • 18
  • 35
13
votes
4 answers

Sorting in linear time and in place

Suppose that n records have keys in the range from 1 to k. Write an algorithm to sort the records in place in O(n+k) time. You may use O(k) storage outside the input array. Is your algorithm stable? if we use counting sort to we can do it in…
13
votes
4 answers

Deleting all nodes in a binary tree using O(1) auxiliary storage space?

The standard algorithm for deleting all nodes in a binary tree uses a postorder traversal over the nodes along these lines: if (root is not null) { recursively delete left subtree recursively delete right subtree delete root } This…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
1
2
3
61 62