 what's up guys my name is michael and welcome to my youtube channel today we're gonna do another problem from 629 is d carousel so there's a carousel that has n figures of animals each figure is numbered from one to n in according to the carousel movement after the nth figure the figure with the number one follows so each figure has its own type the type of the animal corresponding to the figure horse tiger and so on the type of the animal of the i figure is called t of i so here's a figure we have one two three four five six seven eight nine we have so we have nine figures and then each of these represent the corresponding animal so the number each number each each figure represents a corresponding animal okay so in this example we have nine nine figures n is equal to nine and the the array t each number in array t represents the each of the animal type in these nine figures so here we see five five one so that's so in the beginning it's five five one so five is like dolphins because we can see five five both these are dolphins so we have five five one fifteen uh one five five one one okay so yeah each of the numbers in the array t represents a certain type of figure in this picture okay so you see like five five one fifteen one five five one one so like five five is like a dolphin uh one is the horse fifteen is the tiger or cat thingy uh yeah one is the horse again five five is dolphins and then we have one one horses okay so you want to color each of these in the following colors you think it's boring if a carousel contains two different figures with distinct animals going one after the other are colored in the same color so we're our task is to color the figures in such a way that the number of distinct colors is minimum as possible and there are no figures of the different types going one after another colored with the same color so in other words we are trying to color these figures the minimum number of colors possible right and figures that are different animals have to be different colors right so like they don't want us to have different animals with the same color they don't want us to do it so like no figures of different types one after the other are colored in the same color so that means that different animals have to have uh different colors that's what it means so i've used k different distinct colors and color the figures denoted from one to k okay so i'm going to explain this using pen and paper pencil and paper because this kind of this is i don't know how else you to explain this okay so i'm going to explain it with pen pencil and paper okay guys so this is the figure that just redrew and this is really hard to look at so i'm actually going to draw the actual array because i don't think any of you guys can understand what this means okay so i'm going to draw the actual array so five five one fifteen one five five one one okay so this is the actual array so our job is basically to minimize to color all these figures and minimize the number of colors we're coloring it right and uh figures right next to each other have to be different colors so five five if i call this one and then uh five five right so uh no i'm not my bad so let's say in this case five one these figures are different right because they're not the same numbers right so that means they have to be different colors so i can't just color these two like one one at the same time right so that i have to color these different figures if i have if i have if i'm using two colors like one and two then i have to color this one and then color this uh two okay i can't just be one one that's what they mean here and also because i'm using an array now we have to make sure that this last element that we're at its next element is the beginning because it's looping in a circle like we saw in the figure above so this adjacent value which is right next to it we have to make sure we're checking the one going looping back to the beginning also when we're coloring this okay so um i actually couldn't solve this question because it's one of those questions where we're starting to head into like division one territory where we have to start using like different types of uh algorithms that i don't really i'm not a division one person yet so i don't know but i'm going to explain the editorial with you guys okay so let's give it let's show you guys an example let's say i have an example where it's like uh one two two one two two okay so like let's say let's say we're not using this example all right we're not going to use this let's say i have an example one two two one two two okay so i have six i have six elements six figures okay well let's see we're trying to minimize the number of distinct colors to use right so if i'm going to minimize the number of distinct colors to use what is the minimum number of colors i could use two right i could use one and then two then i could alternate them one two one two one two right so if let's look at it there's six elements here one two three four five six and i want to color them uh with uh different values with different colors so i'm going to do that so i could just do this one two one two one two okay and if you could see this uh look at it's the element uh for six elements i could just color them one two one two one two okay um here's the problem is that let's say i have like seven elements right i can't just color this one two one two one two one two right because then my last element one is going to be the same element as the one starting the beginning right and if these two values are actually like different like let's say this was like a four then that's an issue here right because i just i'm not allowed to have different values with the same color right we're trying to not have different values with the same color we're trying to different values must have different colors okay so like one four they cannot both have one one okay so that's an issue here but for any even numbers so let's say let's say i have four size of four one two three uh let's just give in random let's like this okay one two three two whatever okay whatever figure it is i could just color this one two one two okay because the last the last value is not going to interrupt with my beginning value okay so if it's an even if the size even i could just color in one two one two one two for the whole array figure okay because there's not going to be an odd number there's not going to be a lot of the last value going to interrupt whatever my original whole thing value of my whole arrays and like my whole coloring system going okay all right now here's a problem so we know that if it's even i could just if the size is even i could do one two one two one two okay here's a problem now we have to think about the case when it's odd okay so let's say i have n is equal to five okay and in this example we have every single adjacent number is completely different figures okay completely different figures right every adjacent we don't have two adjacents with the same figure okay that could interfere like our last example so in this case i could let's try just doing one two one two okay one two one two okay now at the last one since i know this is going to be a different figure than this i know i can't put one here my last value okay we're assuming that all these figures are different right all these figures are completely different so my last number i know i can't put a one here because if i put a one here then it has the same color as the beginning starting value okay but i can't put a two here either right if i put a two here it'll have the same value as this adjacent right every single different color must have different colors right so what can i do i could put a three so i could literally just put a three i could i have a different color and put a three so for even numbers i'm not even numbers for odd numbers with odd sizes and completely different figures all figures are different i could just color with a minimum number of colors num colors min color num colors is going to equal to three okay i could put one two one two one two one two in all these everything before and then the last value i could just put three okay so that's how you do this problem okay for no this is assuming that there's no adjacent no adjacent figures that are saying the same okay so as you can see here all the figures are different no adjacent figures are the same in this case okay so that's what i'm saying here all right now things get very complicated when you have figures that are adjacent and the same and it's odd okay so i'm going to give an example like in here one two one two okay so here let's give let's give the actual example one two one two two okay so here's the problem okay so these values are so what am i going to do here so we know that the there's there's some so there's some there's adjacent figures here like this and this right next to each other adjacent right next to each other that are the same type same figure okay so here's the problem here i can't just do one two one two one two because if i do that and i put a three well well i could do one two one two and then a three but then that's there's actually a way to color it so that i don't have to do that okay because i could actually use two colors to color this okay as you can see in one of the examples here they use two colors to color this they colored it as one two one two two okay so pretty much if adjacent figures that are at the same color uh not same color the same figure right adjacent figures that are the same figure like these two right it's right next to each other two two right i could color them with the same color okay but i also could color them with different colors because there's no they're adjacent the figures are the same they're fine so we have to figure out a way to color them with to find a formation that allows you to do this coloring okay so uh i'm gonna explain the code how they do this so let's see in the code that they explain here what we could do is we could loop from once we find the adjacent indexes okay so here's what the editorial did let's say i'm gonna find i'm gonna loop through here and i find this adjacent index one two three four one two three four four and then this is index four and then this is index five okay so oh wait zero one two three four okay let's index by zero okay just just because i mean that's how arrays work okay zero one two three okay so this index let's say i find the index where they're adjacent and the same type okay now what do i have to do okay so what we're gonna do is we are going to loop from this point onward that it's the same type and fill it with different colors so we're gonna do one two one two one two from here on forward so that's gonna be one two and then there's no more none there okay then what we're gonna do is we are gonna loop from um oh actually my bad my bad okay so here's what we're gonna do from this index onward i'm gonna i'm gonna alternate from one two one two one two okay so from this index on i'm gonna alternate one two one two one two okay so that ensures that i'm gonna have different values from this index onward okay all right from here this index to the bottom i'm going to alternate one two one two as well so i'm gonna basically alternate um one then two one two okay so this is going to ensure that i'm not going to have any different problems coloring because what's this is going to ensure is that at this point these two values are going to have the same color okay and that's good these two values have the same color like it doesn't matter right we don't care if uh like what we care is different different types have to have different colors right same types can have same colors like that doesn't matter so this ensures that uh one they're gonna be the same color and then if i'm coloring everything alternating this way downward then i'm i ensure that all these different uh figures are going to have different different uh how should you say this they're gonna have different colors like all the different figures from this point downward is gonna have different colors and all different figures from this point upward is gonna have different colors okay so that that actually helps a lot okay and it also ensures that there is no more uh no issues involving colors okay and let's say we did have like another uh different type of coloring like the same type same type uh then you then it also fixes that as well because like it doesn't matter if they have the same color and different uh same type but different color like it doesn't matter about that what we really want is to have different colors different types have the different colors as you see here so the different types one two is a different type and they have different colors okay so that's that's what we really matter okay so yeah that's how you do this problem i'm gonna code it up with you guys and then i'll pretty much yeah that's it okay guys so i forgot to add something if if all the figures have are the exact same types like two two two two right i could just literally make all these figures have the same color i can make them one one one one one and the reason why is because uh there's no different figure that has there's no yeah there's no different figure in this in this situation here like there's no different figure there's no two one there's no two three two four there's no figure in this this array that i have to change colors so the same figures exactly the same type i could just lay put them all as one one one and so the minimum number of colors is one so men colors is one okay so i forgot to explain that to you guys so yeah okay guys so i don't feel like actually uh retyping my solution over and over again so i'm actually just gonna explain my solution to you guys right here so then i don't have to retype it okay so what did i do first well first i had to read in the the array okay so uh first before everything we have to read the number of test cases and then while number of test cases dropping down i'm going to read in my array uh my number of elements and then i have to create my array this array is basically the different type of figures right remember t that's the all the type of figures that we have for e uh for every figure right with the different figures that we have to check that no adjacent uh no different figure must uh have the same color right no different figure it can have the same color right the different figures must have different colors right we have to check that right so here's the the data data is basically just representing every single figure okay so i have to boolean called all equal because remember if they're all equal i could just label them all as ones right so that's what abs boolean to check if it's all equal right originally i said it to true okay then i'm going to read in my all my my data my values right all my figures that i'm reading in um if i'm not in the first figure so i'm not reading the first figure so that checks i is not equal to zero right i'm going to check the figure right below it right and if it's not equal then i'm going to set my all equal to as false right because that means that there's at least one of them is not equal okay and then i'm going to set all them as false uh all equal to be false okay all right then i'm going to have my vector answers and this answer is going to store the every single coloring that i store for each uh each figure right so here i have uh i also have a boolean called same adjacent and i said equal to false in case of to check if there's an adjacent element that's the same right if there's an adjacent figure that has the same type then i have to check that right remember we have that other case okay so uh if if it's still all equal right like all the elements are equal right like that means that this this never happened this let's say this never happened and all the elements are all equal well and i could just print out one and then loop through all the number of elements and print one every single time all right that's what i did here and then i printed nl new line okay so that's the easiest case to do the next easiest case is if the number of elements remember we said in the beginning is even if the number of elements is even then we know that the last element can't be like different than the first right and i could just in the light at one two one two one two so i said i do that here if last element is even then i'm going to set the total or the minimum number of colors print out the minimum number colors is going to be two and i'm going to literally just print out one two one two one two okay i'm going to loop through from i equals zero to the n and that's the number of times i'm going to print out one two one two one two um here is this is just a print out one two one two one two okay so if i mod two is equal to zero i print out one two one two one two okay here's where things get interesting after this else this means that the number of elements is odd okay and this is where it's pretty gets pretty interesting here all right so what am i going to do i'm going to loop through from what all my elements right all my elements all my figures inside i'm going to check if my current figure is equal equal to the figure right next to it right figure right next to it what do i what does this represent this represents mod so i plus one mod n the reason why i'm doing this is because if i'm at the last figure right last figure i have to loop back to the beginning right to check if the last figure compared to the beginning and this helps prevent any issue with the bounds right if i do i plus one then mod by n that would the last number is going to go back to the beginning so when i'm at the last at the end i'm going to check the beginning again right now that makes sure that fixes that edge case so uh if if this case is true what am i going to do i'm going to set my same adjacent is equal to true the reason why i do this is so that uh if the if i have two elements that are the same adjacent right that are the same figure and they're adjacent next to each other then i am going to set uh do do set these things right i need this to basically make sure that i don't print the the threes right so like if this is false then i'm going to print threes like i'm going to print the three they're down there so i'll check that i'll explain that later but for now same adjacent is true right um so if same adjacent is true right what are we going to do we're going to loop from i plus one which is the one where it's the same uh same number right the the first so we found the first occurrences when they're uh when they're the same figure right i'm going to loop from that point onward and what am i going to do well i'm going to have this uh this this is a pretty cool trick you could do that i learned from the editorial where uh i have an value in x is equal to zero right and then what am i going to do i'm going to basically xor this x to be zero xor by one and then extra by zero and then one so this x is literally just going to set one two one two one two okay that's what i learned from this editorial so from this position onward that it's the same i'm going to set set it one two one two one two that's what i learned in this editorial yeah so that's what this for loop does it's going to set everything onward be that at the point one two one two one two one two right uh that point where the two values are the same it's going to set one two one two one two okay all right and then then i'm going to do the same thing from the second position downward right so there's the first position that i'm going to set one two one two one two to the right one two one two one two and then from here on the other position i'm going to set one two one two one two downward All right, and that will ensure that at least one of these values will have the same color, right? And then the different values are going to have different colors. That's what it's going to ensure. Okay, so that's what this for loop does. And then once this is done, I just print out the minimum number of colors, which is two, and then I just print out my answer. And then I break. Okay, I break. The reason why I break is because I don't have to do this again, right? I don't have to do this for loop again. I break after I'm done coloring. I'm done coloring. I break. Okay. All right, now let's say that we didn't have the same adjacent. So the same adjacent is still false. So this, this never occurred, right? If that never occurred, I just print out three as a minimum number of colors. And then I'm going to print out one, two, one, two, one, two, except the last element that I'm going to print out. The last element I'm going to print out three. Okay, so that's what this does. This, I'm this looping from zero to n minus one is going to print out all the, all the elements from zero up to the last one. I'm going to print out one, two, one, two, one, two, except the last element, I'm going to print out a three and that prints out that basically ends up the whole code of carousel. I hope you guys enjoy this video. Great. Come subscribe. I'll check you guys later. Peace.