hacker rank 29th problem solution(java arraylist)

29 . JAVA ARRAYLIST

import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        ArrayList<ArrayList<Integer>> rows = new ArrayList<>();
       
        for (int i = 0; i < n; i++) {
            int d = in.nextInt();
            ArrayList<Integer> row = new ArrayList<>();
           
            for (int j = 0; j < d; j++) {
                row.add(in.nextInt());
            }
           
            rows.add(row);
        }
       
        int q = in.nextInt();
       
        for (int i = 0; i < q; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
           
            try {
                System.out.println(rows.get(x - 1).get(y - 1));
            } catch (IndexOutOfBoundsException e) {
                System.out.println("ERROR!");
            }
        }
    }
}


Compiler Message
Success
Input (stdin)Download
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
{-truncated-}
Download to view the full testcase
Expected OutputDownload
74
52
37
ERROR!
ERROR!
{-truncated-}
Download to view the full testcase

Comments

Popular posts from this blog

Amazon Web Services

Hacker Rank all java and python problem solutions

Google Code-In mentorship experience :)