 Alright guys so I'm going to explain the concept of arrays because I got this video of a question from one of my old hacker rank videos about how arrays work and I'm going to explain how arrays work because this is just basically like this is the basic most basic data structure that you could possibly understand and arrays are the old like bare minimum data structure that you should definitely know okay if you don't understand arrays then this is a it's pretty hard to learn data structures at that point okay so what are arrays let's say that I am waiting at Starbucks for coffee right so this is Starbucks and I and I just got to Starbucks right here this is me at the end of the line and I just got right here this is me okay this is me I'm right here and I'm at the end of the Starbucks line and I got there too late okay way too late and then Starbucks is like they want to hand you out the coffee and stuff like that but the line is too damn long so what do they do well in order to make it fair to everyone they're going to give everyone card numbers and whenever they read the card number you'll pick up and get your coffee and pay for your coffee okay so this would be more fair because instead of just waiting in line the whole time they just get a card number and then if they call your call when you they call your call number you just go get your coffee and that's it well how can I assign a car assign the car number to every single person to make it more fair we simple we are going to assign a number in this the line that you're waiting at to get your coffee based on how far you are from the first person so the distance you are from the first person that's going to be when you're going to get your coffee okay so like here this this guy let's say in this guy's name Joe right he is one step away from the first person so we're going to have him one we'll give him the car number one let's say this guy named Kim right well how far is he from the first person simple two he's from two from this first person right because we're going to assign him a number of two because that's going to be when he's going to get his coffee okay well let's say this is Stacy alright let's give her a little hair like that how far Stacy from the first person three and then how far am I at this end of the person from the first person four right because one two three four yeah that's how far I am from the first person and that means well what does that mean what does the first person get when he gets his coffee simple well assign him a value of zero okay this is going to be the first person that's going to have to assign him a value of zero okay and that's basically what arrays are they're basically objects that you every single element for each object you're going to give they are indexed starting from zero and how far they are from the zeroth element so here at the end I'm at four at the beginning I'm at zero okay so now you might be wondering how exactly how many elements are there in this array or objects how many elements are there how many are there well they're five people one two three four five so there's five people and the index we're indexing is starting from zero okay so what does that tell us the last person in our array is going to have four so what does that mean that means that at the end of the array the last index is going to be the number of elements minus one so it's a number of people total number of people so last element is equal to total number total elements minus one okay because we're indexing from zero okay so if I have five people my last element is going to have an index of four and if I have six people let's say I have six people I have another person here let's say I had six people here right six total number elements well my index is going to be five right six minus one will be five so that's basically arrays and if I were to draw this out like how it is in memory it would be like object one object two whatever whatever object these are and the index from zero one two three four five yada yada yada so this is the array so if my length of the array or the number of elements is n right let's say the number of elements is equal to n my last element is going to be at n minus one okay so my last index is going to be at n minus one right so because we have in this case we have six elements the last index is going to be n minus one and our first first element is going to be starting from zero okay so that base that's basically how arrays work um now i'll explain how you loop through an array now in uh c plus plus java whatever language you're doing how are we going to loop through the array simple we start at zero until we reach the end okay so what does that mean i'm going to do four normally we have an index int i is going to zero equal to zero and i'm going to keep adding one until i reach the total number of elements okay so if the number of elements is n i'm going to go up to n and i'm going to add one now here's a thing about four loops when you loop me through arrays okay remember this is the total number of elements right and i'm going to add one until i get to the total number of elements of n the last so that means the last value of i the last value of i in this condition the last value of i is going to be n minus one okay in this for loop we start i equals zero and we go up to n the last element that's less than n is going to be n minus one right our last our last uh index i is going to be n minus one so this is how you for loop through an array so you're going to go through i equals zero go up to the last all number of elements in this case n and then you just add one and then if i want to do something really nice let's say i want to print out something it's in c plus plus i would do c out which is to print something out and i'm going to print out my let's say i have like r as i call this array r okay i'll just r at index i and print out a new line and that's how you loop through an array all right so that's basically how arrays work there's nothing else to it but uh there's actually there's a few things you have to think about let's think about more things about arrays what are the caveats oh whoops what are the caveats of arrays well like i told you arrays you remember we have a specific index and we have two three four if i have an array so this is our array remember the caveats array remember i have index from zero up to n minus one right so that's that's an array and the number of elements is n right so n is the number of elements what's the problem of this data structure one thing is let's say i'm at let's say i have a bunch of objects here right a bunch of people object i don't know people are not objects but let's say i have a bunch of objects i'll just assume that these are objects okay let's say i have a bunch of objects here here's a caveat removing something is awful think about it if i have to remove something i if i have to remove this whatever value at three right i have to literally copy a move every single person after three at index three down one so whatever at this person i have to move him here and then whatever if if there were more people here let's say there was like another person here i have to move him to the next spot right and then yada yada yada so this is removing is a pain in the butt removing is a pain in the butt because if i remove someone from here i have to literally copy every single value after him move him to the front that's a caveat of arrays removing is painful okay what's another caveat what's another problem in arrays adding something all right so let's say i have a continuous people of uh let's say i have values one two three four index right okay so what arrays tell us is that they get uh you specify a size and you allocate memory for whatever uh values you have in your array so in this case i i specified a size number of elements is five and it gave me an array of elements zero one two three four okay that's the good thing about arrays here's a problem what if i want to add another element to this array if i want to not add another person here that's the issue i literally have to create another new array i can't because there's five people i only i have five people in this array i cannot i have to create a completely new array then add that person in so i literally have to have like one two three four five now i have to have before it was five people now i have to have specify now number elements and there's now six right i have to have one two three four five six then i could have him in here and then move everyone move everyone here move everyone here move everyone here gotta move this guy here right i gotta move this here move this here and then i could have him join all right and then i have to specify the index one two three four and five so that so the problem about arrays is adding and removing right adding new elements removing new elements that's the only issue that's the issue about it now the good thing about arrays is that the index indexing is the best thing about arrays i want to get the fifth person just give me array at five whatever array name is that at five this would give me this guy right i want to get the second person right i just have array at two it would give me this guy right it would give me this this person right this this guy would give if i said array thing i want to find this array it would give me this person right that's the good thing about arrays is that uh there's indexing so this is a good thing good thing the good thing about arrays is indexing okay so the caveats of arrays is adding and removing takes a lot of time because you have to copy everything over removing you have to you have takes time because you have to copy everything over indexing the good thing about arrays is that you could index and you could get a specific value at the person at the place they're at okay um so that's basically how arrays work uh if you like in in c++ or java whatever uh you could if the in the syntax would be something like int uh or not int so like uh if you want to create an array of integers right integers booleans whatever you normally specify your the whichever value you want to create so like integer boolean character so whichever type of array you want so this is like the type right and then you would specify the name of your array so normally i would have like r array r for a standing array or something or anything uh it's better to have good variable names so it depends on what you're trying to do with your array but yeah normally you would do this and then uh in in java you would actually have to have your brackets which is like that and then specify like new new new uh int and then give your size so if i would say n is equal to five number of elements five then i'll give five and that's it okay now in uh c++ uh you don't actually have to specify the new so this is this is java right now in c++ let's say it's c++ all i have to do is just specify int r and then just pass in five okay so that's all i have to do uh it depends on what language it is um if you want to create a new object like array of objects you would have to specify whatever object name it is the type and then your name of your array and then yeah that would that's what you would do okay now let's talk about two-dimensional arrays okay so just like in in um math like in math we have matrices remember math if you're taking linear algebra or whatever we have matrices so this is like um matrices are basically like tables right you have tables of values so like that so let's say i have this like a table right table of rows and columns like an excel spreadsheet like if you know on on online there's like excel spreadsheets stuff like that well this is basically what a matrix is okay so in matrixes uh this is what the matrix is what the table looks like right um so here's the thing is that in programming languages right we don't actually have like a specific way to like create this depending on some programming languages right i know in like uh in java or c++ i'm not sure if you could actually uh i'm not sure about the new new ones but you read uh i'm not sure if you could actually create specify whatever number of rows and columns for your 2d 2d arrays right so what do you do if you want to create arrays of some amount of rows and columns well simple this is what you do uh you first create a one a okay this is what you do you first specify the number of rows you want and then you allocate you create arrays based on that number of rows right so if if in let's say i want to create rows of like five right so in uh c++ what you would do is you would allocate uh an array of of number of rows of five so you'd have five rows so one two three four five right so that you would do something like that and then for each single cell in your number of rows you would have a point to whichever number of columns you have right so here let's say i have rows equal to five and now i want to create like a table like this right but i want uh wait this is this should be five right let's say i want to columns to be five also right so what i would do is i would create five rows one two three four five five rows i think this is six one two three four five six okay my bad um hold up let me see you something uh okay let me just erase the last row i would create five rows and then what do i do for each single row in my five rows i'm gonna have a point to another array of five columns so one two three four five one two three four five one two three four five and one two three four five one two three four five six oh damn i can't count okay yeah so this is how your 2d array internally that's how it works like it doesn't know how many columns you want so you actually have to like specify um you have to create the columns and rows based on that so that's how this that would work and uh if you want to just like regular arrays it's specified by a specific index right so here um it's specified like row number row row row by column right there's like um regular arrays it's specified by column so if this is a 2d array all right um we're gonna label them all has a index from zero zero one two three four and here's gonna be zero one two three four all right so if i want this specific cell it would be r at zero zero okay that would give me this specific cell if i want this specific cell it would be at row four four row four column four so that would be r at four four right at this specific cell would be column four four and yeah that's how uh 2d arrays work for 2d arrays so um in c++ if you want to do this what you would do is you would actually create your vector a vector is basically or uh it's basically an array but except um you could resize it right it does its own resizing so you don't actually have to do it yourself but um yeah vectors are actually a good way to do things in c++ because you don't have to like they have like a bunch of methods so it's better to use vectors instead of just using actual arrays but yeah that's how i want to create my array i would do something like this and it's specified the number rows so here in this case so i would have rows five right five rows so i would do something like that and then for every single row from zero up to row less than five right for every single row so this is going to go up to the last element is going to go up to four right because four is going to be the last value that's less than five it will go up to four and um what i'll do as i said r at every single row i'm going to equal to another vector which is now going to be another integer vector and i'm going to specify every the number of columns i want so that would be in this case five columns also okay so this is how you would create your 2d matrix in c++ using vectors vectors are basically just uh rays so that you could resize it so yeah um i'm not sure if you guys could see it i hope you guys can see yeah so that's what you would do let you go through everything a row and then specify the number of columns um if you want to like have uh yeah yeah that's what you do i was going to talk about like yeah you could plug in like whatever uh variable you want into this in this as well so like let's say if i wanted n rows and then m columns i would plug in n and m here i'd plug in n for the number of rows there but yeah you could pass in variables also if you want to change something so yeah that's how you would do some uh 2d's arrays and c++ and in java in java you could actually you would this would be an array list right that's what you do but yeah that's how you do this um that's how you do 2d arrays um sometimes you could actually you could actually create ragged arrays which is like 2d arrays except each side is different so you could have like at 0 be like three columns and at 1 be four columns right and at 3 you would be like two columns and stuff like that okay so on and so forth okay um so how would you go through how would you go through your 2d array in your in your um in your 2d matrix uh what you would do is just like one two three four one two three four five and then remember the it points to like one two three four five so and so on and so forth so how would you print on all the values of your 2d array in java or in c++ simple you would go through every single row so we go just intro to row equals zero and you go through every single number of rows which is the number of rows in this case it would be zero one two three four five right because we have one two three four five elements and then you would have to go through every single number of columns so it would be into column zero and then we're going to go up to five columns call plus plus and then you would just print it so in java you could do c system dot system dot out dot print land in c++ you do c out so c out uh it would be your array whatever array in your name it is so whatever array name you do put here uh it doesn't matter like what name is i like to call this array r because just to make more sense in a b row and then call yeah that's how you would print out this is how you print out your array 2d array yeah that's how you would print out your 2d array and yeah that's how you do it um there's also other ways you could print it out uh instead of doing like this you could do like auto which is like a shorter version of it so instead of doing like indexing you could print out like uh four auto x of r is less than number rows and then just print out like that um you could also use like one liner one liner lambda expressions which they added in java and then c++ yeah you could do that also i mean you could use for each loops you could use one line um lambda expressions to do it however way you want to do it it depends but yeah that's how you that's how arrays work i hope you guys enjoyed this video rate com subscribe i'll check you guys later i'll probably do another video on um i'll do another video on the current uh code forces contest that just came out today i'll put that up there at the end of later today but yeah that's all i'll do all right rate com subscribe i'll check you guys later peace