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
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 4 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)