Skip to content

Instantly share code, notes, and snippets.

@bangarharshit
Last active April 10, 2020 08:03
Show Gist options
  • Save bangarharshit/7224bc579628dfacaebd4b27d5ce33f3 to your computer and use it in GitHub Desktop.
Save bangarharshit/7224bc579628dfacaebd4b27d5ce33f3 to your computer and use it in GitHub Desktop.
// 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