F(n) = F(n-1) + F(n-2),
Where, F(1) = F(2) = 1.
For ‘N’ = 5, the output will be 5.
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.
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.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 10^5
Time Limit: 1 sec.
Can you solve it in Time Complexity better than O(N)?
Ans of ‘i-th’ number is = ‘ans[i-1] + ans[i-2]’, so
Observe that if we take the ‘N-1’th power of the matrix ‘COEF’ = [ [0, 1], [1, 1] ], then the bottom right element of the resultant matrix i.e ‘RESULT[1][1]’ will give the ‘N’th Fibonacci number.
More details can be found at the given link.
https://cp-algorithms.com/algebra/fibonacci-numbers.html
Algorithm: