forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
56 lines (54 loc) · 1.44 KB
/
Solution.java
File metadata and controls
56 lines (54 loc) · 1.44 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;
import java.util.ArrayList;
public class Solution
{
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
ArrayList<Integer> al=new ArrayList();
for(int i=0;i<n;i++)
{
int temp = sc.nextInt();
if(i==0)
al.add(temp);
else if(i!=0&&al.get(i-1)==temp)
continue;
else al.add(temp);
}
int t=sc.nextInt();
for(int k=1;k<=t;k++)
{
int temp=sc.nextInt();
int i = al.indexOf(temp);
if(i!=-1)
System.out.println(i+1);
else
{
for(i=0;i<al.size()-1;i++)
{
if(al.get(i)<temp)
{
al.set(i,temp);
System.out.println(al.indexOf(temp)+1);
break;
}
else if(al.get(al.size()-1)>temp)
{
al.add(temp);
System.out.println(al.indexOf(temp)+1);
break;
}
else if(al.get(i)>temp&&al.get(i+1)<temp)
{
al.set(i+1,temp);
System.out.println(al.indexOf(temp)+1);
break;
}
else continue;
}
}
}
}
}