Last active
April 10, 2020 08:03
-
-
Save bangarharshit/7224bc579628dfacaebd4b27d5ce33f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given an array with n objects colored red, white or blue, | |
// sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. | |
// Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. | |
// Note: Using library sort function is not allowed. | |
// Example : | |
// Input : [0 1 2 0 1 2] | |
// Modify array so that it becomes : [0 0 1 1 2 2] | |
public class Solution { | |
int[] count = new int[3]; | |
public void sortColors(ArrayList<Integer> a) { | |
for(int i: a) { | |
count[i]++; | |
} | |
a.clear(); | |
for (int i=0; i<=2; i++) { | |
int count = 0; | |
int noOfEntity = count[i]; | |
while (count < noOfEntity) { | |
a.add(i); | |
count++; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment