Skip to content

Commit

Permalink
Merge pull request #419 from codelord09/main
Browse files Browse the repository at this point in the history
Created Fibonacci Series using recursive(DP)
  • Loading branch information
Sayansree committed Oct 1, 2021
2 parents e4ec5aa + 8bcff80 commit 7b3fce0
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions 21. Dynamic Programming/Fibonacci_Series_recursive_DP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//Top-Down Approch ( Recursive approach)

#include <bits/stdc++.h>
using namespace std;

#define mx 1000000

int dp[mx + 1];

int fib(int n)
{
if (n == 0 || n == 1)
return n;

if (dp[n] != -1)
{
return dp[n];
}

int res;

res = fib(n - 1) + fib(n - 2);

dp[n] = res;

return dp[n];
}

int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 1; i <= mx; ++i)
{
dp[i] = -1;
}
cout << fib(n) << endl;
}
return 0;
}

0 comments on commit 7b3fce0

Please sign in to comment.