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
What is infinite loop?
What is a program flowchart and how does it help in writing a program?
Describe newline escape sequence with a sample program?
Explain what is the benefit of using #define to declare a constant?
What is the use of parallelize in spark?
Explain the red-black trees?
Difference between constant pointer and pointer to a constant.
Explain how can I convert a string to a number?
What is a built-in function in C?
How to write a code for implementing my own printf() and
scanf().... Please hep me in this... I need a guidance...
Can you give an coding for c... Please also explain about
the header files used other than #include
Write a program to print fibonacci series using recursion?
What is the use of f in c?
write a program to create a sparse matrix using dynamic memory allocation.
How do I use strcmp?
What is queue in c?