Skip to content

Instantly share code, notes, and snippets.

@aershov24
Created October 13, 2020 05:28
Show Gist options
  • Select an option

  • Save aershov24/c0a57fa699c3935b35fcd6656b6c96f0 to your computer and use it in GitHub Desktop.

Select an option

Save aershov24/c0a57fa699c3935b35fcd6656b6c96f0 to your computer and use it in GitHub Desktop.
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
class Solution {
int fib(int N) {
if (N <= 1) {
return N;
}
int[][] A = new int[][]{{1, 1}, {1, 0}};
matrixPower(A, N-1);
return A[0][0];
}
void matrixPower(int[][] A, int N) {
if (N <= 1) {
return;
}
matrixPower(A, N/2);
multiply(A, A);
int[][] B = new int[][]{{1, 1}, {1, 0}};
if (N%2 != 0) {
multiply(A, B);
}
}
void multiply(int[][] A, int[][] B) {
int x = A[0][0] * B[0][0] + A[0][1] * B[1][0];
int y = A[0][0] * B[0][1] + A[0][1] * B[1][1];
int z = A[1][0] * B[0][0] + A[1][1] * B[1][0];
int w = A[1][0] * B[0][1] + A[1][1] * B[1][1];
A[0][0] = x;
A[0][1] = y;
A[1][0] = z;
A[1][1] = w;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment