Skip to content

Commit 986ca8a

Browse files
committed
Test knowledge of ArrayList
1 parent 607997e commit 986ca8a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Java/Data-Structure/ArrList.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Sometimes it's better to use dynamic size arrays. Java's Arraylist can provide you this feature. Try to solve this problem using Arraylist.
3+
You are given nn lines. In each line there are zero or more integers. You need to answer a few queries where you need to tell the number located in ythyth position of xthxth line.
4+
Take your input from System.in.
5+
6+
Input Format
7+
The first line has an integer nn. In each of the next nn lines there will be an integer dd denoting number of integers on that line and then there will be dd space-separated integers. In the next line there will be an integer qq denoting number of queries. Each query will consist of two integers xx and yy.
8+
9+
Output Format
10+
In each line, output the number located in ythyth position of xthxth line. If there is no such position, just print "ERROR!"
11+
*/
12+
13+
import java.util.*;
14+
15+
public class ArrList {
16+
17+
public static void main(String[] args) {
18+
Scanner s = new Scanner(System.in);
19+
int t = s.nextInt();
20+
ArrayList<Integer> a = new ArrayList<Integer>();
21+
ArrayList<Integer> b = new ArrayList<Integer>();
22+
for (int i = 0; i < t; i++) {
23+
int x = s.nextInt();
24+
a.add(x);
25+
for (int j = 0; j < x; j++) {
26+
b.add(s.nextInt());
27+
}
28+
}
29+
int d = s.nextInt();
30+
ArrayList<Integer> e = new ArrayList<Integer>();
31+
for (int i = 0; i < d; i++) {
32+
e.add(s.nextInt());
33+
e.add(s.nextInt());
34+
}
35+
s.close();
36+
for (int y = 0; y < d+d; y=y+2) {
37+
int pos = e.get(y+1);
38+
int line = e.get(y);
39+
int len = a.get(line-1);
40+
int sum2 = 0;
41+
if (line - 1 > 0) {
42+
for (int u = 0; u < line-1; u++) {
43+
sum2 += a.get(u);
44+
}
45+
}
46+
if (len < pos) {
47+
System.out.println("ERROR!");
48+
}
49+
else {
50+
System.out.println (b.get(sum2 + (pos-1)));
51+
}
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)