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
Tell me what are bitwise shift operators?
Explain what are the different file extensions involved when programming in c?
write a program that declares an array of 30 elements named "income" in the main functions. then cal and pass the array to a programmer-defined function named "getIncome" within the "getIncome" function, ask the user for annual income of 30 employees. then calculate and print total income on the screen using the following function: "void getIncome ( ai []);
why do some people write if(0 == x) instead of if(x == 0)?
Tell me can the size of an array be declared at runtime?
Can 'this' pointer by used in the constructor?
What is the use of pragma in embedded c?
What is the difference between new and malloc functions?
What are the different types of endless loops?
What is the best organizational structure?
What is %lu in c?
What is far pointer in c?
Explain what is the benefit of using enum to declare a constant?
Why is python slower than c?
What is the purpose of void pointer?