Sort by

recency

|

816 Discussions

|

  • + 0 comments

    public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in);

        // Read number of lines
        int n = sc.nextInt();
        ArrayList<ArrayList<Integer>> list = new ArrayList<>();
    
        // Read each line
        for (int i = 0; i < n; i++) {
            int d = sc.nextInt(); // number of integers in this line
            ArrayList<Integer> line = new ArrayList<>();
            for (int j = 0; j < d; j++) {
                line.add(sc.nextInt());
            }
            list.add(line);
        }
    
        // Read number of queries
        int q = sc.nextInt();
    
        // Process each query
        for (int i = 0; i < q; i++) {
            int x = sc.nextInt(); // line number
            int y = sc.nextInt(); // position in line
    
            // Check if valid indices
            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!");
            }
        }
    
        sc.close();
    }
    

    }

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            
            int n = scanner.nextInt();
            
            if (n < 1 || n > 20000) {
                scanner.close();
                throw new IllegalArgumentException("The number of lists must be a positive integer below 20000");
            }
            
            ArrayList<ArrayList<Integer>> array = new ArrayList<>();
            
            for (int i=0; i<n; i++) {
                int d = scanner.nextInt();
                
                if (d < 0 || d > 50000) {
                    scanner.close();
                    throw new IllegalArgumentException("Each list must contain between 1 and 50000 numbers");
                }
                
                ArrayList<Integer> inner = new ArrayList<>();
                
                for (int j=0; j<d; j++) {
                    int nextInt = scanner.nextInt();
                    inner.add(nextInt);
                }
                
                array.add(inner);
            }
            
            int q = scanner.nextInt();
            
            if (q < 1 || q > 1000) {
                scanner.close();
                throw new IllegalArgumentException("The number of queries must be between 1 and 1000");
            }
            
            for (int i=0; i<q; i++) {
                int x = scanner.nextInt();
                int y = scanner.nextInt();
                
                if (x < 1 || x > n) {
                    scanner.close();
                    throw new IllegalArgumentException(String.format("The line used for searching cannot be greater than %d", n));
                }
                
                try {
                    System.out.println(array.get(x-1).get(y-1));
                } catch (IndexOutOfBoundsException exc) {
                    System.out.println("ERROR!");
                }
            }
            
            scanner.close();
        }
    }
    
  • + 0 comments

    Most of you used your high level knowledge to make it work, but here it is for begineers like me who would like to solve it with an easy method:

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

    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. */
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int arrObj;
    
        // data list
        ArrayList<ArrayList<Integer>> dataList = new ArrayList<>();
        // making array list to store data
        for (int i = 0; i < n; i++){
    
            int nArray = sc.nextInt();
            ArrayList<Integer> arr = new ArrayList<>();
            for(int j = 0; j < nArray; j++){
    
                arrObj = sc.nextInt();
    
                arr.add(arrObj);
    
            }
            // System.out.println(arr);
            dataList.add(arr);
        }
    
    
        int n2 = sc.nextInt();
        //query list
        // ArrayList<ArrayList<Integer>> queryList = new ArrayList<>();
        // making data list to fetch data or else print Erorr
        for (int i = 0; i < n2; i++){
    
            int row = sc.nextInt();
            int coloumn = sc.nextInt();
            // System.out.println(arr2);
            try{
                System.out.println(dataList.get(row-1).get(coloumn-1));
            }
            catch(Exception e){
                System.out.println("ERROR!");
            }
    
    
        }
    
    
    }
    

    }

    `

  • + 0 comments

    Solution:

    public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        ArrayList<ArrayList<Integer>> list = new ArrayList<>();
        for(int i=1;i<=n;i++){
            ArrayList<Integer> newList = new ArrayList<>();
            int d = sc.nextInt();
            for(int j=1;j<=d;j++){
                int l = sc.nextInt();
               newList.add(l);
            }
            list.add(newList);
        }
        int q = sc.nextInt();
        for(int i=0;i<q;i++){
            int x = sc.nextInt()-1;
            int y = sc.nextInt()-1;
        if(list.get(x).size()>y){
            System.out.println(list.get(x).get(y));
        } else {
            System.out.println("ERROR!");
        }
    
    
        }
    }
    

    }

  • + 0 comments

    Here is java Arraylist solution - https://programmingoneonone.com/hackerrank-java-arraylist-problem-solution.html