-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathlastIndex.java
More file actions
56 lines (48 loc) · 1.21 KB
/
lastIndex.java
File metadata and controls
56 lines (48 loc) · 1.21 KB
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.util.Scanner;
public class Runner {
static Scanner s = new Scanner(System.in);
public static int[] takeInput(){
int size = s.nextInt();
int[] input = new int[size];
for(int i = 0; i < size; i++){
input[i] = s.nextInt();
}
return input;
}
public static void main(String[] args) {
int[] input = takeInput();
int x = s.nextInt();
System.out.println(Solution.lastIndex(input, x));
}
}
public class Solution {
public static int lastIndex(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 index=1;index<input.length;index++){
smallArray[index-1]=input[index];
}
int val=lastIndex(smallArray,x);
if(val==-1){
if(input[0]==x){
return 0;
}
}
if(val>=0){
return ++val;
}
return -1;
}
}