forked from sunnyshahabuddin/Coding-Ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirstIndex.java
More file actions
29 lines (28 loc) · 759 Bytes
/
firstIndex.java
File metadata and controls
29 lines (28 loc) · 759 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
class Solution {
public static int firstIndex(int input[], int x) {
/* Your class should be named Solution
* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
if(input.length<=1){
if(input[0]==x){
return 0;
}else{
return -1;
}
}
int smallArray[]=new int[input.length-1];
for(int i=0;i<input.length-1;i++){
smallArray[i]=input[i];
}
int val=firstIndex(smallArray,x);
if(val==-1){
if(input[input.length-1]==x){
return input.length-1;
}
}
return val;
}
}