Answer the given question with a proper explanation and step-by-step solution. public class mathFormula { // Iterative implementation public static int iterative(int n) { if (n == 0) return 1; if (n == 1) return 2; int f1 = 1, f2 = 2, fn = 0; for (int i = 2; i <= Math.abs(n); i++) { fn = f1 - f2; f1 = f2; f2 = fn; } return (n < 0 && n % 2 == 0) ? -fn : fn; } // Recursive implementation public static int recursive(int n) { if (n == 0) return 1; if (n == 1) return 2; if (n > 1) return recursive(n - 1) - recursive(n - 2); return -recursive(-n); } // Memoization implementation public static int memoization(int n) { int[] memo = new int[Math.abs(n) + 1]; memo[0] = 1; memo[1] = 2; return memoizationHelper(n, memo); } public static int memoizationHelper(int n, int[] memo) { if (memo[Math.abs(n)] != 0) return (n < 0 && n % 2 == 0) ? -memo[Math.abs(n)] : memo[Math.abs(n)]; memo[Math.abs(n)] = memoizationHelper(n - 1, memo) - memoizationHelper(n - 2, memo); return (n < 0 && n % 2 == 0) ? -memo[Math.abs(n)] : memo[Math.abs(n)]; } // Main method to test the implementations public static void main(String[] args) { int n = -10; System.out.println("Iterative: " + iterative(n)); System.out.println("Recursive: " + recursive(n)); System.out.println("Memoization: " + memoization(n)); } } //Hi i tried to run the above code but i kept receiving errors below. Kind assist me and correct it
Answer the given question with a proper explanation and step-by-step solution.
public class mathFormula { // Iterative implementation public static int iterative(int n) { if (n == 0) return 1; if (n == 1) return 2; int f1 = 1, f2 = 2, fn = 0; for (int i = 2; i <= Math.abs(n); i++) { fn = f1 - f2; f1 = f2; f2 = fn; } return (n < 0 && n % 2 == 0) ? -fn : fn; } // Recursive implementation public static int recursive(int n) { if (n == 0) return 1; if (n == 1) return 2; if (n > 1) return recursive(n - 1) - recursive(n - 2); return -recursive(-n); } // Memoization implementation public static int memoization(int n) { int[] memo = new int[Math.abs(n) + 1]; memo[0] = 1; memo[1] = 2; return memoizationHelper(n, memo); } public static int memoizationHelper(int n, int[] memo) { if (memo[Math.abs(n)] != 0) return (n < 0 && n % 2 == 0) ? -memo[Math.abs(n)] : memo[Math.abs(n)]; memo[Math.abs(n)] = memoizationHelper(n - 1, memo) - memoizationHelper(n - 2, memo); return (n < 0 && n % 2 == 0) ? -memo[Math.abs(n)] : memo[Math.abs(n)]; } // Main method to test the implementations public static void main(String[] args) { int n = -10; System.out.println("Iterative: " + iterative(n)); System.out.println("Recursive: " + recursive(n)); System.out.println("Memoization: " + memoization(n)); } } //Hi i tried to run the above code but i kept receiving errors below. Kind assist me and correct it. Thank you
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 11 out of bounds for length 11
at mathFormula.memoizationHelper(mathFormula.java:32)
at mathFormula.memoizationHelper(mathFormula.java:33)
at mathFormula.memoization(mathFormula.java:28)
at mathFormula.main(mathFormula.java:42)
Process finished with exit code 1
Step by step
Solved in 4 steps with 4 images