-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlab2.java
More file actions
99 lines (85 loc) · 2.63 KB
/
lab2.java
File metadata and controls
99 lines (85 loc) · 2.63 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
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class lab2_2 {
public static int[] insert(int x, int[] num, int initial)
{
int index=0;
int temp1;
int temp2=0;
for (int i =0; i<num.length;i++)
{
if (x<num[i])
{
index=i;
break;
}
else if(i == num.length-1) {
num[initial]=x;
System.out.println(""+ initial+1 + " " + 0);
return num;
}
}
temp1=num[index];
for (int j =index;j<=initial; j++){
temp2 = num[j+1];
if(j==index){
num[j]=x;
}
num[j+1]= temp1;
temp1=temp2;
}
System.out.println(""+(initial+1)+ " "+ (initial - index) );
return num;
}
public static int[] delete(int x, int[] num,int initial){
int index=0;
for(int i=0;i<initial;i++){
if (num[i]==x){
index=i;
break;}
}
for(int j=index;j<initial;j++){
num[j]=num[j+1];}
System.out.println("" + (initial-1 )+ " " + (initial-index-1));
return num;
}
public static void solver() throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input = reader.readLine();
String[] parts = input.split(" ");
int max = Integer.parseInt(parts[0]);
int initial = Integer.parseInt(parts[1]);
int cases = Integer.parseInt(parts[2]);
String arr = reader.readLine();
String[] ar = arr.split(" ");
int[] nums = new int[max];
for (int i =0 ; i<initial;i++)
{
nums[i]= Integer.parseInt(ar[i]);
}
for(int j=0;j<cases;j++){
String test = reader.readLine();
String[] tests=test.split(" ");
if (Integer.parseInt(tests[0])==1){
if(initial!=max){
nums = insert(Integer.parseInt(tests[1]),nums,initial);
initial=initial+1;}
else System.out.println("0 "+nums.length);}
else if (Integer.parseInt(tests[0])==2){
nums = delete(Integer.parseInt(tests[1]),nums,initial);
initial = initial -1;
}
else {
for (int k=0;k<initial;k++)
{
System.out.print(nums[k]);
System.out.print(" ");
}
System.out.println();}
}
}
public static void main(String[] args) throws IOException{
solver();}
}