Rearrange positive and negative numbers in an array | faceprep

The problem, rearrange positive and negative numbers in an array can be done in 2 different methods. They are

Method 1: This approach moves all negative numbers to the beginning and positive numbers to the end but changes the order of appearance of the elements of the array.

Method 2:  In this approach, we separate the negative and positive numbers of the array by maintaining the order of appearance of elements of the array.

Method 1 to rearrange positive and negative numbers in an array

In this approach, the order of appearance of the elements of the array is not maintained. Start traversing the array and if a negative element is encountered, swap the negative element and first positive element and continue until all the elements have been traversed.

Algorithm

1. Declare an array and input the array elements.
2. Start traversing the array and if the current element is negative, swap the current element with the first positive element and continue traversing until all the elements have been encountered.
3. Print the rearranged array.

Test case:

• Input: 1 -1 2 2 3 -3
• Output: -1 -2 -3 1 3 2
`#include <stdio.h>#include <stdlib.h>void rearrange_alternate_positions(int arr[], int n){int i,j;j = 0;for(i = 0; i < n; i++){if(arr[i] < 0){if(i != j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}j++;}}}int main(){int n,i;printf("\nEnter the number of elements : ");scanf("%d",&n);int arr[n];printf("\nInput the array elements : ");for(i = 0; i < n; i++){scanf("%d",&arr[i]);}printf("\nOriginal array : ");for(i = 0; i < n; i++){printf("%d ",arr[i]);}printf("\n");rearrange_alternate_positions(arr, n);printf("\nRearranged array : ");for(i = 0; i < n; i++){printf("%d ",arr[i]);}printf("\n");return 0;}`
`#include <iostream>using namespace std;void rearrange_alternate_positions(int arr[], int n){int i,j;j = 0;for(i = 0; i < n; i++){if(arr[i] < 0){if(i != j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}j++;}}}int main(){int n,i;cout << "\nEnter the number of elements : ";cin >> n;int arr[n];cout << "\nInput the array elements : ";for(i = 0; i < n; i++){cin >> arr[i];}cout << "\nOriginal array : ";for(i = 0; i < n; i++){cout << arr[i] << " ";}cout << endl;rearrange_alternate_positions(arr, n);cout << "\nRearranged array : ";for(i = 0; i < n; i++){cout << arr[i] << " ";}cout << endl;return 0;}`

Output:

Method 2 to rearrange positive and negative numbers in an array

In this approach, the order of appearance of the elements of the array is maintained. Start traversing the array and if a positive element is encountered, continue traversing. If a negative element is encountered, shift all the positive numbers to the right by one position and insert the negative number in the sequence array[0 to n-1].

Algorithm

1. Declare an array and input the array elements.
2. Start traversing the array and if the current element is positive, traverse to the next element in the array.
3. If the current element is negative, right shift the positive elements by one position and insert the negative number in the sequence array[0 to n-1]
4. Print the rearranged array.

Test case:

• Input: 1 -1 2 -2 3 -3
• Output: -1 -2 -3 1 2 3
`#include <stdio.h>#include <stdlib.h>void rearrange_alternate_positions(int arr[], int n){int temp, j;for(int i = 1; i < n; i++){temp = arr[i];if (temp > 0)continue;j = i - 1;while ( arr[j] > 0 && j >= 0){arr[j + 1] = arr[j];j--;}arr[j + 1] = temp;}}int main(){int n,i;printf("\nEnter the number of elements : ");scanf("%d",&n);int arr[n];printf("\nInput the array elements : ");for(i = 0; i < n; i++){scanf("%d",&arr[i]);}printf("\nOriginal array : ");for(i = 0; i < n; i++){printf("%d ",arr[i]);}printf("\n");rearrange_alternate_positions(arr, n);printf("\nRearranged array : ");for(i = 0; i < n; i++){printf("%d ",arr[i]);}printf("\n");return 0;}`
`#include <iostream>using namespace std;void rearrange_alternate_positions(int arr[], int n){int temp, j;for(int i = 1; i < n; i++){temp = arr[i];if (temp > 0)continue;j = i - 1;while ( arr[j] > 0 && j >= 0){arr[j + 1] = arr[j];j--;}arr[j + 1] = temp;}}int main(){int n,i;cout << "\nEnter the number of elements : ";cin >> n;int arr[n];cout << "\nInput the array elements : ";for(i = 0; i < n; i++){cin >> arr[i];}cout << "\nOriginal array : ";for(i = 0; i < n; i++){cout << arr[i] << " ";}cout << endl;rearrange_alternate_positions(arr, n);cout << "\nRearranged array : ";for(i = 0; i < n; i++){cout << arr[i] << " ";}cout << endl;return 0;}`

Output: