Java List | HackerRank Solution

Hello coders, today we are going to solve Java List HackerRank Solution.

Java List

Problem

For this problem, we have types of queries you can perform on a List:

Insert y at index x :

 Insert
 x y

Delete the element at index x :

 Delete
 x

Given a list, L, of N integers, perform Q queries on the list. Once all queries are completed, print the modified list as a single line of space-separated integers.

Input Format

The first line contains an integer, N(the initial number of elements in L).
The second line contains N space-separated integers describing L.
The third line contains an integer, Q(the number of queries).
The 2B subsequent lines describe the queries, and each query is described over two lines:

  • If the first line of a query contains the String Insert, then the second line contains two space separated integers x y, and the value y must be inserted into L at index x.
  • If the first line of a query contains the String Delete, then the second line contains index x, whose element must be deleted from L.

Constraints

  • 1 <= N <= 4000
  • 1 <= Q <= 4000
  • Each element in is a 32-bit integer.

Output Format

Print the updated list L as a single line of space-separated integers.

Sample Input

 5
 12 0 1 78 12
 2
 Insert
 5 23
 Delete
 0

Sample Output

 0 1 78 12 23

Explanation

L = [12, 0, 1, 78, 12]

Q0 : Insert 23 at index 5.

L0 = [12, 0, 1, 78, 12, 23]

Q1 : Delete the element at index 0.

L1 = [0, 1, 78, 12, 23]

Having performed all Q queries, we print L1 as a single line of space-separated integers.

Solution – Java List

import java.util.Scanner;
import java.util.LinkedList;

public class Solution {
    public static void main(String[] args) {
        /* Create and fill Linked List of Integers */
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < N; i++) {
            int value = scan.nextInt();
            list.add(value);
        }
        
        /* Perfrom queries on Linked List */
        int Q = scan.nextInt();
        for (int i = 0; i < Q; i++) {
            String action = scan.next();
            if (action.equals("Insert")) {
                int index = scan.nextInt();
                int value = scan.nextInt();
                list.add(index, value);
            } else { // "Delete"
                int index = scan.nextInt();
                list.remove(index);
            }
        }
        scan.close();
        
        /* Print our updated Linked List */
        for (Integer num : list) {
            System.out.print(num + " ");
        }
    }
}

Disclaimer: The above Problem ( Java List ) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *