a4ary4n

Untitled

Oct 13th, 2020 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class QueryPrinting {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.        
  8.         System.out.print("Enter n: ");
  9.         int n = sc.nextInt();
  10.         ArrayList<Integer>[] inpData = new ArrayList[n];
  11.        
  12.         int[] numEle = new int[n];
  13.         for (int i = 0; i < n; i++) {
  14.             inpData[i] = new ArrayList<Integer>();
  15.             numEle[i] = sc.nextInt();
  16.             for (int j = 0; j < numEle[i]; j++) {
  17.                 inpData[i].add(sc.nextInt());
  18.             }
  19.         }
  20.        
  21.         int nQuery = sc.nextInt();
  22.         int[][] query = new int[nQuery][2];
  23.         for (int i = 0; i < nQuery; i++) {
  24.             for (int j = 0; j < 2; j++) {
  25.                 query[i][j] = sc.nextInt() - 1;
  26.             }
  27.         }
  28.        
  29.         for (int i = 0; i < nQuery; i++) {
  30.             if (query[i][0] < n && query[i][1] < numEle[query[i][0]]) {
  31.                 System.out.println(inpData[query[i][0]].get(query[i][1]));
  32.             } else {
  33.                 System.out.println("-1");
  34.             }
  35.         }
  36.        
  37.         sc.close();
  38.     }
  39.  
  40. }
  41.  
Add Comment
Please, Sign In to add comment