HackerRank Solution: Java Arraylist

Sakshi Singh
1 min readApr 14, 2019

import java.io.*;

import java.util.*;

import java.util.Collection.*;

public class Solution {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

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

for(int i=0;i<n;i++)

{

int d = sc.nextInt();

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

for(int j=0;j<d;j++)

{

l.add(sc.nextInt());

}

all.add(l);

}

int q = sc.nextInt();

for(int i=0;i<q;i++)

{

int q1 = sc.nextInt();

int q2 = sc.nextInt();

if(q2 > all.get(q1–1).size())

System.out.println(“ERROR!”);

else

System.out.println(all.get(q1–1).get(q2 -1));

}

}

}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Sakshi Singh
Sakshi Singh

No responses yet

Write a response