There are a few grouping styles that are common in competitive programming to optimise the solution time complexity.
- Grouping by the powers of 2
- Grouping by a fixed bracket size
Out of these two, the second one has a special group size, i.e. make the fixed bracket size equal to the Square Root of input size, N.
Solving techniques like Square Root decomposition, are actually based upon this grouping style. For example, you want to compute some property for a subarray that can be calculated using N² complexity only. But if you could figure-out some special property that allows you to merge two blocks of the same array, you can compute for √N size block then, merge √N blocks using the merging technique.
But this article is actually not about square root and square root decompositions.
The problem, I want to address here is related to streaming input, i.e. some query oriented problem that adds a new element to the existing set of input and asks us some result of a query. Something like that.
Suppose you found out that grouping could help here but what will be the grouping size. If you could use a fixed size like 100, 1000, 2500 etc it's well and good, but in a case, the size has to vary with the number of elements currently in the input. How will you deal with such a scenario?
I have a hint, I am not sure if it will help in your scenario or not but you can try. You can make the current group size =previous group size +1, taking the initial group size = 1. And you add a new group every time you exhaust the current group.
1 + 2 +3 +4+5…… = N
What will be the total number of groups in this case?
Well, this will be answered by using the integer sum formula.
suppose, we have 1 + 2 +3 +4 + 5 …. k = N
then we can say that
( k * (k + 1) )/ 2 = N
and using approximations we can conclude that k almost = √N
This makes the total number of groups = √N
and also the group size = √N
Cool, Right?
This simple technique can be used in so many streaming techniques which can help in making the group size dynamic and easy to handle.
Comments
Post a Comment