The code is a simple Java program that uses the List data structure. It reads a series of commands from the user, performs the specified operations on a list, and then prints the final state of the list. The operations can be either 'INSERT' to add an item at a specific position, or 'DELETE' to remove an item at a specific position.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The solution approach is straightforward. It first reads the number of elements to be added to the list and adds them. Then it reads the number of operations to be performed and performs each operation as per the user's input. If the operation is 'INSERT', it reads the index and the item to be inserted at that index. If the operation is 'DELETE', it reads the index of the item to be removed. Finally, it prints the elements of the list.
/**
*
*/
package com.javaaid.hackerrank.solutions.languages.java.datastructures;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
*/
public class JavaList {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List L = new ArrayList();
for (int i = 0; i < n; i++) {
L.add(sc.nextInt());
}
int Q = sc.nextInt();
for (int i = 0; i < Q; i++) {
String op = sc.next();
if (op.equalsIgnoreCase("INSERT")) {
int index = sc.nextInt();
int item = sc.nextInt();
L.add(index, item);
} else {
L.remove(sc.nextInt());
}
}
for (Integer integer : L) {
System.out.print(integer + " ");
}
sc.close();
}
}
If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.