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 was c language developed?
Does c have enums?
Explain how do I determine whether a character is numeric, alphabetic, and so on?
Explain how many levels deep can include files be nested?
how to solve "unable to open stdio.h and conio.h header files in windows 7 by using Dos-box software
What is difference between constant pointer and constant variable?
How important is structure in life?
What is c++ used for today?
7-Given an index k, return the kth row of the Pascal's triangle. For example, when k = 3, the row is [1,3,3,1]. For reference look at the following standard pascal’s triangle.
Explain a file operation in C with an example.
What are different types of operators?
How do we print only part of a string in c?
What is the purpose of & in scanf?
What is masking?
Who invented bcpl language?