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
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images