Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Java Arraylist problem solution

YASH PAL, 31 July 2024

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!");
            }
        }
    }
}
coding problems hackerrank solutions java

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes