17. Print first n Fibonacci Numbers
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
Simple go with the flow of question statement.
If n <= 2, return a vector containing n ones.
Otherwise, initialize a vector 'out' of size n and set the first two elements to 1 (since the first two Fibonacci numbers are always 1).
Use a loop to calculate the remaining Fibonacci numbers and store them in the 'out' vector.
Time Complexity: The loop runs for n
iterations to calculate the Fibonacci numbers, which is O(n)
.
Auxiliary Space Complexity: I use a vector of size n
to store the Fibonacci numbers, which is O(n)
.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.