Sort by

recency

|

809 Discussions

|

  • + 0 comments

    public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); ArrayList> list = new ArrayList<>(); int n = sc.nextInt();

        for (int i = 0; i < n; i++) {
            int m = sc.nextInt();
            ArrayList<Integer> a = new ArrayList<Integer>();
            for (int j = 0; j < m; j++) {
                a.add(sc.nextInt());
            }
            list.add(a);
        }
    
        int x = sc.nextInt();
    
        for (int i = 0; i < x; i++) {
            int num1 = sc.nextInt()-1;
            int num2 = sc.nextInt()-1;
    
            if(num1<list.size() && num1>=0) {
                if(num2<list.get(num1).size() && num2>=0) {
                    System.out.println(list.get(num1).get(num2));
                }else{
                System.out.println("ERROR!");   
            }
            }
        }
    
    }
    
  • + 0 comments

    Isnt it kinda weird that the author seem to mean index 0 in their queries but uses "1" instead, e.g., for query 1 3 we actually mean the first list (1). It means that we need to shift the index so that when we assign int x to the value, it should be x = value -1 (and int y = value -1).

    Its not wrong, it just felt like adding an unneccesary layer of complexity. Perhaps its part of the exercise, i could also misunderstand the reasoning behind it.

    The solution, as i see it is to shift the lists in both images and in the tests, so images show lists 0, 1, 2, 3 ,4 ... instead of 1,2,3,4,,, and so the test doesnt expect the list in place 1 but rather in place 0

  • + 0 comments
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);        
        int n = sc.nextInt();  
        ArrayList<Integer>[] arr = new ArrayList[n];
    
        for (int i = 0; i < n; i++) {
            int m = sc.nextInt();  
            arr[i] = new ArrayList<>();
    
            for (int j = 0; j < m; j++) {
                int value = sc.nextInt();
                arr[i].add(value);
            }
        }
    
        int mn = sc.nextInt();  
    
        for (int i = 0; i < mn; i++) {
            int val1 = sc.nextInt()-1;  
            int val2 = sc.nextInt()-1;  
    
            if (val1 < 0 || val1 >= n || val2 < 0 || val2 >= arr[val1].size()) {
                System.out.println("ERROR!");
            } else {
                System.out.println(arr[val1].get(val2));
            }
        }
    
    }
    
  • + 0 comments

    import java.io.; import java.util.; import java.util.Scanner; public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        // you need to answer a few queries
        Scanner sc = new Scanner(System.in);
        ArrayList[] al = new ArrayList[20000]; // including 2000 elements
        int lists = sc.nextInt();
    
        // read numbers;
        for (int i = 0; i < lists; i++) {
          al[i] = new ArrayList(); // 
          int number = sc.nextInt();
          for(int j = 0; j < number; j++) {
            int value = sc.nextInt();
            al[i].add(value);
          }
        }
        // testing the queries;
        int queries = sc.nextInt();
        for (int i = 0; i < queries; i++) {
          int x = sc.nextInt();
          int y = sc.nextInt();
          try {
            System.out.println(al[x-1].get(y-1));
          } catch (Exception e) {
            System.out.println("ERROR!");
          }
        }
    }
    

    }

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int cantListas = sc.nextInt();
        ArrayList<ArrayList<Integer>> listas = new ArrayList<>();
    
        for (int i = 0; i < cantListas; i++) {
            int posiciones = sc.nextInt();
    
            ArrayList<Integer> numeros = new ArrayList<>();
    
            for (int j = 0; j < posiciones; j++) {
                numeros.add(sc.nextInt());
            }
    
            listas.add(numeros);
        }
    
        int cantQueries = sc.nextInt();
        for (int i = 0; i < cantQueries; i++) {
            int lista = sc.nextInt()-1;
            int posicion = sc.nextInt()-1;
    
            if (listas.get(lista).size() > posicion ) {
                System.out.println(listas.get(lista).get(posicion));
            } else {
                System.out.println("ERROR!");
            }
        }        
    
    
        sc.close();
    }
    

    }