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.
Simplest answer: List integerList = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int numOFElements = sc.nextInt();
for (int i = 0; i < numOFElements; i++) {
integerList.add(sc.nextInt());
}
int numOfQueries = sc.nextInt();
// clearing the buffer
sc.nextLine();
for (int i = 0; i < numOfQueries; i++) {
String str = sc.next();
if (str.equals("Insert")) {
int index = sc.nextInt();
int element = sc.nextInt();
integerList.add(index, element);
} else if (str.equals("Delete")) {
int index = sc.nextInt();
integerList.remove(index);
}
}
for (int element : integerList) {
System.out.print(element + " ");
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Map
You are viewing a single comment's thread. Return to all comments →
Simplest answer: List integerList = new ArrayList<>(); Scanner sc = new Scanner(System.in); int numOFElements = sc.nextInt();