AlgoInsight

DSA problem

Fibonacci Series up to N Terms

EasyMathPattern: RecurrencebeginnerFree

Given an integer N, generate the first N terms of the Fibonacci series. The series starts with 0 and 1, and every subsequent term is the sum of the two preceding terms, producing 0, 1, 1, 2, 3, 5, 8, 13, ....

The Fibonacci sequence appears throughout computer science and nature - from sunflower spirals and shell curves to the analysis of Euclid's GCD algorithm and Fibonacci heaps. The efficient way to build it is iterative: keep just two variables for the previous two numbers and slide them forward on each step. This two-variable sliding window is the same idea you will reuse in many dynamic-programming problems.

The key insight is that you never need to store the whole sequence to extend it - only the last two values matter at any point, which keeps the auxiliary space constant.

Examples

Example 1
Input: N = 8
Output: [0, 1, 1, 2, 3, 5, 8, 13]
Explanation: The first 8 Fibonacci numbers, starting from 0 and 1, where each term is the sum of the previous two.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

mathfibonacci

Browse all DSA problems