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

Does creating a subarray use O(1) extra space in java

if there is given array, ex. arr Java code: int arr[] = {10,23,65,15,45}; int n = arr.length; //i.e n=5 int k = 2; int temp[] = new temp[k]; and if I create new array if size k which will be subarray of arr and will always have size less than…
-4
votes
2 answers

Capital Movement | Competitive Programming

PROBLEM Suppose there are 'n' cities in a country out of which, one is the capital. All of the cities are connected through 'n-1' roads that it's possible to travel between any two cities using those roads. Population of cities Pi is provided in the…
-4
votes
2 answers

Determining the Time and Space complexity of a given code

I wish to write code for this problem: Given a string, remove duplicates from it in O(n) time and O(1) space. Now, I have written a code to remove duplicates from a string. public class RemoveDuplicates { public static void main(String…
subham soni
  • 274
  • 1
  • 5
  • 17
-4
votes
1 answer

What is the space complexity in the following snippet?

def percolate(int i): l = 2*i + 1 r = 2*i + 2 max = i temp = new node() if (l!=-1 and arr[l] > arr[i]): max = l if (r!=-1 and arr[r] > arr[max]): max = r if (max != i): temp = arr[i] arr[i] = arr[max] arr[max] =…
-4
votes
1 answer

How to reduce time and space complexity of the following code

Optimize (Reduce the space and Time complexity)this function as much as possible. public void q1(String str, int[] arr) { String local = "findnumber"; for(int i=0; i
-8
votes
1 answer

How can i reduce the time complexity of this program?

This program takes n integers in the list and then gives the count of the next t numbers entered by the user. If the numbers are in the list it prints the count and if it is not then it gives the "NOT PRESENT" message. I want to know if I can reduce…
Gaurav Bahadur
  • 189
  • 2
  • 14
1 2 3
61
62