Hello Everyone,
You have been given an array and you have to make a program to convert that array such that positive elements occur at even numbered places in the array and negative elements occur at odd numbered places in the array. We have to do it in place.
There can be unequal number of positive and negative values and the extra values have to left as it is.
Examples:
Input : arr[] = {1, -3, 5, 6, -3, 6, 7, -4, 9, 10}
Output : arr[] = {1, -3, 5, -3, 6, 6, 7, -4, 9, 10}
Input : arr[] = {-1, 3, -5, 6, 3, 6, -7, -4, -9, 10}
Output : arr[] = {3, -1, 6, -5, 3, -7, 6, -4, 10, -9}
We take two pointers positive and negative. We set the positive pointer at start of the array and the negative pointer at 1st position of the array.
We move positive pointer two steps forward till it finds a negative element. Similarly, we move negative pointer forward by two places till it finds a positive value at its position.
If the positive and negative pointers are in the array then we will swap the values at these indexes otherwise we will stop executing the process.
// C++ program to rearrange positive and negative
// numbers
#include <bits/stdc++.h>
using
namespace
std;
void
rearrange(
int
a[],
int
size)
{
int
positive = 0, negative = 1;
while
(
true
) {
/* Move forward the positive pointer till
negative number number not encountered */
while
(positive < size && a[positive] >= 0)
positive += 2;
/* Move forward the negative pointer till
positive number number not encountered */
while
(negative < size && a[negative] <= 0)
negative += 2;
// Swap array elements to fix their position.
if
(positive < size && negative < size)
swap(a[positive], a[negative]);
/* Break from the while loop when any index
exceeds the size of the array */
else
break
;
}
}
// Driver code
int
main()
{
int
arr[] = { 1, -3, 5, 6, -3, 6, 7, -4, 9, 10 };
int
n = (
sizeof
(arr) /
sizeof
(arr[0]));
rearrange(arr, n);
for
(
int
i = 0; i < n; i++)
cout << arr[i] <<
" "
;
return
0;
}
Output:
1 -3 5 -3 6 6 7 -4 9 10
Lets explain the working of the code on the first example
arr[] = {1, -3, 5, 6, -3, 6, 7, -4, 9, 10}
We declare two variables positive and negative positive points to zeroth position and negative points to first position
positive = 0 negative = 1
In the first iteration positive will move 4 places to fifth position as a[4] is less than zero and positive = 4.
Negative will move 2 places and will point to fourth position as a[3]>0
we will swap positive and negative position values as they are less than size of array.
After first iteration the array becomes arr[] = {1, -3, 5, -3, 6, 6, 7, -4, 9, 10}
Now positive points at fourth position and negative points at third position
In second iteration the positive value will move 6 places and its value will
more than the size of the array.
The negative pointer will move two steps forward and it will point to 5th position
As the positive pointer value becomes greater than the array size we will not perform any swap operation and break out of the while loop.
The final output will be
arr[] = {1, -3, 5, -3, 6, 6, 7, -4, 9, 10}