array contains zeros and ones as elements.we need to bring
zeros one side and one other side in single parse.
ex:a[]={0,0,1,0,1,1,0,0}
o/p={0,0,0,0,0,1,1,1}
Answer Posted / anon
import java.util.Arrays;
public class Exps {
public static void array_0s_1_seprator(int[] arr){
System.out.println("Orignal Array : " + Arrays.toString(arr));
for(int i = 0, j =arr.length ; i< j ;++i ){
if(arr[i]==0) continue;
while(arr[--j]==1 && i<j)
continue;
if(i< j){
arr[i] = 0;
arr[j] = 1;
}
System.out.println("Modified Array : " + Arrays.toString(arr));
}
}
public static void main(String[] args) {
int arr[] = new int[15];
for(int i =0; i<arr.length;++i)
arr[i] = (int)(Math.random()*10) <5 ? 0 : 1;
array_0s_1_seprator(arr);
}
}
Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
Explain function?
When can a far pointer be used?
hi... can anyone help me to make a two-dimensinal arrays in finding the sum of two elements plzzz. thnx a lot...
What are the uses of null pointers?
Is it better to use a macro or a function?
Explain what is the benefit of using enum to declare a constant?
What is the hardest programming language?
When should a far pointer be used?
How can I display a percentage-done indication that updates itself in place, or show one of those twirling baton progress indicators?
Are global variables static in c?
Write an efficient algo and C code to shuffle a pack of cards.. this one was a feedback process until we came up with one with no extra storage.
What is a struct c#?
Can a function argument have default value?
What do you mean by recursion in c?
How do I determine whether a character is numeric, alphabetic, and so on?