Sort by

recency

|

2020 Discussions

|

  • + 0 comments

    Time complexity: O(n)

    Space complexity: O(n), if deallocates dynamic array

    int compute_idx(int x, int last_answer, int n){
        return (x ^ last_answer)%n;
    }
     
    int* dynamicArray(int n, int queries_rows, int queries_columns, int** queries, int* result_count) {
        *result_count = 0;
        int* ans_arr = (int* )malloc(queries_rows*sizeof(int));
        // initialize 2-dimensional array
        int** res_arr = (int** )malloc(n*sizeof(int* ));
        for (int i = 0; i<n ; i++){
            res_arr[i] = (int* )malloc(1000*sizeof(int));
        }
        int* res_idx = (int* )calloc(n,sizeof(int));
        int last_answer = 0;
        for (int i = 0; i<queries_rows; i++){
            int temp_idx = compute_idx(queries[i][1], last_answer, n);
            if (queries[i][0] == 1){
                res_arr[temp_idx][res_idx[temp_idx]++] = queries[i][2];
            }
            else{
                last_answer = res_arr[temp_idx][queries[i][2] % res_idx[temp_idx]];
                ans_arr[*result_count] = last_answer;
                (*result_count)++;
            }
        }
        // Free memory for res_arr and res_idx
        for (int i = 0; i < n; i++) {
            free(res_arr[i]);
        }
        free(res_arr);
        free(res_idx);
        return ans_arr;
    }
    
  • + 0 comments

    finding the solution for this question is not the problem, but trying to understand.

    tips if it helps,

    You need two array:

    • n dimensional for storing the result of query 1 section.
    • temporary arr for storing lastanswer generated from query 2 section.

    What are queries: example of provided sample data

    • n=2 #query=5
    • 1 0 5 = query 1, x=0, y=5
    • 1 1 7 = query 1, x=1, y=7
    • 1 0 3 = query 1, x=0, y=3
    • 2 1 0 = query 2, x=1, y=0
    • 2 1 1 = query 2, x=1, y=1
  • + 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

    public static List dynamicArray(int n, List> queries) { // Write your code here List> arr = new ArrayList<>(); for (int i = 0; i < n; i++) { arr.add(new ArrayList<>()); }

        List<Integer> result = new ArrayList<>();
        int lastAnswer = 0;
    
        for (List<Integer> query : queries) {
            int queryType = query.get(0);
            int x = query.get(1);
            int y = query.get(2);
    
            // Calculate index using (x XOR lastAnswer) % n
            int idx = (x ^ lastAnswer) % n;
    
            if (queryType == 1) {
                // Append y to the arr[idx]
                arr.get(idx).add(y);
            } else if (queryType == 2) {
                // Retrieve element at position y % size of arr[idx]
                int value = arr.get(idx).get(y % arr.get(idx).size());
                lastAnswer = value;
                result.add(lastAnswer);
            }
        }
    
        return result;
    }
    
  • + 0 comments

    I really don't understand what it wanted me to do. it doesn't complicate, but i have to research to understand that what it really wanted me to do

      // Write your code here
        arr := make([][]int32, n) 
        result:= make([]int32, 0)
        lastAnswer := int32(0);
        for _, query := range queries {
          qType := query[0]
          x := query[1]
          y := query[2]
          idx := (x ^ lastAnswer) % n
        if qType == 1 {
          arr[idx] = append(arr[idx], y)
        } else if qType == 2 {
          lastAnswer = arr[idx][y % int32(len(arr[idx]))]
          result = append(result, lastAnswer)
          }
        }
        return result