Nth Fibonacci Number
You are given an integer ‘N’, your task is to find and return the N’th Fibonacci number using matrix exponentiation.
Since the answer can be very large, return the answer modulo 10^9 +7.
Fibonacci number is calculated using the following formula:
F(n) = F(n-1) + F(n-2),
Where, F(1) = F(2) = 1.
For Example:
For ‘N’ = 5, the output will be 5.
Input Format:
The first line contains a single integer ‘T’ denoting the number of test cases to be run. Then the test cases follow.
The first line of each test case contains a single integer ‘N’, representing the integer for which we have to find its equivalent Fibonacci number.
Output Format:
For each test case, print a single integer representing the N’th Fibonacci number.
Return answer modulo 10^9 + 7.
Output for each test case will be printed in a separate line.
Note:
You are not required to print anything; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 100
1 <= N <= 10^9
Time Limit: 1 sec.
Follow Up:
Can you solve it in Time Complexity better than O(N)?
Be the first one to answer
Add answer anonymously...
Top Walmart Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Walmart Software Developer
Stay ahead in your career. Get AmbitionBox app
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