HackerRank Java- Arraylist




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 yth position of xth line.

Take your input from System.in.



  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6. public class Solution {
  7. public static void main(String[] args) {
  8. Scanner in = new Scanner(System.in);
  9. ArrayList<ArrayList<Integer>> rows = new ArrayList<>();
  10. int n = in.nextInt();
  11. for (int i = 0; i < n; i++) {
  12. int m = in.nextInt();
  13. ArrayList<Integer> row = new ArrayList<>();
  14. for (int j = 0; j < m; j++) {
  15. row.add(in.nextInt());
  16. }
  17. rows.add(row);
  18. }
  19. n = in.nextInt();
  20. for (int i = 0; i < n; i++) {
  21. int x = in.nextInt();
  22. int y = in.nextInt();
  23. try {
  24. System.out.println(rows.get(x - 1).get(y - 1));
  25. } catch (Exception e) {
  26. System.out.println("ERROR!");
  27. }
  28. }
  29. }
  30. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube