-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJava List.java
130 lines (95 loc) · 2.38 KB
/
Java List.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
For this problem, we have
types of queries you can perform on a List:
Insert
at index
:
Insert
x y
Delete the element at index
:
Delete
x
Given a list,
, of integers, perform
queries on the list. Once all queries are completed, print the modified list as a single line of space-separated integers.
Input Format
The first line contains an integer,
(the initial number of elements in ).
The second line contains space-separated integers describing .
The third line contains an integer, (the number of queries).
The
subsequent lines describe the queries, and each query is described over two lines:
If the first line of a query contains the String Insert, then the second line contains two space separated integers
, and the value must be inserted into at index
.
If the first line of a query contains the String Delete, then the second line contains index
, whose element must be deleted from
.
Constraints
Each element in is a 32-bit integer.
Output Format
Print the updated list
as a single line of space-separated integers.
Sample Input
5
12 0 1 78 12
2
Insert
5 23
Delete
0
Sample Output
0 1 78 12 23
Explanation
Insert 23 at index .
Delete the element at index .
Having performed all queries, we print as a single line of space-separated integers.
Code:
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
List<Integer> l = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++)
{
int ele=sc.nextInt();
l.add(ele);
}
int q=sc.nextInt();
for(int i=0;i<q;i++)
{
int p,e;
String s = sc.next();
if(s.equals("Insert"))
{
p=sc.nextInt();
e=sc.nextInt();
l.add(p,e);
}
else if(s.equals("Delete"))
{
p=sc.nextInt();
l.remove(p);
}
}
for(int i:l)
{
System.out.print(i+" ");
}
}
}
Input (stdin)
5
12 0 1 78 12
2
Insert
5 23
Delete
0
Your Output (stdout)
0 1 78 12 23
Expected Output
0 1 78 12 23