My code is not passing test cases but it gives the output of custom input

I don’t think your logic is quite correct. Well, the logic is quite simple indeed. Just count the number of zeros. ones, and twos, in the array, now, iterate from i = 0 and first place all zeros, then all ones and then all twos.