Monday, May 14, 2012

DutchNationalFlagProblem


Problem :

Given an array consisting of 0's 1's 2's ( Red, Blue and Green ). Come up with an approach where in array is rearranged such that, all 0's appear first followed by 1's and then 2's.

Solution :

This is equivalent to the partitioning having pivot element as 1. All elements less than 1( which is 0) will appear on the left hand side and all elements >1 are appearing on the right hand side. ( 2)

Here is the attached code for the same.



No comments:

Post a Comment