In this HackerRank java Arraylist problem in java programming language 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 the Yth position of the Xth line.


HackerRank Java Arraylist problem solution


HackerRank Java Arraylist problem solution.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scan=new Scanner(System.in);

    int total=scan.nextInt();

    ArrayList<ArrayList<Integer>> mainlist=new ArrayList<>();

    for(int i=0;i<total;i++)
    {
     int size=scan.nextInt();
     ArrayList<Integer> list=new ArrayList();

     for(int j=0;j<size;j++)
       {
          int item=scan.nextInt();
          list.add(item);
       }
      mainlist.add(list);
   }

    int totalprint=scan.nextInt();
    for(int k=0;k<totalprint;k++)
    {
      int row=scan.nextInt();
      int col=scan.nextInt();
      try
       {
       System.out.println(mainlist.get(row-1).get(col-1));
       }
      catch(Exception e)
      {
      System.out.println("ERROR!");
      }
    }
    scan.close();
    }
}


Second solution

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;


public class Solution
{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 64 * 1024);

        final int N = Integer.parseInt(br.readLine().trim(), 10);

        final List<List<Integer>> arr = new ArrayList<List<Integer>>();

        for (int i = 0; i < N; i++) {
            final String[] data = br.readLine().trim().split(" ");
            final int D = Integer.parseInt(data[0], 10);
            final List<Integer> a = new ArrayList<Integer>();
            for (int j = 1; j <= D; j++) {
                final int v = Integer.parseInt(data[j], 10);
                a.add(v);
            }
            arr.add(a);
        }

        final StringBuilder sb = new StringBuilder();
        final int Q = Integer.parseInt(br.readLine().trim(), 10);
        for (int i = 0; i < Q; i++) {
            final String[] query = br.readLine().trim().split(" ");
            final int r = Integer.parseInt(query[0], 10);
            final int c = Integer.parseInt(query[1], 10);
            if (r > arr.size()) {
                sb.append("ERROR!\n");
            } else {
                final List<Integer> row = arr.get(r - 1);
                if (c > row.size()) {
                    sb.append("ERROR!\n");
                } else {
                    sb.append(row.get(c - 1)).append('\n');
                }
            }
        }

        System.out.print(sb.toString());

        br.close();
        br = null;
    }
}


The solution in java8 programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        ArrayList<ArrayList< Integer >> arr = new ArrayList<ArrayList< Integer >>();        
        
        Scanner sc = new Scanner(System.in);
        
        int N = sc.nextInt();
        for (int n=0 ; n<N ; ++n) {
            
            int D = sc.nextInt();
            ArrayList< Integer > a = new ArrayList< Integer >();
            
            for (int d=0 ; d<D ; ++d) {
                a.add(sc.nextInt());   
            }
            
            arr.add(a);            
        }
        
        int Q = sc.nextInt();
        for (int q=0 ; q<Q ; ++q) {
            
            int x = sc.nextInt();
            int y = sc.nextInt();
            
            try {
                System.out.println(arr.get(x-1).get(y-1));
            } catch (Exception e) {
                System.out.println("ERROR!");
            }
        }
    }
}