Sort by

recency

|

2037 Discussions

|

  • + 0 comments

    I do not understand the question itself. What is x and what is y ? what are queires. where is 5 oming from in the first example they gave

  • + 0 comments

    The introduction is wrong; it does not mention that the index needs to be modulo by the n parameter, as described in the explanation. :@@@@@

  • + 0 comments

    My Java solution with o(n) time complexity and o(n) space complexity:

    public static List<Integer> dynamicArray(int n, List<List<Integer>> queries) {
            // Write your code here
            List<List<Integer>> arr = new ArrayList<>();
            IntStream.range(0, n).forEach(i -> arr.add(new ArrayList<>()));
            
            int lastAnswer = 0;
            List<Integer> answers = new ArrayList<>();
            for(List<Integer> query : queries){
                int queryType = query.get(0);
                int x = query.get(1);
                int y = query.get(2);
                int idx = (x ^ lastAnswer) % n;
                
                switch(queryType){
                    case 1:
                        arr.get(idx).add(y);
                        break;
                    case 2:
                        lastAnswer = arr.get(idx).get(y % arr.get(idx).size());
                        answers.add(lastAnswer);
                        break;
                }
            }
            return answers;
        }
    
  • + 1 comment

    The problem prompt idx = (x XOR lastAnswer) doesn't say that it should be modulo'ed by the size of the array.

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/MYdQxgmJ4Sk

    vector<int> dynamicArray(int n, vector<vector<int>> queries) {
        int lastAnswer = 0;
        vector<int> result;
        vector<vector<int>> arr(n);
        for(auto query: queries) {
            int type = query[0], x = query[1], y = query[2];
            int idx = (x ^ lastAnswer) % n;
            if(type == 1) {
                arr[idx].push_back(y);
            }
            else {
               lastAnswer = arr[idx][y % arr[idx].size()];
               result.push_back(lastAnswer);
            }
        }
        return result;
    }