Sort by

recency

|

492 Discussions

|

  • + 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

  • + 0 comments
    //https://www.hackerrank.com/challenges/java-list
    
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            
            int n = scanner.nextInt();
            List<Integer> list = new ArrayList<>();
            
            for(int i = 0; i < n; i++)
                list.add(scanner.nextInt());
                
            int queries = scanner.nextInt();
            for(int i = 0; i < queries; i++) {
                String query = scanner.next();
                
                int index = scanner.nextInt();
                if (query.equals("Insert")){
                    int newValue = scanner.nextInt();
                    list.add(index, newValue); 
                }
                else
                    list.remove(index);
            }
            
            scanner.close();
            
            for(Integer num : list)
                System.out.print(num + " ");
        }
    }
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
        
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            
                int size = sc.nextInt();
                ArrayList<Integer> elements = new ArrayList<>();   
                
                for (int i = 0; i < size; i++) {
                    elements.add(sc.nextInt());
                }
                
                int numberQueries = sc.nextInt();
                
                for (int j = 0; j < numberQueries; j++) {
                    
                    String query = sc.next();
                    if (query.equals("Insert")){
                        int position = sc.nextInt();
                        int replace = sc.nextInt();
                        elements.add(position, replace);
    
                    }
                    else{
                        elements.remove(sc.nextInt());
                    }
                }
                sc.close();
                
                for (Integer integer : elements) {
                    System.out.print(integer + " ");
                }     
        }
    }