Nth Element Of Modified Fibonacci Series

Easy
0/40
Average time to solve is 15m
profile
Contributed by
11 upvotes
Asked in companies
Tata Consultancy Services (TCS)OptumInfosys

Problem statement

You have been given two integers ‘X’ and ‘Y’ which are the first two integers of a series and an integer ‘N’. You have to find the Nth number of the series using the Fibonacci rule given by f(x) = f(x - 1) + f(x - 2).

The answer may be very large, return it after modulus 10 ^ 9 + 7.

Note:

The series is 1-based indexed.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer T denoting the number of test cases. Then each test case follows.

The first line of each test case contains three space-separated integers ‘X’, 'Y', and ‘N’, respectively where ‘X’ and ‘Y’ represent the first and second element of the series while N represents which number of the sequence we have to find out.
Output Format:
For each test case, print a single line containing a single integer denoting the Nth number of the series.

The output of each test case will be printed in separate lines.
Note
You are not required to print the expected output; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= N <= 10 ^ 18
-10 ^ 6 <= X, Y <= 10 ^ 6

Time limit: 1 sec.
Sample Input 1:
2
2 3 4
-1 -2 1
Sample output 1:
8
1000000006
Explanation of Sample output 1:
For the first test case, the series will be 2, 3, 5, 8, 13. So the 4th element will be 8.

For the second test case, the series will be -1, -2, -3, -5, -8. So, the 1st element will be -1. As we are supposed to modulate this with 10^9 + 7, we will get 1000000006.
Sample Input 2:
1
2 3 4
Sample output 2:
8
Hint

Try to use dynamic programming. Build the answer in a top-down manner.

Approaches (2)
Dynamic Programming

Let’s define a dp array of size N, where dp[i] represents the i-th Fibonacci number. For each block, let’s compute the answer in a top-down fashion, starting with the leftmost blocks (dp[0] and dp[1]). We will iterate through the array for i = 2 to N and then we can fill the array in a top-down manner like:

 

                                  dp[i] = dp[i-1] + dp[i-2].

 

Since, during this method, we only need the previous two states, we can store the last two states in two variables instead of using a whole array.

 

Here is the algorithm:

  1. Initialise variable sum = 0 that stores the sum of the previous two values.
  2. Now, run a loop from i = 2 to N-1 and for each index update value of sum = X + Y  and X = Y, Y = sum.
  3. Finally, return the sum, which is the required Nth element.
Time Complexity

O(N), where ‘N’ represents which number of the sequence we have to find out.

 

As we are iterating over the whole array only once so the time complexity will be O(N).

Space Complexity

O(1)

 

In the worst case, only a constant extra space is required.

Code Solution
(100% EXP penalty)
Nth Element Of Modified Fibonacci Series
Full screen
Console