-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirst_Index_Array.java
More file actions
30 lines (26 loc) · 736 Bytes
/
First_Index_Array.java
File metadata and controls
30 lines (26 loc) · 736 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
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// write your code here
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scn.nextInt();
}
int x = scn.nextInt();
System.out.println(firstIndex(arr, 0, x));
}
public static int firstIndex(int[] arr, int idx, int x) {
if (idx == arr.length) {
return -1;
}
if (arr[idx] == x) {
return idx;
} else {
int asa = firstIndex(arr, idx + 1, x);
return asa;
}
}
}