First Index

 Take as input N, the size of array. Take N more inputs and store that in an array. Take as input M, a number. Write a recursive function which returns the first index at which M is found in the array and -1 if M is not found anywhere. Print the value returned.

Input Format

Enter a number N and add N more elements to an array, then enter a number M

Constraints

None

Output Format

Display the first index at which number M is found

Sample Input
5
3
2
1
2
2
2
Sample Output
1
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
int length=s.nextInt();
int arr[]=new int[length];
for(int i=0; i<arr.length; i++)
{
arr[i]=s.nextInt();
}
int m=s.nextInt();
System.out.println(firstIndex(arr,0,m));
//System.out.println(Arrays.toString(arr));
s.close();
}
private static int firstIndex(int arr[],int i,int number)
{
if(i>=arr.length)
{
return -1;
}
if(arr[i]==number)
{
return i;
}
return firstIndex(arr, i+1,number);
}
}

Post a Comment

0 Comments