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
How to set file pointer to beginning c?
What does the characters “r” and “w” mean when writing programs that will make use of files?
What are loops c?
Write a C program to help a HiFi’s Restaurant automate its breakfast billing system. Your assignment should implement the following items: a. Show the customer the different breakfast items offered by the HiFi’s Restaurant. b. Allow the customer to select more than one item from the menu. c. Calculate and print the bill to the customer. d. Produce a report to present your complete program and show more sample output. Assume that the HiFi’s Restaurant offers the following breakfast menu: Plain Egg $2.50 Bacon and Egg $3.45 Muffin $2.20 French Toast $2.95 Fruit Basket $3.45 Cereal $0.70 Coffee $1.50 Tea $1.80
What is pointer to pointer in c?
Difference between exit() and _exit() function?
Is c pass by value or reference?
hi to every one .. how to view table pool after creating the pooled table? plz help me.. if any knows abt this ..
Explain what is a program flowchart and explain how does it help in writing a program?
Can you write the algorithm for Queue?
What are the advantages and disadvantages of c language?
Write the test cases for checking a variable having value in range -10.0 to +10.0?
Why is void main used?
What is a sequential access file?
What is array of structure in c programming?