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 / hemavathi
in java this works jus fine:
public static void singlePass(int[] arr){
System.out.println("Orignal Array : " +
Arrays.toString(arr));
int first1index = -1;
for(int i=0; i<arr.length; i++) {
if(arr [i] == 1 && first1index == -1) {
first1index = i;
}
else if(arr [i] == 0 && first1index != -1) {
arr[i] = 1; arr[first1index] = 0;
first1index++;
}
}
System.out.println("Modified Array : " +
Arrays.toString(arr));
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
what are bit fields? What is the use of bit fields in a structure declaration?
What are structure members?
What is the use of header files?
Can one function call another?
Tell us the use of fflush() function in c language?
What is && in c programming?
How do I determine whether a character is numeric, alphabetic, and so on?
When is a void pointer used?
Can a function be forced to be inline? Also, give a comparison between inline function and the C macro?
How can I trap or ignore keyboard interrupts like control-c?
Write a programme using structure that create a record of students. The user allow to add a record and delete a record and also show the records in ascending order.
For what purpose null pointer used?
How can I do serial ("comm") port I/O?
What is wrong with this code?
Is printf a keyword?