-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallindicesrecursion.java
More file actions
44 lines (33 loc) · 1007 Bytes
/
Copy pathallindicesrecursion.java
File metadata and controls
44 lines (33 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.*;
public class allindicesrecursion {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[]= new int[n];
for(int i =0 ; i< n ; i++){
arr[i]= sc.nextInt();
}
int x= sc.nextInt();
int[]iarr= allindices(arr, x, 0,0);
if(iarr.length==0){
System.out.println();
return;
}
for(int i= 0 ; i<arr.length;i++){
System.out.println(iarr[i]);
}
}
public static int[] allindices(int arr[], int x , int idx , int fs){
if(idx== arr.length){
return new int[fs];
}
if(arr[idx]==x){
int[]iarr= allindices(arr, x,idx+1, fs+1);
iarr[fs]=idx;
return iarr;
}else{
int[]iarr= allindices(arr, x, idx+1, fs);
return iarr;
}
}
}