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
hrithikjain988919 hours ago
Total Time Taken:
0.01/1.27
 long long int nthFibonacci(long long int n){       // code here        int first=0;      int sec=1;       long long int add=0;       n--;       while(n--)       {           add=(first+sec)%1000000007;           first=sec;           sec=add;       }       return add;   }
0
devashishbakare2 weeks ago
JAVA Using Tabulation(DP)
 
 
    static long nthFibonacci(long n){
        
      long dp[] = new long[(int)n+1];
      int pow = 1000000007;
      
      dp[0]=0;
      dp[1]=1;
      
      for(int i=2; i<=n ;i++)
      {
          dp[i]=((dp[i-1]%pow)+(dp[i-2]%pow))%pow;
      }
      return dp[(int)n]%pow;
0
moaslam8264 weeks ago
SIMPLE C++ SOLN
typedef long long int ll;const ll m=1e9+7;class Solution { public:   long long int nthFibonacci(long long int n){       // code here       vector<ll>v(n);       v[0]=1; v[1]=1;       for(int i=2;i<n;i++){           v[i]=v[i-1]%m+v[i-2]%m;           v[i]%=m;       }       return v[n-1];   }};
0
annanyamathur1 month ago
ong long int nthFibonacci(long long int n){     long long int dp[n+1];      dp[0]=0;      dp[1]=1;      for(int i=2;i<=n;i++)      {          dp[i]=((dp[i-1]%1000000007)+(dp[i-2]%1000000007))%1000000007;      }      return dp[n]%1000000007;   }
+1
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION (0.1/1.1 SEC)
class Solution {   static long nthFibonacci(long n){          int N=(int)n;      long dp[]=new long[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[N];   }}
0
madhukartemba1 month ago
JAVA SOLUTION:
class Solution {
    static long nthFibonacci(long n){
        
        if(n<=2) return 1;
        long pp = 1, p = 1;
        
        long cur = 0;
        
        for(long i=3; i<=n; i++)
        {
            cur = pp + p;
            cur = (cur%1000000007);
            pp = p;
            p = cur;
        }
        
        return cur;
        
    }
}

0
swastikp17111 month ago
// 0 1 1 2 3 5 8 13 21 34 55 89
class Solution {
    static long nthFibonacci(long n){
        int mod = 1000000007;
        long secondPrev = 0L;
        long  firstPrev= 1L;
        long ans=0L;
        
        if(n<=firstPrev)
                return n;
        for(long i=2;i<=n;i++){
            ans=(firstPrev+secondPrev)%mod;
                secondPrev=firstPrev;
                firstPrev=ans;
        }
        return ans;
    }
}
0
programmerhere1 month ago
why runtime error?
 
 
class Solution { public:   long long int nthFibonacci(long long int n){       long long int i=1;        stack<int> temp;      while(i<=n){           if(i==1)      temp.push(1);      if(i==2)      temp.push(1);      else{      long long int store=temp.top();      temp.pop();      long long int s=temp.top()+store;      temp.push(store);      temp.push(s);            }      i++;      }       return temp.top();   }};
0
sahuop21211 month ago
def nthFibonacci(self, n):
        # code here 
        lst = [0,1]
        for i in range(n):
           lst.append(lst[i]+lst[i+1])
        
        return lst[n]%1000000007
0
amiransarimy1 month ago
Python Solutions without using Recursive
 
def nthFibonacci(self, n):
       
       if n == 1 or n == 2:
           return 1
       else:
           f1 = 1
           f2 = 1
           f3 = 0
           
           for idx in range(n-2):
               f3 = f1 +f2
               f1 = f2
               f2 = f3
           
           return f3%1000000007
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.