Fibonacci series up to Nth term

shashi
2 min readMar 23, 2024

--

EasyAccuracy: 51.0%Submissions: 25K+Points: 2

You are given an integer n, return the fibonacci series till the nth(0-based indexing) term. Since the terms can become very large return the terms modulo 109+7.

Example 1:

Input:
n = 5
Output:
0 1 1 2 3 5
Explanation:
0 1 1 2 3 5 is the Fibonacci series up to the 5th term.

Example 2:

Input:
n = 10
Output:
0 1 1 2 3 5 8 13 21 34 55
Explanation:
0 1 1 2 3 5 8 13 21 34 55 is the Fibonacci series up to the 10th term.

Your Task:
You don’t need to read input or print anything. Your task is to complete the function Series() which takes an Integer n as input and returns a Fibonacci series up to the nth term.

Expected Time Complexity: O(n)
Expected Space Complexity: O(n)

Constraint:
1 <= n <= 105

Topic Tags

RecursionFibonacciAlgorithmsDynamic Programming

Related Courses

Fork CPP Programming — Self PacedSchool Guide: Learning Roadmap For Young Geeks

Report An Issue


class Solution {

int[] Series(int n) {
int[] dp=new int[n+1];
dp[0]=0;
dp[1]=1;
for(int i=2;i<=n;i++){
dp[i] =(dp[i-1] +dp[i-2]) % 1000000007 ;
}
return dp;
}
}



/*
Fibonacci series up to Nth term
EasyAccuracy: 51.0%Submissions: 25K+Points: 2
You are given an integer n, return the fibonacci series till the nth(0-based indexing) term. Since the terms can become very large return the terms modulo 109+7.

Example 1:

Input:
n = 5
Output:
0 1 1 2 3 5
Explanation:
0 1 1 2 3 5 is the Fibonacci series up to the 5th term.
Example 2:

Input:
n = 10
Output:
0 1 1 2 3 5 8 13 21 34 55
Explanation:
0 1 1 2 3 5 8 13 21 34 55 is the Fibonacci series up to the 10th term.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Series() which takes an Integer n as input and returns a Fibonacci series up to the nth term.

Expected Time Complexity: O(n)
Expected Space Complexity: O(n)

Constraint:
1 <= n <= 105

Topic Tags
RecursionFibonacciAlgorithmsDynamic Programming
Related Courses
Fork CPP Programming - Self PacedSchool Guide: Learning Roadmap For Young Geeks


*/

--

--

shashi
shashi

No responses yet