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 high level languages like C and FORTRAN also known as?

1176


What is New modifiers?

1130


What is declaration and definition in c?

1098


Who invented bcpl language?

1198


What is pointers in c with example?

1078


What are volatile variables in c?

948


Can static variables be declared in a header file?

1062


int main() { Int n=20,i; For(i=0;i<=n;i--) { Printf(“-“); Return 0;

1832


Explain what is #line used for?

1072


What does sizeof function do?

1197


Why isn't it being handled properly?

1067


What does the && operator do in a program code?

1236


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

1163


What are pointers really good for, anyway?

1066


what is use of malloc and calloc?

1904