Sort by

recency

|

494 Discussions

|

  • + 0 comments
    public class Solution {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            sc.nextLine();
            List<Integer> li=Arrays.stream(sc.nextLine().trim().split(" ")).map(Integer::parseInt).collect(Collectors.toList());
            int n=sc.nextInt();
            while(n-->0){
                sc.nextLine();
                if(sc.nextLine().trim().equals("Insert")){
                    li.add(sc.nextInt(), sc.nextInt());
                }else{
                    li.remove(sc.nextInt());
                }
            }
            li.stream().forEach(ele->System.out.print(ele+" "));
        }
    }
    
  • + 0 comments

    public class Solution {

    private List<Integer> theList;
    private Scanner sc;
    
    public Solution(Scanner sc) {
    
        this.theList = new ArrayList<>();
        this.sc = sc;
    
        int sizeList = sc.nextInt();
        init(sizeList, sc);
    }
    
    private void init(int sizeList, Scanner sc) {
    
        if (sizeList > 0) {
    
            theList.add(sc.nextInt());
            init(sizeList - 1, sc);
        }
    }
    
    public void choose(String action){
    
        int index = sc.nextInt();
    
        if(action.equalsIgnoreCase("insert")){
    
            int element = sc.nextInt();
            theList.add(index, element);
    
        } else theList.remove(index);
    }
    
    public List<Integer> getList() {
    
        for (int element : theList) {
            System.out.print(element + " ");
        }
    
        return theList;
    }
    
    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
    
        Solution solution = 
            new Solution(sc);
    
        int queries = sc.nextInt();
    
        for(int i = 0; i < queries; i++){
    
            sc.nextLine();
            String action = sc.nextLine();
    
            solution.choose(action);
        }
    
        solution.getList();
    
    
    
    }  
    

    }

  • + 0 comments

    This is a great exercise for practicing list manipulation and handling dynamic input! funinexchange 247.com

  • + 0 comments
    import java.util.*;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            int n = scanner.nextInt();
    
            if (n < 1 || n > 4000) {
                throw new IllegalArgumentException("The entry isn't an integer between 1 and 4000");
            }
    
            List<Integer> list = new ArrayList<>();
    
            for (int i = 0; i < n; i++) {
                if (scanner.hasNextInt()) {
                    list.add(scanner.nextInt());
                } else {
                    throw new IllegalArgumentException("The entry isn't an integer");
                }
            }
    
            int q = scanner.nextInt();
    
            if (q < 1 || q > 4000) {
                throw new IllegalArgumentException("The entry isn't an integer between 1 and 4000");
            }
    
            for (int i = 0; i < q; i++) {
                String queryType = scanner.next().trim();
    
                if ("insert".equalsIgnoreCase(queryType)) {
                    int x = scanner.nextInt();
                    int y = scanner.nextInt();
    
                    if (x < 0 || x > list.size()) {
                        throw new IllegalArgumentException("Insert index out of bounds");
                    }
    
                    list.add(x, y);
    
                } else if ("delete".equalsIgnoreCase(queryType)) {
                    int x = scanner.nextInt();
    
                    if (x < 0 || x >= list.size()) {
                        throw new IllegalArgumentException("Delete index out of bounds");
                    }
    
                    list.remove(x);
                } else {
                    throw new IllegalArgumentException("Unknown query type: " + queryType);
                }
            }
    
            for (int number : list) {
                System.out.print(number + " ");
            }
            scanner.close();
        }
    }
    
  • + 0 comments

    Here is Java List solution - https://programmingoneonone.com/hackerrank-java-list-problem-solution.html