This video offers a clear and logical breakdown of complex competitive programming problems, making advanced algorithmic concepts easy to grasp. It is a highly efficient resource for anyone looking to improve their problem-solving skills and technical intuition.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
CodeChef Starters 240 | Video Solutions - A to D | by Vikas Soni | TLE EliminatorsAdded:
So, hello everyone. My name is because only and I welcome you all in this post contest discussion of code chef starters 240.
So, let's start our discussion by solving the first question which is comfortable sitting.
So, in this question there are n people standing in a line and the i-th person has a friendliness value of AI.
And you want to fit n people around a circular table and a sitting arrangement is called comfortable if every person has at least one neighbor whose friendliness value is less than or equal to that.
And we have to determine whether a comfortable sitting arrangement exists or not.
And the constraint for this question are like test case number of test cases are 100.
The size of the array is 100 and the value of L uh the elements are 100.
So, yeah, there is nothing much about time complexity.
Okay, so yeah, let's analyze this.
So, the name is uh name of the question is comfort table sitting.
So, in this question we are given an array. So, assume that the array is something like this.
The values are like A1, A2, A3, A4, A5.
So, you just have to place them in a circular arrangement. Let's say arrangement is like this.
Such that for each person there should be a neighbor which have a friendliness value less than or equal to its one. So, like for this A3 either either the value of A1 should be less than equal to A3 or this A5 should the value of friendliness of A5 like should be less than or equal to A3. So, at least one condition should be satisfied for this.
Like for this A5 uh it should be either greater than or equal to this one or either greater than or equal to this one.
So, and we have to find the if if some some arrangement exist or not.
So, let's analyze some test cases for this and uh the first uh Okay, so let's analyze some test case.
So, let's assume that the our array is like this 1 3 5 5 6.
So, now like if you try to have a sitting arrangement, you will find that this one is the smallest value.
So, your whatever the arrangement is the neighbors of one are going to be bigger than one, right?
This one because each value is bigger than this each value is bigger than one.
So, like if you have arrangement like this still you can see that uh both of its neighbor are greater than this one, right?
So, you can see here that the sitting arrangement is not possible because every value is bigger than this one.
Okay, so this is our first observation, right? If uh So, our observation one which is if the minimum value has a frequency is equal to one then solution is not possible.
Because in that case, each value is going to be greater than the that mean value.
Solution is not possible.
Okay, so yeah, we Now we know that uh uh the array is something going to be like this. Let's say two two four five six six and something like this.
So, yeah, we know that the minimum value has frequency greater than one.
So, now how you are going to have an arrangement?
So, yeah uh the easiest way you can see here is just uh put them in the sorted way.
So, like you put it something like this.
Because if you put them in a sorted way, you can see that like for seven the lesser value is six here, which is its neighbor. For this six its left value is less than or equal to six and this value is also less than equal to uh this four is also less than equal to five.
This uh uh for this four, the two is also less than equal to four.
For this two, this is also less than equal to two.
But only for this two, in the left one, this seven will be there.
But yeah, we know that for this, the condition will be satisfied from the the neighbor.
So, if you just put them in the sorted order like this, always we are uh we are going to get our solution.
Because, yeah, as we saw here that for each person, its left value, like for this five, that four is less.
For this four, on its uh right, this two is less uh two is smaller.
So, the solution will will always exist.
So, yeah, that was it in the question, like and so, this our observation one was a sufficient condition for getting the solution.
So, yeah, only this is our logic.
So, we have to see that the frequency of the minimum element and if this is uh if the minimum value has frequency is equal to one, then answer is not possible.
Else, the answer is possible.
Else, answer is possible.
So, that's it.
So, for this, what logic I used is I just sorted all the elements and check if these two values are equal, right?
If these two are equal, then I know that this minimum value has frequency greater than one.
And if the these two values are not equal, which means that this minimum value is alone, right? If the array is something like this, let's say it is two and four, so, these two are not equal, then this minimum value has frequency equal to one, that's it.
Let's see the code and uh yeah, this was a pretty easy uh question.
So, we took the number of test case.
This is the input of n. Then, we take the input of the array.
Then, I sorted the array and we check if the smallest value and the second smallest value, which is a r of one, If they both are equal, then the answer is possible.
So, we print yes, else we print no.
So, that was it in the in this question.
So, let's move to the second question of our discussion, which way binary smile.
So, in this question, uh we are given two binary strings A and B.
The binary strings are the strings which have only two values, either zero or one.
So, each element of the string is either zero or either one.
And the length is N.
So, in one operation, Chef can choose any substring A of A that contains at most one frequency of one.
And then we just reverse it.
And we have to determine the minimum number of operation required to convert A into B.
And if this is impossible, we have to output minus one.
Okay.
And yeah, substring is a continuous characters within A, so we will discuss this.
The constraints are so, the number of test cases are till 25.
And also the length of the string is till 25.
So, we have to think something in O of N or O of N log N.
So, yeah, let's discuss this. Uh So, name of the question is binary string.
I I I I I I Binary smile, sorry. Name binary smile.
So, let's first analyze that when the solution is not possible.
Okay.
Okay, when the uh when we have to print is a minus one.
And even before that, let's analyze about the operation we can do, right?
So, let's assume that we have some string.
So, let's say our STR1, which is like this.
Now, what operation we can do? So, like we can take some sub any substring which have at most one one at least sorry, at most one occurrence of the one, okay? So, I mean, we can take the substring like this.
Uh you can see that in this substring, there is only one occurrence of one.
Or you can take the substring like this.
In this substring, there is zero occurrence of one. But you cannot take the substring which is like this because for this, there are two ones.
Yeah, and you take the substring and just reverse it.
So, let's take let's say we take the substring which is this one.
So, then this substring will be reversed.
So, after this operation, the our STR1 will be 100 one then zero zero one zero and one.
So, this is reversed mean this value is now this one, this value is this, right?
This substring is reversed.
Okay, so now you can see that the only change is like the one which was at this position is now at this position.
So, now you have to think that is why this operation can you just transport the one to anywhere in the string or is there any constraint on traversing right like Okay, so one thing is clear, right? The only thing that changes is the position of one, right? Because the the one was here, the one is now here.
Okay, so we have to analyze this operation.
So, let's say Okay, so now let's just take uh see something.
Let's say this is our one.
You want this one to be here.
You want this one to be here. Can you do that?
Uh the answer is yes, you can do that.
So, you you will just take this substring and then this thing will be reversed.
So, zero will come here and one will come here. So, substring will be like this.
And yes, so this one is now at this position.
Now let's say you want this one at this position. Can you do that?
Uh and the answer is yes, you can do that. So, what you will do is you will take this substring and then this one will come at this end.
So, this is also correct.
So, we can see that in one operation we can change the position of only one one and it can be anywhere, like uh like you can uh transport this one to this position or you can have this one at this position by taking this substring or you can move this one to this position or this position.
Right?
But, you can move only one occurrence of one, right? In one operation.
So, this is our observation. Okay.
So, now how uh we will uh get our logic. So, first thing we have to think uh is uh when the answer is not possible.
So, yeah, the only case for that is uh when the number of ones are different. Okay. So, let's say in the substring A, uh the string A is like this and the string B is like this.
Which is the test case.
So, in this uh case, no matter what operation you do, these two can never be equal, right?
Because this has two ones and two ones and this B has only one ones, right?
So, this was our observation one.
This is our observation one is which is if frequency of one is different, then the solution is not possible.
Then solution is not possible.
Or we can say solution not exist.
Okay. But now let's assume that the frequency of ones are equal. Then how you are going to do that?
So, let's say the strings are like this. So, string A is like uh this.
And the string B is like this.
Okay.
So, now you know that for this one, you can move it here. You can move this one from anywhere from this position to this position, right?
Also, the one thing is uh the ones cannot jump each other. So, you cannot move this one to this one. Because for this, you have to take uh this substring. But you cannot take this substring because there are more than one occurrence of one.
So, you can just move So, yeah, the observation is that for this one, which is like the first one in the sub B string B, uh for this at at this position, this first one will come only.
Uh for this This is the second one.
So, at this position, only the second second occurrence of one will come here.
Right? Because like for this one, this one cannot come here, or this one cannot come here.
Because no one can jump this thing, right?
So, that's why uh at this position, this one will come.
Then for the second occurrence of one in the string B, the second occurrence of this uh string A will come.
One uh in string A will come, right?
So, yeah, you can just uh use this approach. So, what we'll do is uh we will traverse from left to right.
Okay? We will find the first occurrence of one.
So, the first occurrence of one is here, and for this it is here.
So, both are not same, right? So, I mean, both are not matched. Okay?
So, you have to move this one.
So, you have to move this one to here, and this thing you can do in one operation only by taking this thing.
So, you take this substring, and now the array is uh string is reversed, and we will get 100.
And everything else is same. So, let's remove this thing, and we get uh 100.
So, now you can see that these both are matched.
Now, you will see the second occurrence of one in the string B.
And this should be matched with a second occurrence of one in the string A, right?
So, now this one have to come this at this position.
This will be also done in a one operation. You take this string and you move it here.
So, yeah.
Then after one operation, it will be like this.
Now, let's let's test.
Assume that the string was like this, okay?
So, we matched the first occurrence in one move. We matched the second occurrence in one move. Then we find the third occurrence of one, okay?
So, this is our third occurrence of one in the string B.
And this is our third occurrence of one in the string A. And you can see that both are at same position initially. So, there is no need to change them.
So, yeah.
You just move ahead and find another ones if there is any.
So, that was it in the code. So, for this, we will use a two-pointer approach. What we will do is we will have a pointer uh at the starting of the string A. And we will have a Let's call this P1.
And we will have a pointer, let's call this P2, at the starting of B.
So, they will move uh so, they will point at some occurrence of one.
If they both are at same position, then we don't have to do any operation.
Okay, let's write it.
Uh so, what we will do is let's assume that this is our string.
Okay?
So, this There will be our pointer P1 and initially and there will be our pointer P2.
So, this P1 and P2 will move until we get any occurrence of one. Okay, so the P1 will point here.
And the P2 will iterate until it points to any one. Okay, so like this is zero, so P2 will move ahead.
Then this is zero, so P2 will So, P2 will also point here.
So, P2 will point here.
Now, we will check if the both ones are at same position.
So, yeah, the P1 is two, so this is index two.
And for the index of P2 is also two.
Both are same.
So, we will not do answer plus plus. So, let's say initially answer is equal to zero.
There is no need to do the operation.
That's okay.
So, then we will move again.
So, now our P2 will come here. Sorry, P1 will move here. It is zero, so it will move again. And P1 will point at this position now.
And then P2 will point at this position.
Okay. Now, we will check if they both are equal, but they both are not same.
So, we will do answer plus plus. So, my answer will be one now.
And then P2 will come here, but yeah, the while loop will end.
So, that's it in our logic.
So, let's see the code.
And just note the point that instead of like moving from this and moving the pointer this way, you can also do like have the pointer here.
Find the first occurrence of ones and then go this way.
So, that was That is the approach I used. So, let's see the code.
So, yeah, this is test case and input.
Uh I found that P1 is the number of ones in the string one.
And this P2 is the number of ones in the string two.
So, I iterate in the string one and count the number of ones and this is count the number of twos.
If they both are not same, then I know that the answer is not possible, so I print minus one and I continue.
So, yeah, the next test case will run.
This code will not run if I write the continue.
Then, initially for my two pointers, the pointers are pointing at the end of the string, which is n minus one index, right?
And the answer is equal to zero.
Until P1 is greater than zero or P2 is greater than zero, I have my this loop.
So, they what this loop is, so like until my P1 is in the range, okay? P1 is greater than equal to zero.
And if the uh if P1 is pointing at the zero currently, I will move left.
Uh and I will stop and I will stop at the position where the element is one.
So, until the position is zero, I will move to left.
Right? Uh I do the same in the string two.
Until P2 is greater than equal to zero, which means I am not out of the string.
If the current element is zero, I move to the left, okay?
So, after this, I know that my P P1 and P2 both are pointing to the index where element is equal to one.
Right?
And so, if anyone is out of the range, I just break.
So, now if P1 and P2 are not equal, index are not equal, I know that I have to do operation.
Uh I have to do exactly one operation.
So, I do answer plus plus and P1 and P2 minus minus after that.
So, this run will uh this loop will run again.
And after that, I uh this is my answer, so I print the answer.
So, that's it in this code in this question.
Uh if you have any doubt, you can uh comment it.
So, now we will move to the third question.
Which is partition.
So, in this question uh we are given an array of an integer and a partition of A is a sequence of X continuous sub-array.
Such that the concatenation equals A.
Okay, so this partition is nothing. We are just dividing our array A into some uh more than one array.
Okay? And for each sub-array DI So see here, we have this array A. Uh we break it into let's say X parts.
So, this B1 is the first part. This B2 is the second part.
And for each part this G of BI denotes the maximum frequency of any element in BI.
So, for this B1 uh the G of BI will be the maximum frequency of any element in this B1 array.
This G of B2 will maximum frequency in this B2 array, and that's it.
And the score of the partition is nothing but the summation of this G of BI for all of the this sub-array.
And we have to find the number of distinct values of F over all partitions of A.
And the number of test case and the N are till 25.
So, again, we have to think something in O of N or O of N log N.
So, let's analyze this question.
The name is uh partition.
>> Okay, so remember that whenever you have to count the distinct count the distinct possible values that's a distinct value.
You have to first of all think that what is the minimum value you can make or let's say here minimum score you can make.
And also you have to find what is the maximum score you can make.
Right?
You have to find this and like after you find this there can be like many possible ways. I mean that that can be possible that uh the whole score is continuous. I mean right? Let's say the score values can be like 2 3 4 5 6 7 8 something like this.
So like if you find this minimum value you if you find this maximum value of score possible then you know that everything in middle is possible.
So the answer will be 8 minus 2 plus 1 here.
Or another case can be possible is that uh the max minus min value is uh so small so you can just brute force there.
Or maybe you can find some pattern or formula there. So yeah this is a important thing that you find the minimum score and the maximum score possible.
Okay.
So let's first analyze like what is the partition mean.
So let's say initially the array is like this A1 A2 A3 A4 and A5.
So I mean you can just partition in how much continuous sub arrays as you want. So So uh you can partition this A1 [clears throat] A2, then this A3, and this A4 A5 alone.
So this will be my one array B1.
This will be my another array B2, and this will be my another array B3.
And then for this, the score will be G of B1 plus G of B2 plus G of B3.
Okay, so let's analyze this by some test case.
So let's say the array is like this, 1 3 2 2 1 1.
So this is our uh array.
Okay, so now maybe you can partition this like uh this thing.
And for this Okay, so for here the maximum frequency is uh one.
For this, the maximum frequency of any element is two, right? Because there are two occurrence of this value two.
For this, the maximum frequency is just one.
Uh sorry.
This is also one, and this is also one.
So the score for this partition is uh five.
Right?
So now think that how we will get the maximum score and the minimum score.
So let's say we want the maximum score.
So for that like we want that each element contributes like I mean what I'm saying is that like this one is contributing one value, right? This one also is contributing one value.
Here like there are two values, but the score is only one.
So, we want each value to contribute.
So, let's say you can partition it like this.
Let's say 1 3 Uh it was 2 2 1 1 2 2 1 1. Okay. So, yeah, the simple observation is you can partition it like this.
And now see, for each thing they are contributing one.
Right? Because like for this array the score is a G of this array is one, right? Because maximum frequency is one.
This also one, for this also one.
For this element is also one. This for also this one and this one. So, yeah, the score will be just the size of the array, size of the array.
Yeah, that's one observation we found.
Okay, but what about the minimum array?
Uh sorry, minimum score possible.
So, yeah, for that you can just do the reverse of what we did here, right? For here, we just partition each element.
But here you can think that I I I just don't partition any element.
So it can be like 1 3 2 2 1 1.
And you don't even partition. So, this is my one partition only.
And for this the score will be three.
Because the maximum frequency is three, which is of the value one, right?
So, yeah, this is three.
So, from this you can also get the observation that the minimum score you can get Let me change the color.
The minimum score that you can get is just the maximum frequency of any element, right? The maximum frequency of any element.
And that's it.
So now we have found the minimum score and the maximum score.
Right? So like for this the like for this array the minimum score is three.
And the maximum score is uh maximum score is six, which is the size of this array.
Okay. Now what?
And so you can think of uh like finding that like you have the score three.
Now can you make the score equal to four? Can you make the score equal to five?
Like can you make the score like which is between this three and this six every score possible?
And yeah, in this case you will find that it is true. So I mean like if you can make the score equal to three and if you can make the score equal to five so every value in between you can also make it. So that's it.
Okay, so and you can prove this by just like taking the several test case and just by into intuition.
I did that I did by that way only like in the contest.
And also this method is really important because I remember in the ICPC Codefest Prelims the fifth question was based on this method. Where you have to find the minimum and the maximum.
And all of the score which is in between them is also possible.
So yeah, that's this is it. Okay.
Now let's prove this thing.
So for proving let's take uh test case.
So let's say my array is like uh this.
Okay.
So, for this we count the maximum value.
So, the maximum value is uh what the size of the array, which is seven here.
And the minimum score is uh the maximum frequency, which is two here.
Okay, so what you can think is let's say we start by the minimum score.
Uh let let's start by the maximum score, okay. So, let's say like initially the score is seven.
So, so let's write here the score.
So, initially the score is seven.
Right?
Now, you have to make the score equal is equal to six.
So, what you can see here is that these are the previous two values, you just combine them.
And now like the score of this array is one only.
So, the total score will be six now.
Okay, now you have to make it five.
So, you can just again uh make this sub-array bigger.
So, now again the score is one.
So, 1 1 1 1, so the score is now five.
Okay, now you want to make it four.
So, yeah, you know the thing, we extend this last sub-array.
So, this one is removed and uh the score is one now. So, the summation is four. So, my score is four now.
Okay, what if we want to make uh the score equal to three?
Again, you will just extend this thing.
So, we extend this thing.
So, now the score is one for this.
And the score is three now, right?
Now, let's say you want to make the sum is equal to two.
Again, you extend this thing.
And now comes the tricky part, okay?
Sorry.
Okay, we want to make it as two, and now comes the tricky part because for this the score is two now, and the score is one, so total score is still three now.
But what we can do is we can keep extending it, right? So, now the score is two.
So, yeah, we you can see that uh if you can you just keep extending this sub array, okay? You keep extending this sub array.
The score is reducing uh and the score can reduce by at most one only, right?
So, yeah, by this proof uh you can say that the score is reducing by at most one for each step you uh add. So, I mean if initially the sub array was one, and now you make it one.
So, the score can be changed by at most one only.
You make it this, and now the score is changed by at most one only.
So, yeah, this is continuous, so you can say that you can find any value. Uh you can make any score possible.
So, that was it. Uh so, yeah, the logic is simple.
What was the logic?
We found that the maximum answer is uh size of array.
And the minimum answer is the max frequency.
So, and the answer will be max minus min plus one.
Right? Because like you can see if the maximum is seven and minimum is two.
Uh then there are seven minus two plus one, six values possible, right?
So, yeah, that's what you have to do.
So, let's see the code.
So, this is our input and we take the input of the array.
Now, we have to find the maximum frequency of any element, right? So, for that I made a map.
So, map of something will just store give me the frequency of that element, okay?
And also this is the max fr which will give me the maximum frequency.
I iterate on each element. I do the map of array plus plus and I do the max frequency is maximum of max frequency or mp of arr of i. So, after this the max frequency will store me the maximum frequency of any element.
And my answer is the maximum answer possible which is the size and the size is n minus the minimum answer possible. So, minimum is max of fr plus one and I just print this thing.
So, I hope that you understood this thing.
Okay, so let's move to the fourth question of our discussion and this was the best question among like this uh four question because like the logic is really amazing. And even if you don't know any hard concepts like the Mobius inversion of the in the number theory, you can easily solve this question.
Okay.
So, in this question we are given that we have a number z and we have to count the number of pairs x y such that that x is less than or equal to Z.
This Y is less than or equal to Z and uh the LCM of these two values must be greater than Z.
And this is strictly greater greater than Z, okay?
And yeah, that's it.
And the range of this Z can be till 10^6.
But also another constraint is that the sum of Z uh don't exceed exceed 10^6.
So yeah, you can see that uh uh you have to solve this in O of Z or O of Z log Z or something like this.
So Okay, just give me a minute uh for Okay, so uh let's discuss this question.
So the name of the question is uh counting LCM.
Uh just a moment.
>> Okay, so like the first thing in this was like if you try to find the number of LCM which are greater than that.
So you will find that this LCM can be any value, okay, so let's discuss here.
So the name is the counting LCM.
And yeah, we have given some value Z.
And uh we have to find the number of pairs, number of pairs XY such that this X and Y both are less than equal to Z.
And their LCM is uh greater than Z, okay.
So >> [clears throat] >> the first thing I analyzed here is uh that this value of LCM of X and Y Let me write more clearly.
This LCM value of X and Y So this value can be very bigger, like I mean, let's say if they are both some prime number near Z, so like they can be till Z square.
So which means they can be till 100 12, which is a very big value.
So it's better So it is better to Okay, this is our observation one.
It's better to count the count the number of pairs which have the LCM less than equal to Z.
Right?
And then we will just do that answer is equal to possible pairs.
Like all possible pairs minus this bad pairs, right?
Uh these are the bad pairs which have the LCM less than equal to Z. Why?
Because now you can see that uh this thing is kind of easy because there are now only Z possible values for LCM, right? I mean the LCM can be from till one to Z.
So there are less options for LCM.
Okay, so now if you know this and like if you have solved the CS CS problem set this is a kind of a fundamental question.
Okay, so now what we will do?
So we know that we have to solve this in O of Z or some complexity like this.
So now we will uh try to think that uh can can we find something like uh Okay, so there are so in the question uh while solving in the contest I tried many approaches, many different approaches. Like what I did was let's say I took some number A and some number B, okay?
Uh let's say this number is like G times A and this number is G times uh sorry, G time X and this is G times Y.
And then I thought uh that uh like the LCM is equal to the multiplication divided by GCD.
So it let's say it is G time X into G time Y and the GCD is equal to G.
So this is G of XY.
And I I just thought like g of xy should be less than equal to z. Then I tried to uh fix some pairs like I tried to fix the GCD.
Let's say GCD is equal to one. So, how many pairs there are? So, then xy should be like this and uh this all uh doesn't work.
So, I just forgot this thing and now let's try to fix something another thing, okay?
So, because like this is we have to solve this in O of z. You can like try something like you try to fix some value and then try to find the number of pairs fixing that value.
Then you just iterate and you get the answer, right? So, let's fix LCM.
So, let's say you have to find the number of pairs such that the LCM is equal to what?
LCM of two numbers is equal to some particular value x, okay? And this x is given to you.
LCM.
Okay, you want to find this. The number of pairs such that LCM of these two numbers is x. And of course, the a and b both should be less than equal to z.
Okay, so for this thing and if you let's say if you don't know any algorithm and if you have not solved any question, like how you will approach this?
So, let's find uh some observation or something, okay?
So, first of all, from this uh you can uh say that this both a and b will be a divisor of x. Will be a divisor of x.
This is uh for sure true.
Uh Okay, and why like let's say Uh why because uh you can take some example. So, let's say uh LCM of uh 4 and 3 is uh 12.
And you can see that 3 is a divisor of 12 and 4 is also a divisor of 12.
Or let's say 4 and 8, the LCM is uh 8.
And both are divisor of 8, you can see.
So, this is our first observation.
And you can see that the number of divisor are not mm that much a big value, right? So, even for a number like 109, the number of divisor are only till 400.
So, you can think in some that way.
Okay. So, now you know And now comes the main part, okay?
So, now you know, okay, let's say the uh value is 12.
Right?
So, now you know that if uh if there are two values AB and their LCM is equal to 12, so this A and B must be from one of these values, right? 1 or 2 or uh they should be 3 or 4 or 6 or 12, right?
This A and B must be one of these values.
A and B must be one of these values, okay?
Uh but you can see that like not every value will give you LCM is equal to 12, right?
So, like what? Uh like let's say you you take uh this two in pair, okay? You take A is equal to two and B is equal to six.
So then LCM of two and six, yeah, you get 12, which is nice.
But let's say you take LCM of uh two and three.
If you take you you get uh only six.
Right? Which is not equal to 12.
So now, how we are going to solve this thing?
Okay, so uh let's take uh one more example properly.
Like let's say our let's say we want our LCM to be 10.
So for that we know that our value A and B must be one of the one, two, five, and 10, right?
This must be true.
Okay, so now let's try to make every pair and uh see what's their LCM is.
So for this one, two, let's write all the pair, one, five, one, 10, two, one, uh two, two, okay, two, there is one, one also.
Uh two, five, and uh two, 10.
Then there will be five, one, this is one pair, five, two, five, five, and five, 10.
And then the 10, one, 10 and two, 10 and five, 10 and 10. Okay, so now, like for all pairs, let's count the LCM. So for this LCM is one.
For this LCM is two, LCM is five, LCM is 10, LCM is two, LCM is two, 10, 10, five, 10, five, 10, 10, 10, 10.
And so you can see now let's take try to take some observation.
Uh so you can see here that either the LCM is equal to 10 or the LCM is some divisor of 10, right?
So the LCMs are some divisor of 10, right? You can see that 2, 1, 5, everything is a divisor of this.
So now to find to find the number of pairs uh such that the LCM is equal to 10, okay.
You have to do the You have to see all possible pairs.
So how many pairs are possible here? So there are 4 cross 4 16 different pairs, right? Because because like for LCM to be 10, only one of these values should possible.
So for A there are four possible values, 1 2 5 or 10. And for B also there are four possible values, so which are 16.
Let's write down.
So which is like total pairs minus you have to uh cancel the pair which have GCD equal to some divisor of it, equal to some divisor.
Okay, so now point is how we are going to find this thing. Right? Because let's say we want the GCD is equal to sorry, LCM is equal to 10.
I'm sorry if I if I was saying GCD if LCM. Yeah.
So now like we will see first of all all possible pairs which are 16. Then I will cancel the pairs which have the LCM is equal to one.
Then also minus the pair which has LCM is equal to two.
And yeah, then I will get the exact pair which have the LCM is equal to 10, right?
Okay, but now the yeah, the point is how you are going to find it.
And here comes the main part.
That we will uh find iteratively like for all of it.
So we will run iteratively for I from one to 10. Uh sorry, let's say from I from one to let's say X.
We will do uh the our work iteratively.
So What does it mean? So let's say I make my array This is my answer array.
1 2 3 4 5 6 7 right?
So like uh so the each value will give me so the value here will give me the number of pairs which have the LCM is equal to three.
So I will work iteratively. What I will do?
So I will count the number of pairs which have LCM is equal to one. Okay.
Uh let's I count this.
Then I will come at this two.
Right?
So for this two, the divisors are one or two, right?
So all of the possible pairs are like 1 1 1 2 2 1 and 2 2.
This has LCM one. All of the other has LCM is equal to two.
So I will do the total pairs.
Okay, let me write here.
So for this answer is one here.
Because there is only one pair which is this 1 1 which have the LCM is equal to one.
Now, when I come at this two, I have already calculated the number of pair that have the LCM is equal to one.
So, I just have to remove that thing.
That's it.
So, the total possible pairs are two cross two four, minus this value, minus this value.
So, here the answer will be three.
Now, I come at this, let's say this three.
So, for this three, uh its divisors are one and three.
Right?
So, total total pairs are four.
And you will go to its divisor, all of its divisor, and minus that value.
So, here the answer will be three.
Here comes the nice part, okay?
Now, you come at this four.
Uh you come at the four.
So, the you want the LCM is equal to four.
So, its divisors are one, two, and four.
Right?
So, total possible pairs can be what? So, like for A, any value from this three can possible.
So, it will be three, and for B, there are three values possible, so three cross three nine.
Then, you will go at each divisor. You will go at one. You will go at two.
And ask that how many pairs are there such that LCM is equal to one.
You will go at this divisor. You will count that how many uh pairs are there such that the LCM is equal to two.
And you minus all of that stuff. So, you minus one, and you do minus three.
Right? And we get the answer is equal to five.
Yeah, that's it.
So, this is like a iterative version of Mobius and like we have derived this thing, okay?
So, that's it.
Uh So, uh This is our logic.
What we will do for like we will first uh find the what? Find the divisor for each number.
Let's say find the list of divisors for each number.
Then we will work iteratively, okay?
We will work like I from 1 to Z because Z is the maximum uh LCM that we want, right? Like and we will just count total pairs.
And then we will run a loop and we will like cancel the pairs which have less LCM, right? Which have less LCM.
Lesser LCM, that's it.
And then the sum of all of these things are our answer.
So, let's see the code and uh you will understand this thing.
Okay, so let's see the solution.
Like I hope you understand like what I was saying here. I mean, how we uh run out iteratively and find the answer and uh let's say uh in case you have some doubt, you can comment me or you can see the CSES uh question. Like exactly I don't know which one it I will write that in comment.
Okay, so let's see the code.
So, first of all, I am calculating the divisors for each uh uh each number, okay? So, the maximum value is 26 only.
Right?
And now, what I do is uh I'm just counting the divisors. So, in case you don't know what I'm doing here is uh Look at this comment.
What I'm doing here is like I know for this one. So, one is divisor of 1 cross 1, then 1 cross 2, or sorry.
1 cross uh 1 into 2. Then, it is divisor of 1 into 3, it is divisor of 1 into 4, right?
If If we talk about four, the four is the divisor of 4 cross 1, 4 cross 2, 4 cross 3, 4 cross 4, and so on.
So, that's what uh we do.
So, this is the vector of uh vector of div.
So, like div of I will give me the list of all of the divisor. I run from uh 1 to till the maximum.
Uh the current value is I.
So, I know that this current is the divisor of current, and I keep adding the cur.
And I in this value J, in this value J, I push back the cur, which means that current is the divisor of this value J. That's it.
Uh then, I run the loop. Uh I take the N.
Then, this is bad, so I'm counting the number of bad pairs, which means that they have LCM less than equal to Z.
Uh sorry, because I have taken the Z is equal to N here.
Okay, so this is my vector.
So, this is just the array we are talking here, like it will So, area of I will give me the number of pairs which have LCM is equal to I.
So, I run from one till N.
So, the number of total possible pairs are like the number of divisor into number of divisor, okay?
So, like uh in the pair like for A, there are this much option and for B, there are this much option.
Then, I go into the uh I I trade on its divisor and minus if their value, okay? So, let just see here.
Uh let's say the list of the divisor is something like this. So, let's say for the five, the divisor list will be like this.
Uh sorry.
For uh let's say for 10.
So, for 10, our divisor list will be one, two, five, and 10.
So, we have to uh run the loop till here only, okay? We don't have to uh see the 10 here, right?
We just have to go till here. So, I iterate on this thing and I just do the I just cancel the number of pairs which have LCM is equal to one, which is answer of one. Then, I do uh minus of answer of two and that's it.
So, you see here, I'm iterating on this divisors list.
The divis divisor is like div of I {comma} J and in that total pair, in the total pair, I'm subtracting the answer of divisors.
That's it.
So, now the number of pairs which have LCM is equal to I is this pair's term.
So, answer of I is equal to pair and also the pair like this is the initial thing. This I'm adding this into pair.
So, my total answer is going to be the number of possible bad which are the Z cross Z or you can say N cross N minus this bad bad and I'm just printing the answer here. So that's it in the code.
And yeah, the time complexity is going to be over first of all here.
186 log 186 and for here it is going to be N log N because I'm traversing over N and the number of divisor are kind of over log N.
Oh yeah, that's it.
So that was it in this code and in case you have any doubt, you can comment me comment in this video and uh So yeah, that was it till now.
So let's uh over this discussion.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











