Created
August 28, 2017 03:14
-
-
Save thmain/803bc172d7b913d3e8689a42288dd4ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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