@Ashot: You can traverse array one time. In your case  you are traversing
twice. First for counting occurrence of R , G and B then again traversing
for assigning values. But constraints is that you have to traverse only
once.

On Sun, Jun 10, 2012 at 5:13 PM, Akshat Sapra <[email protected]> wrote:

> int low,mid,high;
>
> low = mid = 0;
>
> high = (int)(sizeof(arr)/sizeof(arr[0]))-1;
>
> /*************************************************************************
> According to Dutch national flag problem there are three types of
> quantities in an array and we have to combine these elements together
> but in a certain order
> Let the elements in the array are in the form 0,1,2 then these elements in
> an array should appear in order 0 then 1 then 2
>
> ( low to middle-1 ) - elements having 0 as a number
> (middle to high-1 ) - 1 as a number
> ( high to arr - size) - 2 as a number
>
> ****************************************************************************/
>
> n = high;
>
> for ( int i = 0; i <= n; i++  ) {
>           if ( arr[mid] == 0 ) {
>              swap(arr[low],arr[mid]);
>              low++;
>              mid++;
>           }
>           else if ( arr[mid] == 2 ) {
>                 swap(arr[mid],arr[high]);
>                 high--;
>            }
>            else {
>                   mid++;
>            }
> }
>
> --
> 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?hl=en.
>

-- 
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?hl=en.

Reply via email to