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)

Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images









