Hey guys so in my interview question I was given something like this below. By the way this is my code to solve fib. I want to improve this my code to eliminate repetition of fibonacci sequence that might end up being repeated at the process. For example if fib(1) and fib(1) are repeated twice, how to do I avoid this from happening so the program can advance to unique sequence being processed.
I really want to know how to improve this code. My solution is below but when I debug it, I feel like I get lost understanding what is really happening.
Thanks.
public class Fib {
public static void main(String[] args) {
System.out.print(fibonacci(14));
}
private static int fibonacci(int n) {
int fibArray[] = new int[n];
if (n <= 0) {
return 1;
} else if (n == 1) {
return 1;
} else {
fibArray[0] = fibonacci(n - 1);
fibArray[1] = fibonacci(n - 2);
if (fibArray[0] == fibonacci(n - 1)) {
return fibonacci(n - 1) + fibonacci(n - 2);
} else if (fibArray[1] != fibonacci(n - 2)) {
return fibonacci(n - 1) + fibonacci(n - 2);
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
}