Magic index in an array a[1..n] is defined to be an index such that a[ i] = i. Given an array of integers, write a recursive method to find the first magic index from left to right. If one exists in the given array, return the index number i, otherwise return -1. Here are some test cases. The first number is the size of the array. Input1 6 -2-2 3-2-2 -1 Output1 -1
*************Question below: ------------------------
Code in Java. Code must be recursive.
*DriverMain.java:
import java.util.*;
import java.lang.*;
import java.io.*;
//
public class DriverMain {
public static void main (String[] args) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
int A[] = new int[N];
for (int i = 0; i < N; i++) {
A[i] = s.nextInt();
}
ProblemSolution problemSolution = new ProblemSolution();
System.out.print(problemSolution.findMagicIndex(A, N));
}
}
*ProblemSolution.java:
import java.util.*;
import java.lang.*;
import java.io.*;
class ProblemSolution {
public static int findMagicIndex(int[] a, int n) {
//CODE GOES HERE
}
}
*entrypoint.cz:
DriverMain.java
![Magic index in an array a[1.n] is defined to be an index such
that a[ i] = i. Given an array of integers, write
a recursive method to find the first magic index from left to
right. If one exists in the given array, return the index
number i, otherwise return -1.
Here are some test cases. The first number is the size of the
array.
Inputl
6
-2 -2 3 -2 -2 -1
Outputl
-1
Input2
-17234
Output2
2](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fb7b9d31a-c86f-4d0e-8309-e29e98c37251%2F2805c610-fca9-4f6f-af86-994c8dbfef95%2Fe609ru_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 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)