C Exercises: Rearrange positive and negative numbers alternatively in a given array
93. Rearrange Positive & Negative Numbers Alternately
Write a program in C to rearrange positive and negative numbers alternatively in a given array.
N.B.: If positive numbers are more they appear at the end and for also negative numbers, they too appear in the end of the array.
Expected Output:
The given array is:
-4 8 -5 -6 5 -9 7 1 -21 -11 19
The rearranged array is:
-4 7 -5 1 -21 5 -11 8 -9 19 -6
The task is to write a C program that rearranges the elements of a given array such that positive and negative numbers alternate. If there are more positive or negative numbers, the extra numbers will appear at the end of the array. The program should iterate through the array to reorganize the elements accordingly and then display the rearranged array.
Sample Solution:
C Code:
Output:
The given array is: -4 8 -5 -6 5 -9 7 1 -21 -11 19 The rearranged array is: -4 7 -5 1 -21 5 -11 8 -9 19 -6
Flowchart:/p>
For more Practice: Solve these Related Problems:
- Write a C program to rearrange an array so that positive and negative numbers alternate, maintaining their original order.
- Write a C program to alternate positive and negative numbers and then append extra numbers at the end.
- Write a C program to rearrange an array with separate partitions for positive and negative numbers using two-pointer technique.
- Write a C program to recursively rearrange an array so that positives and negatives alternate.
C Programming Code Editor:
Previous: Write a program in C that checks whether the elements in an unsorted array appears consecutively or not.
Next: Write a program in C to find the maximum for each and every contigious subarray of size k from a given array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.