Java List Hackerrank Solution
For Explanation Watch Video:
Sample Input
5
12 0 1 78 12
2
Insert
5 23
Delete
0
Sample Output
0 1 78 12 23
Code:
import java.io.*;import java.util.*;
public class Solution {
public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); ArrayList<Integer> al = new ArrayList<>(); for(int i=0;i<n;i++){ int ele = scn.nextInt(); al.add(ele); } int q = scn.nextInt(); for(int i=0;i<q;i++){ String query = scn.next(); if(query.equals("Insert")){ int x = scn.nextInt(); int y = scn.nextInt(); al.add(x,y); }else{ int x = scn.nextInt(); al.remove(x); } } Iterator itr = al.iterator(); while(itr.hasNext()){ System.out.print(itr.next()+" "); } }}
Comments
Post a Comment