-1
int fib(int n){   // let n = 4;
  int f[n+1];     // array length becomes 5
  f[0] = 0;         
  f[1] = 1;
  for(int i = 2 ; i <= n ; i++ ){
     f[i] = f[i-1] + f[i-2];
  }
  return f[n];    
}

Now, The Time Complexity of this program is coming out to be O(n) or Theta(n) But, what will be the Space Complexity and Auxilary space?

For results on internet it showing SC = theta(n) and aux space also thetha(n) but how??

if we see that the space created by us for fibonacci series is (n+1) so by removing constant the Aux space becomes theta(n) but the space Complexity (by my calculation) is thetha(1) because this program taking an integer as input and giving back an integer as output so it come up to be constant i.e. Theta(1), but every where it is theta(n) How? Please Explain>>>

Visualization solution for this problem is it correct??

  • The space complexity is also O(n) because you are using an array of n+1 length in memory to compute the sequence. – Diogo Sousa Sep 13 '22 at 07:40
  • *"because this program taking an integer as input and giving back an integer as output"* - irrelevant for (space) complexity. What happens *inside* is what matters, not the end result. Otherwise a `findMax(array)` implementation would be in constant time since it only returns a single integer. – luk2302 Sep 13 '22 at 07:42
  • @DiogoSousa the array of length n+1 is comes under auxiliary space, I'm confused with space Complexity. – bhavay sen Sep 13 '22 at 09:54
  • @luk2302 as if we see at the memory level, for the function call a variable space created for value n which is constant in every call, and the extra space that is auxiliary space is (n+1) ~ theta(n).... So, Space Complexity = theta(1) and Auxiliary space = theta(1)?? Is it right?? – bhavay sen Sep 13 '22 at 09:57
  • 1
    `int[] f = new int[n + 1];` is the java syntax. That algorith has also space complexity _O(n)_. It could have been written with just 2 variables, giving _O(1)_ space complexity. (As you are only using two values for the next newer value.) – Joop Eggen Sep 13 '22 at 09:58
  • Yes @JoopEggen but if i search for the solutions on Internet for this problem it coming with space Complexity of theta(n) or O(n). – bhavay sen Sep 13 '22 at 10:00
  • Doesn't matter what some solutions on the internet say, you create an (n+1)-sized array: O(n) / Theta(n) space complexity, end of discussion. – luk2302 Sep 13 '22 at 10:04
  • @luk2302 Please Check the rough memory map visualization for this program in above image in Question. – bhavay sen Sep 13 '22 at 19:51

1 Answers1

1

With that array:

int[] f = new int[n + 1];

You have Space Complexity O(n).

But you need to keep only the last two elements in memory:

int fib(int n){   // let n = 4;
    int a = 0;
    int b = 1;       
    int c = a + b;  
    for (int i = 2 ; i <= n; i++) {
        a = b;
        b = c;
        c = a + b;
    }
    return c;    
}

In that algorithm the space complexity is O(1).

Actually same non-trivial space and time complexity is suspicious.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138