Please write where its written write your code here!! please use java for the code! please use the given code and I will rate thanks
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
-------------------------------------------
Input2
5
-1 7 2 3 4
Output2
2
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Code
import java.util.*;
import java.lang.*;
import java.io.*;
//Your program will be evaluated by this DriverMain class and
several test cases.
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));
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
class ProblemSolution{
public static int findMagicIndex(int[] a, int
n){
//write your code
here
}
}
class ProblemSolution{
public static int findMagicIndex(int[] a, int n){
if (n==0)
return -1;
if (a[a.length-n]==a.length-n)
return a.length-n;
return findMagicIndex( a, n-1);
}
}


Please write where its written write your code here!! please use java for the code! please...
Can anyone help me with this code? I've never worked with
insertion sort and I'm having trouble. I posted the instructions
and the included code. Thank you!
Insertion Sort Given an array of integers, sort the array in ascending/descending order by using Insertion sort. Reference: http://www.algolist.net/Algorithms/Sorting/Insertion_sort Input 4368 92157 Where, • First line represents the type of ordering. • Second line represents the size of the array. • Third line represents the array elements. Output 123456789 + Test Case(s) DriverMain.java...
Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard. Chapter 5 Exercise 37: Java Programming * * (Decimal to...
This java code won't run and I can't figure out the problem with it. Please help import java.io.*; import java.util.*; import java.math.*; import java.util.Scanner; public class Fibonacci { // Returns n-th Fibonacci number static BigInteger fib(int n) { BigInteger[] Fibo = new BigInteger[n+2]; Fibo[0] = BigInteger.ZERO; Fibo[1] = BigInteger.ONE; for (int j=2 ; j<=n ; j++) { Fibo[j] = Fibo[j-1].add(Fibo[j-2]); } return (Fibo[n]); }...
Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform a linear search of the array. The method should receive the search key and starting index as arguments. If the search key is found, return its index in the array; otherwise, return –1. Each call to the recursive method should check one index in the array. Figure 19.2 import java.security.SecureRandom; import java.util.Arrays; import java.util.Scanner; public class LinearSearchTest{ public static int linearSearch(int data[], int searchKey)...
Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change some syntax to make them compile and run import java.io.*; import java.lang.*; import java.util.*; class MyThreadExampleMutex{ public static void main(String[] args) { new HelloThread (1). start (); new HelloThread (2). start (); new HelloThread (3). start (); System.out.print("Global.buffer Content = "); for (int i = 0; i < 9; i++) { System.out.print(Global.buffer[i] + "; "); }} } class Global { public static int[] buffer...
JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in); System.out.println("Seed:"); int seed = scanner.nextInt(); System.out.println("Length"); int length = scanner.nextInt(); Random random...
JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; } public static void main(String[] args) { int n, i, k; System.out.println("Enter...
I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
I need a java flowchart for the following code: import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the input size: "); int n=sc.nextInt(); int arr[]=new int[n]; System.out.print("Enter the sequence: "); for(int i=0;i<n;i++) arr[i]=sc.nextInt(); if(isConsecutiveFour(arr)) { System.out.print("yes the array contain consecutive number:"); for(int i=0;i<n;i++) System.out.print(arr[i]+" "); ...