![]() |
VOOZH | about |
Given an integer n. The task is to find the first n Fibonacci Numbers.
Examples :
Input: n = 3
Output: 0 1 1Input: n = 7
Output: 0 1 1 2 3 5 8
Find nth fibonacci number by calling for n-1 and n-2 and adding their return value. The base case will be if n=0 or n=1 then the fibonacci number will be 0 and 1 respectively.
0 1 1 2 3 5 8
Time Complexity: O(n*2n)
Auxiliary Space: O(n), For recursion call stack.
To find Fibonacci numbers by maintaining two variables (
f1andf2) to represent consecutive Fibonacci numbers. Starting with 0 and 1, it iteratively computes the next Fibonacci number, appends it to the result list, and updates the variables accordingly.
0 1 1 2 3 5 8
Time Complexity: O(n)
Auxiliary Space: O(1)