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.
We need a 2-D array to solve this problem. It's possible to use an int[][] (as someone showed in comments below), but I prefer an ArrayList<ArrayList<Integer>>
importjava.util.Scanner;importjava.util.ArrayList;publicclassSolution{publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);intn=scan.nextInt();/* Save numbers in 2-D ArrayList */ArrayList<ArrayList<Integer>>lists=newArrayList<>();for(introw=0;row<n;row++){intd=scan.nextInt();ArrayList<Integer>list=newArrayList<>();for(intcol=0;col<d;col++){list.add(scan.nextInt());}lists.add(list);}/* Answer the queries */intq=scan.nextInt();for(inti=0;i<q;i++){intx=scan.nextInt();inty=scan.nextInt();ArrayList<Integer>list=lists.get(x-1);if(y<=list.size()){System.out.println(list.get(y-1));}else{System.out.println("ERROR!");}}scan.close();}}
Let me know if you have any questions.
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 →
Java solution - passes 100% of test cases
From my HackerRank solutions.
We need a 2-D array to solve this problem. It's possible to use an int[][] (as someone showed in comments below), but I prefer an ArrayList<ArrayList<Integer>>
Let me know if you have any questions.