We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.util.*;publicclassSolution{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);intn=scanner.nextInt();if(n<1||n>4000){thrownewIllegalArgumentException("The entry isn't an integer between 1 and 4000");}List<Integer>list=newArrayList<>();for(inti=0;i<n;i++){if(scanner.hasNextInt()){list.add(scanner.nextInt());}else{thrownewIllegalArgumentException("The entry isn't an integer");}}intq=scanner.nextInt();if(q<1||q>4000){thrownewIllegalArgumentException("The entry isn't an integer between 1 and 4000");}for(inti=0;i<q;i++){StringqueryType=scanner.next().trim();if("insert".equalsIgnoreCase(queryType)){intx=scanner.nextInt();inty=scanner.nextInt();if(x<0||x>list.size()){thrownewIllegalArgumentException("Insert index out of bounds");}list.add(x,y);}elseif("delete".equalsIgnoreCase(queryType)){intx=scanner.nextInt();if(x<0||x>=list.size()){thrownewIllegalArgumentException("Delete index out of bounds");}list.remove(x);}else{thrownewIllegalArgumentException("Unknown query type: "+queryType);}}for(intnumber:list){System.out.print(number+" ");}scanner.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java List
You are viewing a single comment's thread. Return to all comments →