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
When c language was developed?
Which is better pointer or array?
Explain how do you print only part of a string?
What is string constants?
Give basis knowledge of web designing ...
Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.]
a sequence of bytes with one to one corrspondence to those in the external device a) sequential addressing b) address c) byte code d) none
Which type of language is c?
What is the benefit of using const for declaring constants?
how can use subset in c program and give more example
I need previous papers of CSC.......plz help out by posting them.......
What is static volatile in c?
What is typeof in c?
Is fortran still used today?
What are the types of unary operators?