Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Java Arraylist HackerRank Solution

 Sometimes it's better to use dynamic size arrays. Java's Arraylist can provide you this feature. Try to solve this problem using 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.

Input Format

The first line has an integer n. In each of the next n lines there will be an integer d denoting number of integers on that line and then there will be d space-separated integers. In the next line there will be an integer q denoting number of queries. Each query will consist of two integers x and y.

Constraints

    1<=n<=20000
    0<=d<=50000
    1<=q<=1000
    1<=x<=n

Each number will fit in signed integer.
Total number of integers in n lines will not cross 105.

Output Format

In each line, output the number located in yth position of xth line. If there is no such position, just print "ERROR!"

Sample Input

5
5 41 77 74 22 44
1 12
4 37 34 36 52
0
3 20 22 33
5
1 3
3 4
3 1
4 3
5 5

Sample Output

74
52
37
ERROR!
ERROR!

Explanation

The diagram below explains the queries:

image

Solution:-

import java.util.*;



public class Solution {


    public static void main(String[] args) {


        Scanner in = new Scanner(System.in);


        int n = in.nextInt();


        ArrayList<ArrayList<Integer>> rows = new ArrayList<>();


        


        for (int i = 0; i < n; i++) {


            int d = in.nextInt();


            ArrayList<Integer> row = new ArrayList<>();


            


            for (int j = 0; j < d; j++) {


                row.add(in.nextInt());


            }


            


            rows.add(row);


        }


        


        int q = in.nextInt();


        


        for (int i = 0; i < q; i++) {


            int x = in.nextInt();


            int y = in.nextInt();


            


            try {


                System.out.println(rows.get(x - 1).get(y - 1));


            } catch (IndexOutOfBoundsException e) {


                System.out.println("ERROR!");


            }


        }


    }


}

Comments