-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllOccurrenceRecursion.java
56 lines (45 loc) · 1.42 KB
/
AllOccurrenceRecursion.java
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
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
Name: All Indices of Array
Source: PepCoding
Link: https://www.pepcoding.com/resources/online-java-foundation/recursion-in-arrays/all-indices-official/ojquestion
Statement: Return an array of appropriate size which contains all indices at which x occurs in array a.
*/
public class AllOccurrenceRecursion {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
int x = Integer.parseInt(br.readLine());
int[] iarr = allIndices(arr, x, 0, 0);
if(iarr.length == 0){
System.out.println();
return;
}
for(int i = 0; i < iarr.length; i++){
System.out.println(iarr[i]);
}
}
public static int[] allIndices(int[] arr, int x, int idx, int fsf) {
// write ur code here
if(idx == arr.length)
{
int [] abc = new int [fsf];
return abc;
}
if(arr[idx]==x)
{
fsf+=1;
}
int [] num = allIndices(arr, x, idx+1, fsf);
if(arr[idx]==x)
{
num[--fsf] = idx;
}
return num;
}
}