C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# What is the space complexity of this algorithm (n or log(n))?

By : gmukesh
Date : November 20 2020, 11:01 PM
fixed the issue. Will look into that further The total number times function b is called is O(n), but space complexity is O(log(n)).
Recursive calls in your program cause the execution stack to grow. Every time a recursive call takes place all local variables are pushed to the stack (stack size increases). And when function comes back from recursion the local variables are poped from the stack (stack size decreases).
code :

Share :

## Why the space complexity of this algorithm is O(1)

By : jedimasterfrancis
Date : March 29 2020, 07:55 AM
it fixes the issue Although you are right to say that that recursive implementation of the algorithm requires O(n) space because of the stack space needed, it only uses tail recursion, meaning it can be reimplemented to use O(1) space with a loop:
code :
``````int leastCommanAncestor(struct node* root, int n1, int n2)
while (1)
{
/* If we have reached a leaf node then LCA doesn't exist
If root->data is equal to any of the inputs then input is
not valid. For example 20, 22 in the given figure */
if(root == NULL || root->data == n1 || root->data == n2)
return -1;

/* If any of the input nodes is child of the current node
we have reached the LCA. For example, in the above figure
if we want to calculate LCA of 12 and 14, recursion should
terminate when we reach 8*/
if((root->right != NULL) &&
(root->right->data == n1 || root->right->data == n2))
return root->data;
if((root->left != NULL) &&
(root->left->data == n1 || root->left->data == n2))
return root->data;

if(root->data > n1 && root->data < n2)
return root->data;
if(root->data > n1 && root->data > n2)
root = root->left;
else if(root->data < n1 && root->data < n2)
root = root->right;
}
}
``````

## What is the time complexity and space complexity of this algorithm to find Anagrams?

By : nimers
Date : March 29 2020, 07:55 AM
hop of those help?
And would space complexity be O(N) because I am creating N copies of the 26 length array?

## How to find exact Time complexity and Space Complexity for below new Quick sort algorithm

By : NoodTug
Date : March 29 2020, 07:55 AM
I wish this helpful for you Time complexity for the above algorithm seems to be O(n^2).
As you can see there are 2 nested for loops. external one runs from x = 0, y = n to x < y, and in each step it reduces x++ and y--. While the other inner loop goes from x to y.

## What is the Time Complexity, Space complexity and Algorithm for strstr() function in C++?

By : joedeloy
Date : March 29 2020, 07:55 AM
hope this fix your issue I was curious about the cost of using the default, old fashioned strstr() function in C++. What is its Time and Space complexity? Which algorithm does it use? We have other algorithms with below Worst Case Time and Space complexity : Let n = length of string, m = length of pattern , In the C standard it just says, in §7.24.5.7:
Synopsis
code :
`````` #include <string.h>
char *strstr(const char *s1, const char *s2);
``````

## Will this algorithm be O(1) or O(n) space complexity

By : user3193005
Date : March 29 2020, 07:55 AM
This might help you Since your code loops through the array once, it has O(n) time complexity. Storage only maintains 1 variable so it has O(1) space complexity. I'm assuming you left off the return statement for the sake of the question, otherwise, be sure you include that too.