All Indices Problem

 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 an array containing indices of all items in the array which have value equal to M. Print all the values in the returned array.

Input Format

Enter a number N(size of the array) and add N more numbers to the array Enter number M to be searched in the array

Constraints

1 <= Size of array <= 10^5

Output Format

Display all the indices at which number M is found in a space separated manner

Sample Input
5
3
2
1
2
3
2
Sample Output
1 3
Explanation


2 is found at indices 1 and 3.



import java.util.Scanner;
public class AllIndicesProblem {
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 num=s.nextInt();
indicesFinder(arr,0,num);
s.close();

}
private static void indicesFinder(int arr[],int i,int num)
{
if(i>=arr.length)
{
return;
}
if(arr[i]==num)
{
System.out.print(i+" ");
}
indicesFinder(arr, i+1, num);

}

}

Post a Comment

0 Comments