Sort by

recency

|

814 Discussions

|

  • + 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

  • + 0 comments

    Scanner sc=new Scanner(System.in); int n=sc.nextInt(); ArrayList> list=new ArrayList>();

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

    import java.util.*; public class temporary { public static void main(String[] args) { Scanner myobj = new Scanner(System.in); ArrayList> list = new ArrayList<>(); int n = myobj.nextInt(); myobj.nextLine(); for (int i = 0; i < n; i++) { String line = myobj.nextLine(); String[] parts_list = line.split(" "); ArrayList sublist = new ArrayList<>(); for (int j=1;j< parts_list.length;j++){ sublist.add(Integer.valueOf(parts_list[j])); } list.add(sublist); } int q = myobj.nextInt(); myobj.nextLine(); for(int i=0;i

            }
        }
    
    }
    

    }