23. Fibonacci series up to Nth term
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
Initialize an empty vector vec to store the Fibonacci series.
Initialize variables n1 and n2 with initial Fibonacci values.
If n is 0, push n1 into vec and return vec.
If n is 1, push both n1 and n2 into vec and return vec.
For i starting from 2 up to n, calculate the next Fibonacci number by adding the last two elements of vec, take modulo mod, and push it into vec.
Return the vec vector containing the Fibonacci series.
Time Complexity: The time complexity of this approach is O(N)
, because it iterates N times to generate the Fibonacci series.
Auxiliary Space Complexity: The auxiliary space complexity is O(N)
, where N is the user input.
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.