Nth Fibonacci Number | Practice | GeeksforGeeks

Given a positive integer n, find the nth fibonacci number. Since the answer can be very large, return the answer modulo 1000000007.

Example 1:
Input: n = 2
Output: 1 
Explanation: 1 is the 2nd number
of fibonacci series.

Example 2:
Input: n = 5
Output: 5
Explanation: 5 is the 5th number
of fibonacci series.


Your Task:  
You dont need to read input or print anything. Complete the function nthFibonacci() which takes n as input parameter and returns nth fibonacci number.

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

Constraints:
1<= n <=1000
0
gulshankumaransh9794 days ago
#python dp --space optimization TC-O(n) SC- O(1)
 
def nthFibonacci(self, n):       prev1=1       prev2=0       for i in range(1,n):           curr= prev1+prev2           prev2=prev1           prev1=curr       return prev1%1000000007
0
akkeshri140420016 days ago
long long int nthFibonacci(long long int n){
       // code here
       long long int mod=1e9+7;
       vector<int>dp(n+1);
       dp[0]=0;
       dp[1]=1;
       for(int i=2;i<n+1;i++){
           dp[i]=(dp[i-1]+dp[i-2])%mod;
       }
       return dp[n];
       
   }
0
ankushks1451 week ago
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
    long long int n,a=0,b=1;
    cin>>n;
    if(n==1){
    cout<<a<<endl;
    }
    else if(n==2){
    cout<<b<<endl;
    }
    else{
         long long int a=1,num;
        for(int i=3;i<=n;i++){
             num+=a+b;
             b=num;
             a=b;
        }
        cout<<num<<endl;
    }
}
 
 
0
priyxnshi1 week ago
class Solution { public:   long long int nthFibonacci(long long int n){       // code here       int a=0;       int b=1;       int c;       for(int i=2;i<=n;i++){           c=(a+b)%1000000007;           a=b;           b=c;       }       return c;   }};
0
ravi1010prakash1 week ago
 long long int nthFibonacci(long long int n){   long long int t[n+1];    t[0]=0;    t[1]=1;        for(long long int i=2;i<=n;i++)    t[i]=(t[i-1]%1000000007+t[i-2]%1000000007)%1000000007;    return t[n]%1000000007;   }}; long long int nthFibonacci(long long int n){   long long int t[n+1];    t[0]=0;    t[1]=1;        for(long long int i=2;i<=n;i++)    t[i]=(t[i-1]%1000000007+t[i-2]%1000000007)%1000000007;    return t[n]%1000000007;   }};
0
adityadixit70541 week ago
Java solution:
 static long nthFibonacci(long n){
        // code here
        long[] fib=new long[(int)n+1];
        fib[0]=0;
        fib[1]=1;
        for(int i=2;i<=n;i++){
            fib[i]=fib[i-2]+fib[i-1]%1000000007;
        }
        return fib[(int)n]%1000000007;
    }
0
khyatigupta3691 week ago
Space Optimisation with Tabulation O(n,1)
long long int nthFibonacci(long long int n){
        // 1. recursive solution O(2^n, 1) {stack space is very large in recursion here}
        // 2. iterative solution O(n, 1)
        // 3. dp - top down (recursion + memoisation) O(n, n)
        // 4. dp - bottom up (Tabulation) O(n,n)
        // 5. Space Optimisation with Tabulation O(n,1)
        
        // 5. code
        
        // step 1
        
        long long int prev0 = 0, prev1 = 1, cur;
        
        // step 2f
        for(long long int i = 2; i <= n; i++){
            cur = prev1 + prev0;
            cur = cur % 1000000007;
            prev0 = prev1;
            prev1 = cur;
        }
        
        return cur;
    }
0
khyatigupta3691 week ago
dp - bottom up (Tabulation) O(n,n)
long long int nthFibonacci(long long int n){
        // 1. recursive solution O(2^n, 1) {stack space is very large in recursion here}
        // 2. iterative solution O(n, 1)
        // 3. dp - top down (recursion + memoisation) O(n, n)
        // 4. dp - bottom up (Tabulation) O(n,n)
        
        
        // 4. code
        
        // Step 0th
        vector<long long int> dp(n+1);
        
        // step 1
        dp[0] = 0;
        dp[1] = 1;
        
        // step 2
        for(long long int i = 2; i <= n; i++){
            dp[i] =dp[i-1] + dp[i-2];
            dp[i] = dp[i] % 1000000007;
        }
        
        return dp[n];
    }
0
khyatigupta3691 week ago
Top Down : Recursion + Memoisation O(n, n)(Time,Space)
 
class Solution {
  private:
  long long int helper(long long int n, vector<long long int> &dp){
        // base case
        if(n <= 1)
            return n;
        
        // memoization
        if(dp[n] != -1)
            return dp[n];
            
        dp[n] = helper(n-1, dp) + helper(n-2, dp);
        // very very large, therefore returning answer modulo 1e9
        return dp[n]%1000000007;
  }
  public:
    long long int nthFibonacci(long long int n){
        // 1. recursive solution O(2^n, 1) {stack space is very large in recursion here}
        // 2. iterative solution O(n, 1)
        // 3. dp - top down (recursion + memoisation) O(n, n)
        
        // 3. code
        vector<long long int> dp(n+1);
        for(int i = 0; i <= n; i++){
            dp[i] = -1;
        }
        return helper(n, dp);
        
    }
};
0
anuj19301 week ago
Using Tabulation Method To optimize the solution
TC=O(n);
SC=O(1);
 
long long int nthFibonacci(long long int n){       // code here       long long int prev2=0;       long long int prev=1;       for(long long int i=2;i<=n;i++)       {           long long int curi=(prev2+prev)%1000000007;           prev2=prev;           prev=curi;       }       return prev;   }
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.