Sort by

recency

|

2004 Discussions

|

  • + 1 comment

    Horribly written problem statement. Please overhaul the description. It is unreadable.

  • + 0 comments

    The solution does not go beyond four or five elementary operations, so it is quite an introductory problem, which is fine. But the question description is so horribly written. It's like reading someone's notebook that is written with a pseudo-language that only the author understands. Where's the introduction? What's the matter with xor? Because ^ is pretty cool? In the description it is said that query is made of string, but in the code it's a double vector of int out of nowhere, why?

  • + 0 comments
    public static List<Integer> dynamicArray(int n, List<List<Integer>> queries) {
            List<List<Integer>> result = new ArrayList<>(n);
            List<Integer> store = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                result.add(new ArrayList<>());
            }
            int lastAnswer = 0;
            for (List<Integer> query : queries) {
                int index = (query.get(1) ^ lastAnswer) % n;
                switch (query.get(0)) {
                    case 1:
                        result.get(index).add(query.get(2));
                        break;
                    case 2:
                        int indey = query.get(2) % result.get(index).size();
                        lastAnswer = result.get(index).get(indey);
                        store.add(lastAnswer);
                        break;
                }
            }
            return store;
        }
    
  • + 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;
    }
    
  • + 0 comments

    Solution in Python3. The wording is initially very confusing but just try to follow the instructions given.

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'dynamicArray' function below.
    #
    # The function is expected to return an INTEGER_ARRAY.
    # The function accepts following parameters:
    #  1. INTEGER n
    #  2. 2D_INTEGER_ARRAY queries
    #
    
    def dynamicArray(n, queries):
        # Write your code here
        lastAnswer = 0
        results = []
        arr = [[] for _ in range(n)]
        for query in queries:
            if query[0] == 1:
                idx = (query[1] ^ lastAnswer) % n
                arr[idx].append(query[2])
            if query[0] == 2:
                idx = (query[1] ^ lastAnswer) % n
                lastAnswer = arr[idx][query[2] % len(arr[idx])]
                results.append(lastAnswer)
        return results
        
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        first_multiple_input = input().rstrip().split()
    
        n = int(first_multiple_input[0])
    
        q = int(first_multiple_input[1])
    
        queries = []
    
        for _ in range(q):
            queries.append(list(map(int, input().rstrip().split())))
    
        result = dynamicArray(n, queries)
    
        fptr.write('\n'.join(map(str, result)))
        fptr.write('\n')
    
        fptr.close()