Click here to Skip to main content
14,640,725 members
Rate this:
Please Sign up or sign in to vote.
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();
	        	al.add(a);
	        	for(int j=0;j<a;j++){
	        	int b = scan.nextInt();
	        		al.add(j,b);
	        	}
	        }
	        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

1 solution

Rate this:
Please Sign up or sign in to vote.

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).
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100