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
Why is c still so popular?
Explain the difference between malloc() and calloc() function?
How can a number be converted to a string?
how to introdu5ce my self in serco
Explain Function Pointer?
What does %p mean?
What are the 5 types of inheritance in c ++?
Can we use any name in place of argv and argc as command line arguments?
Difference between goto, long jmp() and setjmp()?
Is return a keyword in c?
What is the heap in c?
Why c is faster than c++?
State two uses of pointers in C?
Tell us something about keyword 'auto'.
What are the back slash character constants or escape sequence charactersavailable in c?