following is the code.
import java.util.*;
public class Main {
public static void sort012(int[] arr , int n){
//Your code goes here
int low = 0;
int high = n-1;
int mid = 0;
while(mid<=high){
if(arr[mid]==0){
swap(arr[low] , arr[mid]);
low++;
mid++;
}
else if(arr[mid]==1){
mid++;
}
else{
swap(arr[mid] , arr[high]);
high--;
}
}
}
public static void swap(int x , int y){
x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)
}
public static void printArray(int[] arr) {
for (int element : arr) {
System.out.println(element);
}
System.out.println();
}
public static void main(String args[]) {
// Your Code Here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
sort012(arr , n);
printArray(arr);
}
}