C Exercises: Segregate 0s and 1s in an array
95. Segregate 0s and 1s in Array
Write a program in C to segregate 0s and 1s in an array.
Expected Output:
The given array is:
1 0 1 0 0 1 0 1 1<>br>
The array after segregation is: 0 0 0 0 1 1 1 1 1
The task is to write a C program that segregates 0s and 1s in a given array such that all 0s are moved to the beginning and all 1s to the end of the array. The program should traverse the array, rearrange the elements accordingly, and then display the modified array with all 0s followed by all 1s.
Sample Solution:
C Code:
Output:
The given array is: 1 0 1 0 0 1 0 1 1 The array after segregation is: 0 0 0 0 1 1 1 1 1
Visual Presentation:
Flowchart:/p>
For more Practice: Solve these Related Problems:
- Write a C program to segregate 0s and 1s in an array using two-pointer technique without extra space.
- Write a C program to count the number of 0s and 1s and then reconstruct the sorted array.
- Write a C program to segregate 0s and 1s and then verify by printing the frequency of each.
- Write a C program to segregate binary elements and then swap the first and last halves of the array.
C Programming Code Editor:
Previous: Write a program in C to find the maximum for each and every contigious subarray of size k from a given array.
Next: Write a program in C to segregate even and odd elements on an array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.