Welcome back.
In the previous video, we have discussed the approach that we can take to solve this question in detail.
Over here.
Let's quickly write the pseudocode for the approach that we came up with.
And then we will go ahead and implement the code of this as well.
So as we discussed in the previous video, the first thing that we need to do to solve this question
is to traverse through the tasks which are given to us and identify the frequency for each task in the
input tasks array.
Okay.
And after we have identified this, there are just 4 or 5 steps to solve this question.
First, we'll go ahead and try to identify the number of parts that would be there.
Once we have organized or arranged the task that occurs at the highest frequency, ensuring that these
tasks are separated by n intervals.
And again, remember n is one of the inputs in the question.
Okay.
So this would be the first thing that we do after we have identified the frequencies of the tasks which
are given to us.
And if we say that this is equal to P, we have seen that we can identify p or the number of parts by
just doing the highest frequency minus one.
So again this means that let's say the input array has these elements okay.
So in this case we can see that the highest frequency is three.
And there would be in this case two parts which is three minus one.
And the reason for this is we would arrange C over here.
Then we'll just have n intervals and then again c and n intervals.
And then again c.
And notice because we had three c's we are left with two parts.
So parts or p is equal to the highest frequency minus one.
Now once we have done this we can go ahead and identify the number of slots in the parts.
And for that we need to multiply the parts with the number of slots in each part, which would be n
minus the max count, minus one, where max count is the number of tasks that are there that have the
highest frequency.
Okay.
Now again, this means that let's say the input tasks array is something like this.
Now in this case max count would be two because we have C and A which each occur three times which is
the highest frequency okay.
So again the slots in parts would be p into n minus max count minus one.
And after this we can go ahead and identify the remaining tasks which we would have to fill in these
slots.
Or at least try to fill in these slots.
And to identify this, we can just do the length of tasks minus the highest frequency into max count,
because we would have arranged already the tasks which have the highest frequency.
For example, in this example we would have already arranged C and a, and we would just be left with
tasks which don't have this frequency.
Like for example if the input array were something like this.
So notice over here we have b, c and d which don't have a frequency of three which is the highest frequency
in this example.
And therefore we just need to identify this four and for that we can just subtract from the length of
tasks which would be the total number of tasks.
The number of times the task with the highest frequency occurs, which in this case is six.
And to get six over here we can just multiply the highest frequency, which is three by the max count
which is two.
Okay.
So that gives us t which is the remaining number of tasks which we will try to place in the slots.
Okay.
These slots over here.
And once we have identified this we can identify the number of empty slots which would be s minus t.
Okay.
Now there is a caveat to this.
Now if s minus t is less than zero then we will just take zero okay.
Now this is possible.
Like I've explained earlier as well.
Let's say the input array is a, a, b, b, c, d and let's say n is equal to one.
Okay.
Now in this case we would place a over here, leave one space and then place a.
So we have placed these two tasks.
And then we would go ahead and place these two tasks as well.
Because they have the same frequency which is the highest frequency which is equal to two.
So we would go ahead and place a b over here and B over here.
Now notice that in this case s is equal to zero because we don't have anything over here.
And t is equal to two right.
And zero minus two gives us minus two.
But then in that case we would take e as just zero because we can just place C and D over here.
And with this arrangement notice that there are in fact no idle or empty slots.
That's why we would just take.
Slots or idols as zero in this case.
Okay, so that's it.
Now once we have identified the number of idols or empty slots, we can just return the answer which
would be E plus the length of tasks.
So this is the pseudo code for solving this question.