Home » Blog » Dynamic Array – HackerRank Solution

Dynamic Array – HackerRank Solution

Hello guys,

In this article, we are going to see the solution for dynamic array which part of problem solving section in HackerRank.

Here two types of queries will be used that is functioned as below.

  1. Query: 1 x y

    1. Find the sequence seq, at index ((x ^ lastAnswer) % n) in seqList.
    2. Append integer y to sequence seq.
  2. Query: 2 x y

    1. Find the sequence seq, at index ((x ^ lastAnswer) % n) in seqList.
    2. Find the value of element y % size in seq (where size is the size of seq) and assign it to lastAnswer.
    3. Print the new value of lastAnswer on a new line.

Now here in 1 x y and 2 x y, 1 and 2 represent the type of the query, and in y % size the size the is length of the output array.

The below code can be useful to solve the problem. In the dynamicArray() function sequences is the 2D list of integer that will used to store the output at ith list. The answer list store the value of lastAnswer that will be printed.

The first for loop will create the n sequences. The second loop will iterate through all the queries. and find the value of seq by using the formula ((x ^ lastAnswer) % n). Then we have to check the type of the queried by taking the first element from the query list which is store in type varible in this case.

Now if the query type is 1 then we have to simply store the y value of the query into the seq’th list. If the query type is 2 then we have to find the index using the formula y % size, where the size is seq’th list size. Then we have to calculate the index element from the seq’th list, and append it to the answer list and return the answer list.

import java.io.*;
import java.util.ArrayList;
import java.util.List;

class Result {

     * 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

    public static List<Integer> dynamicArray(int n, List<List<Integer>> queries)
        int lastAnswer = 0, in = 0;
        List<List<Integer>> sequences = new ArrayList<>();

        List<Integer> answer = new ArrayList<>();

        for (int i=0;i<n;i++)
            sequences.add(new ArrayList<Integer>());

        for(int i=0;i<queries.size();i++)
            int type = queries.get(i).get(0);
            int x = queries.get(i).get(1);
            int y = queries.get(i).get(2);

            int seq = ((x^lastAnswer)%n);

            if (type==1)
                int a = y%sequences.get(seq).size();
                lastAnswer = sequences.get(seq).get(a);

        return answer;



public class DynamicArray
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        int n = Integer.parseInt(firstMultipleInput[0]);

        int q = Integer.parseInt(firstMultipleInput[1]);

        List<List<Integer>> queries = new ArrayList<>();

        for (int i = 0; i < q; i++) {
            String[] queriesRowTempItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

            List<Integer> queriesRowItems = new ArrayList<>();

            for (int j = 0; j < 3; j++) {
                int queriesItem = Integer.parseInt(queriesRowTempItems[j]);


        List<Integer> result = Result.dynamicArray(n, queries);

        for (int i = 0; i < result.size(); i++) {

            if (i != result.size() - 1) {



Hope this explanation will help you to solve the problem.

The problem can be found in HackerRank at : https://www.hackerrank.com/challenges/dynamic-array/problem

Happy Coding…

Leave a Reply

Your email address will not be published.