 What's up guys? My name is Michael and welcome to my YouTube channel today We are going to go over this problem called dynamic array on hacker rank So basically we declare a the problem says is to declare a 2d array n of empty arrays They're all zero indexed. We're gonna create an integer and set to zero Then there's two queries in a queries array. All right. The first one is if it's a one What we're gonna do is we're gonna create this variable called index It's gonna equal to x x or our The last variable last answer and then we mod it by n then what we're gonna do is we're gonna append y to this answer Append the integer y to this array array at index IDX The second query we could do is to x y which is in this case. We do the same thing Where we get the index which is gonna equal to x x or a last answer mod by n And then we're gonna sign the very the value array at index at y mod size of our Index to last answer and then we're gonna store this new value into our answer array in the end We're just gonna return our answer array So I'll just show you guys how to do this the code. We're just fall literally following these steps So first thing you want to do is create your two-dimensional ray of size n So that's how you do that here in C++ you vector vector our n vector Integer, okay, because it's a integer integer here Next thing you want to do is create your last variable Your variable is equal to called last and they want it to be said to equal to zero So that's what we do here. We set our last variable last is gonna equal to zero Then they say they want to create an answer array Right array of answer and then to literally just return that in the end. So we're gonna create an empty array called answer Here it's an integer now what we're gonna do is we're gonna loop through our queries So remember there's two types of queries in each query, it's We'll loop through our queries array and in each query. There are two types one is a Query with a value of one then x y and the second query is about query of value two and then x y So in this case what we're gonna do is we're actually going to get both of these values so The first Value of x is x is gonna equal to queries at i at one and the reason why it's i at one is because If you look here at the values here Okay, so if you see here each of the query very Queries has this form of one x y which is an array that has values one x and y So the value of x is going to be at index one Okay, because the value of One is at index zero the value of x is at index one and the value of y is at index two Right. So what we first have to do is let's actually get each of the values of x and y if I just indexing them So we're going to index x is going to equal to queries at at i at one and then the Query for y is going to equal to queries at i at two so zero one two So that's what we do here We're going to set the queries at integer x is going to equal the queries at i at one integer y is going to equal the queries at i at two And I already explained why it's At one and two is because each of these values are arrays. So zero one two, okay So, yeah, i is the current query by the way because this query is a 2d 2 dimensional So once we get our values of x and y We need to create an index variable So they said here Your index is going to always equal to x x or last mod by n So that's the same case for both of them. So I'm going to decide to do it First okay, because no matter what we do we still have to do it anyway. So I'm going to put it outside of the f statement So that's what I do here. So I create integer index is going to equal to x and this is how you xor To xor you use this uh carrot symbol So I take x xor with last Which is last is our last variable and then take this answer and I mod it by n and that's going to give us our index Okay All right, so that's our index in this line this line of index Index is equal to this. Okay. Now what we need to do is that once we have this Right the index, um, we actually have to do the individual statements for both of them So we need to check, uh, which statement we're going to do So do that we have to actually check if the query is equal to one because if the query is equal to one We just have to do These two statements the first two statements and if the query is equal to two we have to do these three statements Write these three statements So to do the value to check if the query is equal to one We just do queries at i at zero is equal to one And then else if we check if queries at i at zero is equal to two Because the first value at each of these arrays is either one or two Okay, like at each of these queries is either one or two for both of these situations and circumstances So now that we have this we need to do the individual Uh individual thing So in the case of when the query is equal to one We need to append the integer y to array at index So to do that all I did was here is uh array at index We're going to push back the value of y We're just going to push back the value of y into array at index The reason this works is because array at index is a two-dimensional array See right here and you can do a push back at the index Now, um, otherwise if it's not If if it's not the query of one we're going to do the values the the stuff for query or two So otherwise we're going to set last answer is going to equal to this whole thing Which is going to be r at index at y mod by r at index dot size So that's what I do here. I set last is equal to r at index at y mod r at index dot size And this is just going to set this new last variable to equal to this And then it said to push it back to or your answers array So store this new value last answer to your answers array. So I just do a push back So I do answers dot push back last we're just going to store this low value last At the end of our forlet. We're just going to return our answer and that'll be done So yeah, and I pass all the test cases. Hope you guys enjoyed this video rate com subscribe This is a tutorial on how to do dynamic array on hacker rank And yeah, hope you guys enjoyed this video rate com subscribe to check guys later. Peace