23. Fibonacci series up to Nth term
The problem can be found at the following link: Question Link
My Approach
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 and Auxiliary Space Complexity
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.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 getlost01/gfg-potd repository.
Last updated