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


Please Help Members By Posting Answers For Below Questions

Explain function?

659


When can a far pointer be used?

584


hi... can anyone help me to make a two-dimensinal arrays in finding the sum of two elements plzzz. thnx a lot...

1435


What are the uses of null pointers?

588


Is it better to use a macro or a function?

649






Explain what is the benefit of using enum to declare a constant?

583


What is the hardest programming language?

661


When should a far pointer be used?

600


How can I display a percentage-done indication that updates itself in place, or show one of those twirling baton progress indicators?

573


Are global variables static in c?

671


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.

655


What is a struct c#?

599


Can a function argument have default value?

668


What do you mean by recursion in c?

621


How do I determine whether a character is numeric, alphabetic, and so on?

619