Hackerrank Java List Solution
2 min read

Hackerrank Java List Solution

Hackerrank Java List Solution

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

Insert  at index :
Insert x y

Delete the element at index :
Delete x

Given a list, , of  integers, perform  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,  (the initial number of elements in ).
The second line contains  space-separated integers describing .
The third line contains an integer,  (the number of queries).
The  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 , and the value  must be inserted into  at index .
  • If the first line of a query contains the String Delete, then the second line contains index , whose element must be deleted from .

Constraints



  • Each element in  is a 32-bit integer.

Output Format

Print the updated list  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

Insert  23 at index .

Delete the element at index .

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

Solution in java8

Approach 1.

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 scan=new Scanner(System.in);
        int n=scan.nextInt();
        List<Integer> list=new ArrayList<Integer>();
        for(int i=0;i<n;i++)
            {list.add(scan.nextInt());}
        n=scan.nextInt();
        for(int i=0;i<n;i++)
        {String Q=scan.next();
         if(Q.compareTo("Insert")==0)
         {list.add(scan.nextInt(),scan.nextInt());
         }
         else
             {list.remove(scan.nextInt());}
        }
        for(int i:list)
            System.out.print(i+ " ");
    }
}

Approach 2.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int elements = scanner.nextInt();
        List<Integer> list = new ArrayList<>(elements);
        for (int i = 0; i < elements; i++) {
            list.add(scanner.nextInt());
        }
        int queries = scanner.nextInt();
        for (int i = 0; i < queries; i++) {
            String query = scanner.next();
            if ("Insert".equals(query)) {
                 int index = scanner.nextInt();
                int value = scanner.nextInt();
                list.add(index, value);
            } else {
               int index = scanner.nextInt();
                list.remove(index);
            }
        }
        list.stream().forEach(n -> System.out.print(n +" "));
        scanner.close();
    }
}

Approach 3.

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 scan = new Scanner(System.in);
        int x = scan.nextInt();

      List <Integer> myList = new ArrayList<Integer>(x);
        for (int i =0; i< x; i++)
        {
            myList.add(scan.nextInt());

        }
        int numQ= scan.nextInt();

        for (int i=0; i<numQ; i++)
        {
            if (scan.next().equalsIgnoreCase("insert")){

                int pos = scan.nextInt();
                int num = scan.nextInt();

                myList.add(pos,num);



            }
            else {

                myList.remove(scan.nextInt());
            }


        }

        for (Integer i : myList)
                   System.out.print(i+" ");


    
    }
}

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×