Skip to content

Instantly share code, notes, and snippets.

@thmain
Created August 28, 2017 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/803bc172d7b913d3e8689a42288dd4ca to your computer and use it in GitHub Desktop.
Save thmain/803bc172d7b913d3e8689a42288dd4ca to your computer and use it in GitHub Desktop.
import java.util.Arrays;
public class Separate0and1Counting {
public static int[] arrange(int [] arrA){
//count number of 0's
int countOs=0;
int size = arrA.length;
for (int i = 0; i <arrA.length ; i++) {
if(arrA[i]==0)
countOs++;
}
for (int i = 0; i <arrA.length ; i++) {
if(countOs>0) {
arrA[i] = 0;
countOs--;
}
else
arrA[i]=1;
}
return arrA;
}
public static void main(String[] args) {
int [] arrA = {1,0,1,0,1,1,0,0,0,0,1};
System.out.println("Rearranging arrays using counting..");
arrA = arrange(arrA);
System.out.println(Arrays.toString(arrA));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment