Sort by

recency

|

479 Discussions

|

  • + 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+" ");});
    }
    

    }

  • + 0 comments

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

    public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int l = scan.nextInt();   
    
        List<Integer> list = new ArrayList<>(l);
        for(int i = 0; i<l; i++){
            list.add(scan.nextInt());
        }
    
        int q = scan.nextInt();
    
        for(int i= 0; i<q; i++){
            String b = scan.next();
            if("Insert".equalsIgnoreCase(b)){
                int x = scan.nextInt();
                int y = scan.nextInt();  
                list.add(x, y);
            }
            if("Delete".equalsIgnoreCase(b)){
                int x = scan.nextInt();
                list.remove(x);
            }
        }
    
        list.forEach(i->{System.out.print(i+" ");});
        scan.close();
    }
    

    }

  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            sc.nextLine();
            List<Integer> L = Arrays.stream(sc.nextLine().split(" "))
            .map(Integer::parseInt).collect(Collectors.toList());
            int Q = sc.nextInt();sc.nextLine();
            for (int i = 0; i < Q; i++) {
                sc.nextLine();
                List<Integer> xy = Arrays.stream(sc.nextLine().split(" "))
                .map(Integer::parseInt).collect(Collectors.toList());
                if (xy.size() > 1){L.add(xy.get(0),xy.get(1));}
                else {L.remove((int) xy.get(0));}
            }
            System.out.println(
                L.stream().map(String::valueOf).collect(Collectors.joining(" ")));
        }
    }
    
  • + 0 comments

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

    public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        // System.out.println(n);
        ArrayList<Integer> list=new ArrayList<>();
        for(int i=0;i<n;i++){
    
            list.add(sc.nextInt());
        }
        int q=sc.nextInt();
        for(int i=0;i<q;i++){
            String queryTpe=sc.next();
            if(queryTpe.equals("Insert")){
                 int index=sc.nextInt();
            int value=sc.nextInt();
            list.add(index,value);
            }
            else if(queryTpe.equals("Delete")){
                int index=sc.nextInt();
                list.remove(index);
            }
    }
    list.stream().forEach(i->System.out.print(i+" "));
    

    sc.close();

    }}
    
  • + 0 comments

    import java.util.*;

    public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<N;i++){
            list.add(sc.nextInt());
        }
        int Q = sc.nextInt();
        for(int i=0;i<Q;i++){
            String s = sc.next();
            if(s.equalsIgnoreCase("Insert")){
                int index = sc.nextInt();
                int value = sc.nextInt();
                list.add(index,value);
            }else if(s.equalsIgnoreCase("Delete")){
                int indexToDelete= sc.nextInt();
                list.remove(indexToDelete);
            }
        }
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
    }
    

    }