logo
down
shadow

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


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 : facebook icon twitter icon
Why the space complexity of this algorithm is O(1)

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?

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

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++?

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

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.
Related Posts Related Posts :
  • Master method is applicable only for divide and conquer algorithm
  • Find the Number of Distinct Colour
  • NP - Non deterministic polynomial time
  • Handshaking lemma with 3 people?
  • K-th Smallest in Lexicographical Order
  • Finding articulation points using DFS and the bi-components algorithm
  • Sorting - How selection sort is efficient?
  • Pre-order tree walk if we have only the visited, the parent and the distance vectors
  • Count number of consecutive candies
  • Algorithms, Block Stacking - graph theory
  • Searching for a key that doesn't and has never existed O(n)?
  • Arrange n items in k nonempty groups such that the difference between the minimum element and the maximum element of eac
  • Complexity of the following recursive code
  • Dynamic Programming Implementation of Unique subset generation
  • Calculate complexity in recursive algorithm with deep limit
  • Smalltalk : best to avoid non-local returns? Algorithm rewrite
  • Simple LOTTERY Algorithm on PYTHON
  • Drawing a line between a two disjoint sets of points
  • Optimal algorithm to find exit of a maze with no information
  • Sort list of tuples based on second element while alternating over third element
  • Merge the content of two tables (looking for Matlab or Pseudo Code)
  • Find a spanning tree with maximum number of edges with same weight
  • Binary search for first occurrence of k
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com