Java Arraylist - java - Spicy Coders

Recent

Saturday, July 08, 2017

Java Arraylist - java

The n lines are given. 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 lines there will be an integer d denoting number of integers on that line and then there will be space-separated integers. In the next line there will be an integer denoting number of queries. Each query will consist of two integers and y.

Constraint
  • ·         1<=n<=20000
  • ·         0<=d<=50000
  • ·         1<=q<=1000
  • ·         1<=x<=n


Each number will fit in signed integer.
Total number of integers in lines will not cross 100000.

Output Format
In each line, output the number located in yth  position of xth line. If there is no such position, just print "ERROR!"

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!

Explanation
The diagram below explains the queries:

Coding

import java.io.*;
import java.util.*;
public class Solution
 {
    public static void main(String[] args) 
  {
     Scanner in = new Scanner(System.in);
     int n = in.nextInt();
     int d,q,x,y;
     ArrayList[] a = new ArrayList[n];
     for(int i=0;i<n;i++)
    {
        d = in.nextInt();
        a[i] = new ArrayList();
        for(int j=0;j<d;j++)
        { 
            a[i].add(in.nextInt());               
        }
    }
    q=in.nextInt();
    for(int i=0;i<q;i++)
   {
        x=in.nextInt();
        y=in.nextInt();
        try{
            System.out.println(a[x-1].get(y-1));
        } catch(Exception e){
            System.out.println("ERROR!");
        }
    }   
   } 
}



No comments:

Post a Comment