You have been given an integer array/list(ARR) of size N that contains only integers, 0 and 1. Write a function to sort this array/list. Think of a solution which scans the array/list only once and don't require use of an extra array/list.
Sample Input 1:
7
0 1 1 0 1 0 1
Sample Output 1:
0 0 0 1 1 1 1
public class Solution {
public static void sortZeroesAndOne(int[] arr) {
int l=arr.length;
int c=0;
for(int i=0;i<l;i++)
{
if(arr[i]==0)
{
c++;
}
}
for(int i=0;i<c;i++)
{
arr[i]=0;
}
for(int j=c;j<l;j++)
{
arr[j]=1;
}
}
}
Comments
Post a Comment