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 high level languages like C and FORTRAN also known as?
What is New modifiers?
What is declaration and definition in c?
Who invented bcpl language?
What is pointers in c with example?
What are volatile variables in c?
Can static variables be declared in a header file?
int main() { Int n=20,i; For(i=0;i<=n;i--) { Printf(“-“); Return 0;
Explain what is #line used for?
What does sizeof function do?
Why isn't it being handled properly?
What does the && operator do in a program code?
we called a function and passed something do it we have always passed the "values" of variables to the called function. such functions calles are called a) calls by reference b) calls by value c) calls by zero d) none of the above
What are pointers really good for, anyway?
what is use of malloc and calloc?