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 getche() function?
How can I implement sets or arrays of bits?
What is the difference between call by value and call by reference in c?
Explain union. What are its advantages?
What is the function of volatile in c language?
Why is c called a mid-level programming language?
What are the two forms of #include directive?
What is data types?
main() { int i = 10; printf(" %d %d %d ", ++i, i++, ++i); }
What are the difference between a free-standing and a hosted environment?
In c programming write a program that will print 10 multiples of 3 except 15,18,21 using looping
write a program to convert a expression in polish notation(postfix) to inline(normal) something like make 723+* (2+3) x 7 (not sure) just check out its mainly printing expression in postfix form to infix.
Why are all header files not declared in every c program?
Hai what is the different types of versions and their differences
What is the use of header?