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

How to determine the space and time complexity of these two double linked list algorithms?

I solved the next exercises having two solutions: https://www.hackerrank.com/challenges/reverse-a-doubly-linked-list First (non-recursive): /* Insert Node at the end of a linked list head pointer input could be NULL as well for empty list …
oscar.fimbres
  • 1,145
  • 1
  • 13
  • 24
-2
votes
1 answer

space complexity of free(malloc(m))

I was trying to find the space complexity of the following code: void f(int arr[], int n){ int m=0; for(int i=1; ik)?m:k; printf("1"); } …
Anna
  • 27
  • 5
-2
votes
2 answers

Time and Space complexity of recursive and non-recursive small algorithm

Consider two functions that accept as a parameter an unsigned integer and returns the number of digits of this number. One function is recursive and the other is non-recursive. In terms of complexity , which implementation is better? The language…
-2
votes
1 answer

Calculate the space complexity and time complexity of a function

As a part of my course work i need to calculate the complexity of programs. I'd like to calculate the space complexity and time complexity of the below program.How do i calculate it? It would be really helpful for me if somebody can explain it in…
-2
votes
2 answers

how space complexity of this code is O(n)?

public static boolean isUniqueChars2(String str) { boolean[] char_set = new boolean[256]; for (int i = 0; i < str.length(); i++) { int val = str.charAt(i); if (char_set[val]) return…
c2h5oh
  • 227
  • 5
  • 9
-2
votes
3 answers

Complexity of if statement

I am wondering complexity of following if statement if (isTrue()) //case 1 VS if(isTrue()==true) //case 2 And isTrue defined as boolean isTrue(){ //lots of calculation and return true false based on that. return output; } I was thinking,…
minhaz
  • 4,233
  • 3
  • 33
  • 49
-3
votes
1 answer

Time and space complexity of this algorithm for counting nodes in a complete binary tree

I have this below code for calculating the number of nodes in a complete binary tree* without visiting all the nodes. public int countNodes(TreeNode root) { if(root==null){ return 0; } int…
-3
votes
1 answer

Time and Space Complexity of Nested loop

Please consider the following algorithm - for( j = 1; j < n ; j = j * 3) { for( k = 1 ; k <= n ; k = k + 2 ) { r = i + j + k ; System.out.println(r); } } How is the time and space complexity found for this?
-3
votes
1 answer

In C++, which takes up more space, a hashmap or an array of structure? and which is faster?

in leetcode for majority elements II question, i used the hashmap to keep track of how many times each element has been repeated but in geeks for geeks i saw them use an array of structure, so i wanted to know which was faster and takes up more…
-3
votes
1 answer

Does using a temp variable while swapping two numbers have any affect on space complexity?

Ques. 1.) I was writing a code to swap two numbers using a temp variable. I don't know how this temp variable will affect the space complexity in this case. Below is the code. public void swap(int a, int b){ int temp; temp = a; a = b; b…
grayskull
  • 11
  • 1
-3
votes
1 answer

Search a book name by using prefix in less complexity java

Problem statement: Consider a library which has millions of books. Thousands of books are being added and removed each minute. The Problem is to find all the books available by using a prefix (given as input) with minimum time and space complexity…
-3
votes
1 answer

Space complexity of a sequential combination of two Algorithms

Let's say I have an Algorithm A which is a combination of 2 algorithms A1 and A2. The algorithm A consists of carrying out algorithm A1 and A2 one after another. The input to algorithm A are 2 arrays of length m and n respectively. The goal of…
Buna
  • 45
  • 4
-3
votes
1 answer

Java - Space complexity of this code

What is the space complexity of the code? I think it is O(1), since I don't technically store all the inputs into one array, i sort of split it up into 2 arrays. This code is supposed to take an array with duplicates, and find the two numbers that…
data_pi
  • 801
  • 2
  • 11
  • 30
-3
votes
3 answers

How can I find T (1) when I measure the complexity of an algorithm

Question 01: How can I find T (1) when I measure the complexity of an algorithm? For example I have this algorithm Int Max1 (int *X, int N) { int a ; if (N==1) return X[0] ; a = Max1 (X, N‐1); if (a > X[N‐1]) return a; else return…
-4
votes
1 answer

Does memory requirement of a code consist of a arithmetic computation is greater than that of code consist of low arithmetic computation in java?

`here I am writing a solution for concatenation of two arrays using but I come up with two solutions. the problems is given as below: Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and…
1 2 3
61
62