Nth Element Of Modified Fibonacci Series

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.
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.
CodingNinjas
author
2y

I solved the problem using the dynamic programming concept creating a 1-D array and using the previous 2 values to calculate the next final leading to the final result.

CodingNinjas
author
2y
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 block...read more

CodingNinjas
author
2y
Matrix Multiplication.

The N’th term of a Fibonacci series can be computed using matrix multiplication as terms of the Fibonacci series follows the following relation.


(Fn-1 Fn) = ( Fn−2 Fn−1 ) ⋅ ( 0 ...read more

Add answer anonymously...
Optum Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter