14,640,725 members
Rate this:
See more:
Problem Statement:

Quote:
You are given n 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 Yth position of Xth line.

Input Format

The first line has an integer n. In each of the next n lines there will be an integer d denoting number of integers on that line and then there will be d space-separated integers. In the next line there will be an integer q denoting number of queries. Each query will consist of two integers x and y.

Output Format

In each line, output the number located in Y'th position of X'th line. If there is no such position, just print "ERROR!"

Quote:
Sample Input

5
5 41 77 74 22 44
1 12
4 37 34 36 52
0
3 20 22 33
5
1 3
3 4
3 1
4 3
5 5

Sample Output

74
52
37
ERROR!
ERROR!

I have written my code till adding elements to the ARRAYLIST. I have tried to print elements in the array list.
My code:
```import java.util.*;
public class Solution {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
ArrayList<Integer> al = new ArrayList<Integer>();
for(int i=0;i<n;i++){
int a = scan.nextInt();
for(int j=0;j<a;j++){
int b = scan.nextInt();
}
}
scan.close();
Iterator<Integer> itr = al.iterator();
while(itr.hasNext()){
System.out.print(itr.next()+" ");
}
}
}```

What I have tried:

My code's Result:
Input:
5
5 41 77 74 22 44
1 12
4 37 34 36 52
0
3 20 22 33
Output:
20 22 33 37 34 36 52 12 41 77 74 22 44 5 1 4 0 3

Now that my output is a sequence of integers which I added to the list, I need some insights on how to print the number located in Y'th position of X'th line.
Posted
Updated 5-Aug-20 0:07am
v3

Rate this:

## Solution 1

You have to keep `x` and `y` position of each input integer. Hence an `ArrayList<integer></integer>` is not enough (what about `ArrayList<arraylist<integer>></arraylist<integer>` ?)
Please note, the input data of your example is wrong (it doesn't contain query lines).