# Positive elements at even and negative at odd positions (Relative order not maintained) in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array and our task is to convert the array in such a way that all positive numbers are at even index places and all negative numbers are at odd index places.

There might be an unequal number of positive and negative values, in this case, we will not move extra values.

Let’s take an example to understand the problem,

Input − {3, 5, -1, 19, -7, -2}

Output − {3, -1, 5, -7, 19, -2}

To solve this problem, we will have to find the element which is out of order in the array. There can be more that one way to find this, here we will discuss two of them.

## Method 1

This method will simply traverse the array and find the first occurrence of elements that are not in the place (i.e. positive not at even and negative not at odd) and then swap them. We will do this process until the whole array is traversed.

### Example

Program to show the implementation of our solution,

Live Demo

#include<iostream>
using namespace std;
void swapElements(int* a, int i , int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
return ;
}
void printArray(int* a, int n){
for(int i = 0; i<n; i++)
cout<<a[i]<<"\t";
cout<<endl;
return ;
}
void generateOrderedArray(int arr[], int n){
for(int i = 0; i <n; i++){
if(arr[i] >= 0 && i % 2 == 1){
for(int j = i + 1; j <n; j++){
if(arr[j] < 0 && j % 2 == 0){
swapElements(arr, i, j);
break ;
}
}
}
else if(arr[i] < 0 && i % 2 == 0){
for(int j = i + 1; j <n; j++){
if(arr[j] >= 0 && j % 2 == 1){
swapElements(arr, i, j);
break;
}
}
}
}
printArray(arr, n);
}
int main(){
int arr[] = { 1, -3, 5, 6, -3, 6, 7, -4, 9, 10 };
int n = sizeof(arr)/sizeof(arr);
cout<<"Inital Array is : ";
printArray(arr, n);
cout<<"Array with positive numbers at even index and negative numbers at odd index :";
generateOrderedArray(arr,n);
return 0;
}

### Output

Inital Array is : 3 5 -1 19 -7 -2
Array with positive numbers at even index and negative numbers at odd index : 3 -1 5 -7 19 -2

## Method 2

In this method, we will use a process that will look like a quick sort of technique. Here, we will take two pointers, one for positive and other for negative numbers. We will set the positive pointer at index 0 (even index) and negative at index 1 (odd index). And step forward the pointer by 2. And stop when positive pointer encounters negative numbers and negative pointer encounter positive numbers. And then swap when both stop. If any of the pointers go out of array index we will stop the execution.

### Example

Program to show the implementation of our solution

Live Demo

#include <iostream>
using namespace std;
void swapElements(int* a, int i , int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
return ;
}
void printArray(int *a, int n){
for (int i = 0; i <n; i++)
cout<<a[i]<<"\t";
cout<<endl;
}
void generateOrderedArray(int a[], int size){
int positive = 0, negative = 1;
while (1) {
while (positive < size && a[positive] >= 0)
positive += 2;
while (negative <size && a[negative] <= 0)
negative += 2;
if (positive < size && negative < size)
swapElements(a, positive, negative);
else
break;
}
}
int main(){
int arr[] = { 3, 5, -1, 19, -7, -2 };
int n = (sizeof(arr) / sizeof(arr));
cout<<"Inital Array is : ";
printArray(arr, n);
cout<<"Array with positive numbers at even index and negative numbers at odd index : ";
generateOrderedArray(arr, n);
printArray(arr, n);
return 0;
}

### Output

Inital Array is : 3 5 -1 19 -7 -2
Array with positive numbers at even index and negative numbers at odd index : 3 -1 5 -7 19 -2