Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

what are bit fields? What is the use of bit fields in a structure declaration?

2198


What are structure members?

1141


What is the use of header files?

1153


Can one function call another?

1171


Tell us the use of fflush() function in c language?

1201


What is && in c programming?

1221


How do I determine whether a character is numeric, alphabetic, and so on?

1182


When is a void pointer used?

1302


Can a function be forced to be inline? Also, give a comparison between inline function and the C macro?

1152


How can I trap or ignore keyboard interrupts like control-c?

1114


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.

2144


For what purpose null pointer used?

1112


How can I do serial ("comm") port I/O?

1215


What is wrong with this code?

1239


Is printf a keyword?

1257