• + 26 comments

    BOOOOM! That was challenging and fun. I wish the language of the challenge were easier to understand. For example, the lastAn should have been named "lastAnswer" so as to be easier to understand. I thought lastAn was some kind of mathematical term. I would recommend the person responsible for making these challenges read "Clean Code" by Uncle Bob Martin!

    • + 4 comments

      I completely agree, the wording of the question is terrible. I thought 'lastans' would be assigned the value of 'size'.... Your comment put me back on the right track. Thanks.

      • + 1 comment

        Agree the wording is not goode enough. I was not quite understand his explaination why he came up to the output initially. Got it and solved the problemm finally. Thanks.

        • + 4 comments

          here is problem solution in python java c++ and c programming. https://programs.programmingoneonone.com/2021/05/hackerrank-dynamic-array-solution.html

          • + 1 comment
            [deleted]
            • + 0 comments

              Find Solution in Python :) https://github.com/alibaba/nacos/compare/develop...SourabhMishra048:patch-1#diff-7ba6d8d49d41a083c560e01c1a23eb836ddfaebb38a8ea39a28fcaa0e7286ae9

          • + 0 comments

            @yashpalsinghdeo1 if you add explanation of your solution and explanation of the question it will be helpfull to us.

          • + 0 comments

            your code is almost the same with mine.. but why is it my code having rubtime error in most of the test cases?

      • [deleted]
        + 5 comments

        Agreed, very confusing wording of the question. For me, the use of curly braces in the example made me think, wait, should seqList be a set? But he said it was a list? Does the word sequence suggest it should be ordered? I guess this question reflects real life, when the client cannot express accurately what they want!

        • + 1 comment

          completely agree.. question is not explained well enough to write code..

          • + 0 comments

            yeah I too agree to the fact.It became cryptic for me

        • + 2 comments

          Also, they say "size", which is non-specific, when they mean "length". Size could be size in memory.

          • + 0 comments

            Agreed that the wording was a bit odd.

            The usage of "size" instead of length is consistent the Java list interface, but not so much in general.

          • + 1 comment

            Just adding a comment because I understood this incorrectly. Size is the y % length of seqList[index].

            So it is the length of the sub-array in the seqList array with an index ( seq ) calcuated from the original XOR ( ( x ^ lastAnswer ) % N ).

            Size = y % len( seqList[seq] )

            • + 2 comments

              Thank you! Thank you! After banging my head on the wall for an hour trying to understand their modulus statement, this cleared it up. Such a poorly written set of instructions.

              • + 1 comment

                Took an hour on understand. very confusing problem

                I think you guys interpreted problem wrongly for query type 2

                • Determine sequence number using ( ( x ^ lastAnswer ) % N ).
                • ( y % size ) tell the index in an sequence. we need assign value at that index in the sequence(Step 1) to lastAnswer.

                Hope its clear now :)

                • + 1 comment

                  what are the values of x and y ?

                  • + 0 comments

                    The values of x and y are the items at queries[i][1] and queries[i][2].

                    let x = queries[i][1]; let y = queries[i][2];

                    I had to read the instruction 10 times to understand. What it means by (1 or 2) x y is that in the Ith index of queries, the first value will be 1 or 2, the second value will be what x's value will be and the third will be y's value.

                    Hope this hlps.

              • + 2 comments

                import java.io.; import java.util.;

                public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); int N= sc.nextInt(); int q=sc.nextInt(); int lastAnswer=0; int x=1; int a[] [] = new int[q][3]; List> seqList = new ArrayList>(); for(int i=0;i

                        }
                
                        x=(a[i][1]^lastAnswer%N);
                
                        if(a[i][0]==1)
                        {           
                                  seqList.add(new ArrayList<Integer>());
                                  seqList.get(x).add(a[i][2]);               
                        }
                        else
                        {   
                                    int s = seqList.get(x).size() - 1;
                                    lastAnswer =seqList.get(x).get(s);
                                        if(lastAnswer!=0)
                                    System.out.println(lastAnswer);            
                        }
                
                    }
                    }
                    }
                    what is wrong with this code as it is giving only one positive test case?????
                
                • + 0 comments

                  Why don't you print lastAnswer when it's 0?

                • + 0 comments

                  I have the same behaviour in javascript. My success test is the 10th one.

                  The problem is seq calculation returns a value much bigger than N, the actual length of seqList.

                  The solution is to add the paranthesis on xor calculation ( a ^ b) % c

        • + 3 comments

          There are 2 queries , when to use which query?Like in the example they are executing 3 type 1 query and then type 2. When to switch from 1 to 2?

          • + 1 comment

            I had the same doubt. The query formats are: Query: 1 x y(for query 1) & Query: 2 x y(for query 2).

            • + 1 comment

              Do we take the input of q (query) or is it given?

              • + 0 comments

                it is already given..check the input format section

          • + 0 comments

            queries[i][0] represents query type either 1 or 2 queries[i][1] denotes x value queries[i][2] denotes y value

          • + 0 comments

            When query is 1 0 1, that is at 0th index if it is 1, it refers to query 1 similarly if query is 2 0 1 that is it refers to query 2

        • + 0 comments

          No in real life, you ask the client clarifying questions. If you build using unclear requirements, you will fail.

        • + 0 comments

          so true

      • + 1 comment

        I found that the example at the bottom contradicted what the explanation of the different queries did. They actually detail 2 different operations.

      • + 0 comments

        true. I find it unnecessarily hard to understand. The example is organized in a bad and confusing way.

    • + 7 comments

      import java.io.; import java.util.;

      public class Solution {

      public static void main(String[] args) {
      
      Scanner sc = new Scanner(System.in);
      
          int n = sc.nextInt();
          int q = sc.nextInt();
          int inc=0,inb=0;
          int last = 0,x;
          int  a[][] = new int[q][3];
          int  b[] = new int[100000];
         int  c[] = new int[100000];
      
          for(int i = 0; i<q ; i++)
          {
              for(int j=0;j<3;j++)
              a[i][j] = sc.nextInt();
      
              x = ((a[i][1]^last)%n);              //sequence s1 or s0
              if(a[i][0] == 1)                    // query 1 or 0
              {
                  if(x == 0)                         
                  b[inb++] = a[i][2];
                  else
                  c[inc++] = a[i][2];
              }
              else{
      
                      if(x == 0)
                      last = b[a[i][2]%(inb)];
                      else
                      last = c[a[i][2]%(inc)];
      
                      System.out.println(last);
      
      
              }
      
      
          }
      
      
      }
      

      }

      WOULD ANYONE MIND HELPING ME TO FIND WHATS WRONG WITH THIS PROGRAM?

      • + 1 comment

        n is number of sequences so ther might possible more then 2 sequences. In that case you need more array like b[],c[].Max possible size of these arrays are q so there you can also save some memory.

        • + 1 comment

          Thanks a lot friend!I got my mistake.

          • + 1 comment

            I have the same problem can you explain??

            • + 2 comments

              n = number of sequence lastAns=0 q = number of query sequences S0={} S1={} S2={} . . . Sn={}

              for queries type (1) 1 x y index of sequence id = (x^lastAns)%n append y in Si-th sequence

              (2) 2 x y index of sequence id = (x^lastAns)%n lastAns = valueAt(y%(size of Si-th sequence)) and pint lastAns

              after every query value of lastAns will be updated according to query type.

              • + 2 comments

                what is y and x

                • + 2 comments

                  y and x are integer inputs (given in question)

                  • + 3 comments

                    what is the size of every sequence??

                    according to me it must be n, but i'm guessing that size of each sequence should be exactly the amount of elements that are present at that moment.

                    • [deleted]
                      + 1 comment

                      Yes, the sequences start of as size 0 and grow as you add elements in.

                      • + 0 comments

                        so what should we take the size initially

                    • + 0 comments

                      Yeah. That sentence has terrible phrasing.

                    • + 1 comment

                      Sequence is a dynamic list. in Java Arraylist or in C# List is ok to use i think?

                      • + 1 comment

                        Can also use 2D Vector in Cpp

                        • + 0 comments

                          yes i did it with vectors

                • + 7 comments

                  watch each input line after the first one.

                  1 0 5

                  this means, query type 1, x is 0 and y is 5

                  • + 0 comments

                    Thank you so much, understood this now

                  • + 0 comments

                    Thanks! It was unclear.

                  • + 0 comments

                    Thank You So much. Really it was unclear.

                  • + 0 comments

                    oh, without your explanation I've could not figure it out. Thank you!

                  • + 0 comments

                    tnks bro.... i was missing that....

                  • + 0 comments

                    Thanks a lot! the problem statement is clear now.

                  • + 0 comments

                    Thank you! I was soo lost. Thanks for explaining.

              • + 1 comment

                hi how to store all those sequences. Because then I need nearly 100 arrays to store.

                • + 1 comment

                  yes you are right , you have to do this creating dyanmically allocated array of structures and each structure would contain and array and a variable which will always point to the end of array

                  • + 0 comments

                    thanks

      • + 1 comment
        [deleted]
      • + 1 comment

        Use ArrayList,it will be easier

        • [deleted]
          + 2 comments

          how to create N number of arraylist dynamically ??

          • + 1 comment
            ArrayList[] sequenceArray=new ArrayList[length];
            for (int j = 0; j <length; j++) {
                        sequenceArray[j]=new ArrayList<Integer>();
            }
            

            where length is the first int read from the input.

            • + 0 comments

              Or you can just create and ArrayList of ArrayLists.

              ArrayList> arr=new ArrayList<>();

              each element i in the list arr will be another ArrayList.

          • + 2 comments

            To avoid errors such as "Unsafe operations",Use the following:-

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

            And to later on perform the operations, use expressions like:

            int t=(a[i][1]^last)%n;
            b.get(t).add(a[i][2]);
            
            • + 0 comments

              You're wonderful for this man

      • + 1 comment

        Many, many users have repeatedly asked that others not post their code in the discussions. If you wish help with your code, simply say so, and if another user responds yes, then send them a private message. Please.

        • + 1 comment

          Is this really true? I've never seen others comment on this, and it's seems to be a function of the discussion board to post your (working) result and compare to others' solutions.

          • + 1 comment

            I find it much better to just submit your code, then copy the link and posting it on here. It seems to be a way to deter people from just copying the code and not learning anything. The submission link method requires them to forgo any points if they have not completed the problem.

            • + 0 comments

              the function dynamicArray is returning a List which is not specified properly what list is get to return?

      • + 11 comments

        In c++:

        int n,q;
        cin >> n >> q;
        
        vector <vector <int> > s(n,vector <int> ());
        int lastAnswer=0;
        for (int i=0;i<q;i++) {
            int t,x,y;
            cin >> t >> x >> y;
        
            if (t==1) {
                s[(x^lastAnswer)%n].push_back(y);
            }
            else {
                lastAnswer=s[(x^lastAnswer)%n][y%s[(x^lastAnswer)%n].size()];
                cout << lastAnswer << endl;
            }
        }
        
        • + 3 comments

          Here why vector > s(n,vector ());

          • + 0 comments

            what is used here vector>

          • + 0 comments

            it's a vector of vector

        • + 0 comments

          what is T,X,y Here

        • + 0 comments

          Why did you do lastAnswer=s[(x^lastAnswer)%n][y%s[(x^lastAnswer)%n].size()]; in the column sec for s?

        • + 0 comments

          great approach!

        • + 2 comments

          we can make it a bit more simple by replacing vector decl. by

          vector <int> s[n];
          
          • + 0 comments

            what will that do ? will it declare 2d vector of n rows ?

          • + 0 comments

            no.

        • + 5 comments

          I wrote this and i am getting Run time error. ALmost same code. Can anyone tell me why this isn't working.

          #!/bin/python3
          
          import math
          import os
          import random
          import re
          import sys
          
          # Complete the dynamicArray function below.
          def dynamicArray(n, queries):
              lastNumber = 0
              seqList=[];
              for i in range(n):
                  seqList.append([])
              res = [];
              for k, x, y in queries:
                  index = (x^lastNumber)%n
                  if k==1:
                      seqList[index].append(y)
                      print(seqList)
                  else:
                      size = len(seqList[index])
                      print(seqList)
                      print(size)
                      lastNumber = seqList[index][y%size]
                      print(lastNumber)
                      res.append(lastNumber)
                      
              return res 
          
          if __name__ == '__main__':
              fptr = open(os.environ['OUTPUT_PATH'], 'w')
          
              nq = input().rstrip().split()
          
              n = int(nq[0])
          
              q = int(nq[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()
          
          • + 2 comments

            Hi ! Just remove your "print" instructions because they perturb the output...

            Edit: Why minus votes? Could someone explain?

            • + 0 comments

              Bro youre correct, those who downvoted thoughts that adding print in between makes no negative effect on grading.

            • + 0 comments

              Thanks! Really wasted a lot of time because of this.

          • + 1 comment

            I have a small problem: when we generate the dynamic array, i used seqList= [[]] * n instead of your approach, but we i used seqList.append(y), every list in seqList add the element of y. Do you know why it happened like this?

            Thanks verymuch

            • + 2 comments

              Use [[] for i in range(n)] instead.

              • + 0 comments

                thanks dude!!

              • + 2 comments

                Can you please explain why seqList= [[]*n] * n is not working

                • + 0 comments

                  becuase all the empty sequences youre creating referring to the same object, they all will simultaneously when you make inplace changes to one of the "[]".

                • + 0 comments

                  if you append a digit to one sublist then the number appends to all the sublists...so its not a good idea d = [[]]*n d[0].append(1)

                  [[1],[1],...]

          • + 0 comments

            lastNumber=seqList[index][y%len(seqList[index])]

          • + 3 comments

            It's XOR operation not Addition.

            def dynamicArray(n, queries):
                lastAnswer = 0
                answerList = []
                seqLists = [[] for i in range(n)]
            
                for i in range(len(queries)):
                    query_type = queries[i][0]
                    x = queries[i][1]
                    y = queries[i][2]
            
                    if query_type == 1:
                        seq = (x^lastAnswer) % n
                        seqLists[seq].append(y)
                    else:
                        seq = (x^lastAnswer) % n
                        size = len(seqLists[seq])
                        index = y % size
                        lastAnswer = seqLists[seq][index]
                        answerList.append(lastAnswer)
            
                return answerList
            
            • + 0 comments

              bro how r we taking x and y as given by u

            • + 0 comments

              My eyes are bleeding here. Why did you need the "i" for ? If you need each element in a list just ask "for element in list". If you need to name each element in list just ask for "element1, element2, element3 = list" And why would you write something twice ?

            • + 0 comments

              Didn't you get a runtime error, cause i've written the same code and got runtime error

          • + 0 comments

            this is the solution in python

            def dynamicArray(n, queries): # Write your code here la=0 a=[] for i in range(n): a.append([]) for i in queries: t=(i[1]^la)%n if i[0]==1: a[t].append(i[2]) if i[0]==2: la=a[t][i[2]%len(a[t])] print(la)

            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())))

            dynamicArray(n, queries)

        • + 0 comments
          vector<vector<long>> l;
          
          for(int i = 0; i < n; i++)
          l.push_back( *(new vector<long>()) );
          

          You can also do this to initiate the 2D vector

        • + 0 comments

          why did you use y%s[(x^lastAnswer)%n].size()? in here: -

          lastAnswer=s[(x^lastAnswer)%n][y%s[(x^lastAnswer)%n].size()];

          cause, acc to que, it should be just this much: -

          lastAnswer=s[(x^lastAnswer)%n][y];

        • + 1 comment

          I also had used vector only but, i am getting segementation fault though, i dont know what is wrong!?

          vector>s(n);

          vector<int>m;
          int lastAns=0;
          for(int i=0;i<queries.size();i++){
              if(queries[i][0]==1){
                  int re=(queries[i][1]^lastAns)%n;
                  s[re].push_back(queries[i][2]);
              }
              else{
                  int re=(queries[i][1]^lastAns)%n;
                  lastAns=s[re][queries[i][2]];
                  m.push_back(lastAns);
              }
          }
          
          • + 1 comment

            Bro your code passes only sample test case . Other test cases gives segmentation fault.

        • + 0 comments

          thank you so much... i didn't know that how to declare vector vector > s(n,vector ()); this really helped

      • + 0 comments

        it works only for two sequences if there are more than 2 sequences then error

    • + 1 comment

      Funny thing is it's a simple piece of code, just 11 lines in python, shame the question is so poorly framed.

    • + 2 comments

      After reading the description several times, trying to map the output to the input, then reading the comments, reading the description again, taking a ten minute break, reading the description again and only after that realizing what the types of query actually means, I have to agree that it was fun too solve. Seriously is there any way to obscure the intention any more? The description is way to hard for the work that has to be done.

      • + 0 comments

        hey can u plz help me in understanding the question

      • + 0 comments

        im still not undestand the question :/ can you please describe in easy way? :)

    • + 1 comment

      Agreed - I read the question start to finish twice and still don't completely understand what the question is asking.

      • + 1 comment

        When you first read the question you look over at the difficulty level and wonder if "Easy" is relative to a genius. The person who wrote this question definitely needs to rewrite it so it's more clear.

        • + 0 comments

          No, the sign of a genius is to be able to explain a subject matter to a child. This guy couldn't explain to me how to work a faucet if he wanted to.

    • + 0 comments

      Boy, I had no clue what he was asking about. I did not understand what the problem is.

    • + 0 comments

      So true. The question was really abstruse. Kind of infuriated me.

    • + 0 comments

      can u pls explain me this question

    • + 0 comments

      Agreed! Problem discription is way too confusing.

    • + 0 comments

      can u please explain the question?

    • + 0 comments

      Hard to read, it made me want to avoid that challenge.

    • + 0 comments

      I completely agree, the wording of the question is terrible.

    • + 0 comments

      The style of writing used was very much the kind we find in technical books or some textbooks. It is closer to "mathematical language".

    • + 0 comments

      3 years later, and it's still worded in a confusing way. Also, the mathematics appear to use non-standard symbols.

      The example gives (1 XOR 7) % 2 = 3. So 1 XOR 7 is 6, but 6 mod 2 gives 0. So apparently they mean integer division when using the % sign??? Who does that??

      Edit: Oops, never mind. Re-read it once again, and they do go with 6 mod 2 = 0. But the zero is then used as an index into array1 (3,5), and LastAnswer is set to that. That's where the 3 comes from. Okay, I think I understand it well enough to start programming finally.

    • + 0 comments

      I am still wondering if the difficulty I found to understand the challenge was because I need to study more or was because the question was made in a confusing way?

    • + 0 comments

      hi,

      I have uploaded a video tutorial on the same.

      I hope this comment will help you to understand the problem.

      https://www.hackerrank.com/challenges/dynamic-array/forum/comments/591510

    • + 1 comment

      Hello, I'm Fakhruddin. I am new to HackerRank. Sorry for this silly question but I can not understand this specific question at all. If you could please help me understand what the problem wants to convey. It would be really great.

      • + 1 comment

        hi,

        I hope this comment will help you to understand the problem.

        https://www.hackerrank.com/challenges/dynamic-array/forum/comments/591510

        • + 1 comment

          thanks!!

          • + 1 comment

            most welcome. If you find my tutorials helpful, please provide your feedback like, dislike , comment etc. on my video. It motivate me to do more for you all

            • + 0 comments

              thanks~ I'll do it ~

    • + 0 comments

      agree, challenge should invoke logical thought process in our mind not lost in understanding the english statments

    • + 0 comments

      The description of what they want is almost incomprehensible.

    • + 0 comments

      Very true.. Can't understand what he is trying to explained. I also have sevaral of doubts. i would skip and jump to other challenges rather spending time to understand this. It would help if he could map this to practicle situation if it is not possible to expalin at least.

    • + 0 comments

      Agreed. For me to understand language of query took time.

    • + 0 comments

      The question is very poorly written. For all those who are having doubt, the following should be done for "2 x y" type queries

      index = (x ^ last_answer) % n
      last_answer = arr[index][y % len(arr[index])]
      
    • + 0 comments

      Agreed, wordings of question is very difficult to understand. It took more time to understand rather than solving this.

    • + 0 comments

      Challenging, and due to the very obtuse language, not fun. Correct me if I'm wrong, but it's making a simple hashmap using a specific/custom hashing algorithm, right? If it weren't for previous examples to follow along with, I would not have solved this, and if this were a real world coding challenge to whiteboard, there would be a lot of questions, enough that I assume the interviewer would be graded more poorly than the code. /rant

    • + 0 comments

      new easy python solution https://hacker-rank-dsa-python.blogspot.com/2022/05/dynamic-array.html