We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int d = sc.nextInt();
ArrayList row = new ArrayList<>();
for (int j = 0; j < d; j++) {
row.add(sc.nextInt());
}
list.add(row);
}
int q = sc.nextInt();
for (int i = 0; i < q; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
if (x <= list.size() && y <= list.get(x - 1).size()) {
System.out.println(list.get(x - 1).get(y - 1));
} else {
System.out.println("ERROR!");
}
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Arraylist
You are viewing a single comment's thread. Return to all comments →
import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int d = sc.nextInt(); ArrayList row = new ArrayList<>(); for (int j = 0; j < d; j++) { row.add(sc.nextInt()); } list.add(row); } int q = sc.nextInt(); for (int i = 0; i < q; i++) { int x = sc.nextInt(); int y = sc.nextInt(); if (x <= list.size() && y <= list.get(x - 1).size()) { System.out.println(list.get(x - 1).get(y - 1)); } else { System.out.println("ERROR!"); } } } }