If you have just two colors then this is possible in O(N). But since the
problem says K colors, this would effectively be sorting of K numbers.
If K is limited (<N) then we could use counting sort and such algo's to do
it in near O(N) (with O(K) storage as well). If K can be unlimited and the
input size can be HUGE then we rather resort to one of the in-place sort in
O(N log N) complexity.

On Sun, Sep 6, 2009 at 1:06 AM, ankur aggarwal <[email protected]>wrote:

> You have N balls having one of K colors. Arrange them into groups of same
> colors. e.g.
>
> RRRRRRGRG
> can be arranged as
> RRRRRRRGG (Answer)
> GGRRRRRRR
> >
>


-- 
Yesterday is History.
Tomorrow is a Mystery.
Today is a Gift! That is why it is called the Present :).

http://sites.google.com/site/ramaswamyr

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to