Sort by

recency

|

483 Discussions

|

  • + 0 comments

    my code: import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
    
        int n = scan.nextInt();
        List<Integer> l = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int x = scan.nextInt();
            l.add(x); 
        }
    
        int m = scan.nextInt();
        scan.nextLine(); 
    
        for (int i = 0; i < m; i++) {
            String s = scan.nextLine();
            if (s.equals("Insert")) {
                int a = scan.nextInt();
                int b = scan.nextInt();
                l.add(a, b);
                if (scan.hasNextLine()) {
                    scan.nextLine(); 
                }
            } else if (s.equals("Delete")) {
                int x = scan.nextInt();
                l.remove(x);
                if (scan.hasNextLine()) {
                    scan.nextLine(); 
                }
            }
        }
    
        for (Integer element : l) {
            System.out.print(element + " ");
        }
    }
    

    }

  • + 0 comments
    public class Solution {
    
        public static void main(String[] args) {
            Scanner in=new Scanner(System.in);
            int n=in.nextInt();
            List<Integer> list=new ArrayList<>(n); 
            for(int i=0;i<n;i++){
                list.add(in.nextInt());
            }  
            int q=in.nextInt();
            for(int j=0;j<q;j++){
                String str=in.next();
                if(str.equalsIgnoreCase("Insert")){
                    int x=in.nextInt();
                    int y=in.nextInt();
                    list.add(x,y);
                }
                if(str.equalsIgnoreCase("delete")){
                    int z=in.nextInt();
                    list.remove(z);
                }
            }
            for(int num:list){
                System.out.print(num+" ");
            }
        }
    }
    
  • + 0 comments

    the sample test case is passed but all other test cases got failed

    Scanner sc = new Scanner(System.in); int n = sc.nextInt();

        List<Integer> l = new ArrayList<>(n);
        for(int i =0; i<n; i++){
            l.add(sc.nextInt());
        }
        int q = sc.nextInt();
        sc.nextLine();
        for(int i =0; i<q; i++ ){
            String str = sc.nextLine().trim();
             if(str.equalsIgnoreCase("Insert")){
                int index = sc.nextInt();
                int value = sc.nextInt();
                sc.nextLine();
            l.add(index,value);
        } 
        if(str.equalsIgnoreCase("Delete")){
                int removeindex = sc.nextInt(); 
                 l.remove(removeindex);
           }
        }
           l.forEach(l1 -> System.out.print(l1 + " "));
    
  • + 0 comments

    This is my solution:

    List elements = new ArrayList<>(); Scanner scan = new Scanner(System.in);

    int size = scan.nextInt();

    for(int i=0;i

  • + 1 comment

    Delete is not working

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        List<Integer> list= new ArrayList<>(n);
        for(int i=0;i<n;i++){
            list.add(scan.nextInt());
        }
        int q=scan.nextInt();
        for(int j=0;j<q;j++){
            String str=scan.nextLine();
            if(str.equalsIgnoreCase("Delete")){
                int m=scan.nextInt();
                list.remove(m);
            }  
            if(str.equalsIgnoreCase("Insert")){
                int x=scan.nextInt();
                int y=scan.nextInt();
                list.add(x, y);
            }   
        }
       list.forEach(i->{System.out.print(i+" ");});
    }
    

    }