# Rearrange positive and negative numbers in array

Here given code implementation process.

``````// C Program
// Rearrange positive and negative numbers in array
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("  %d", arr[i]);
}
printf("\n");
}
// Swapping of array elements
void swapNode(int arr[], int a, int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
// Arrange pairwise positive and negative elements
void rearrange(int arr[], int size)
{
int negative = 1;
int positive = 0;
// Loop controlling variable
int i = 1;
// Set positive elements in Even position in given arrays
while (i < size && positive < size)
{
if (arr[positive] > 0)
{
positive += 2;
}
else if (arr[i] >= 0)
{
if (i < positive)
{
if (i % 2 == 1)
{
// When if swap the positive elements at odd position element
swapNode(arr, i, positive);
positive += 2;
}
}
else
{
swapNode(arr, i, positive);
positive += 2;
}
}
i++;
}
i = 0;
// Set negative elements in Odd position in given arrays
while (i < size && negative < size)
{
if (arr[negative] < 0)
{
// When element already negative number
negative += 2;
}
else if (arr[i] < 0)
{
if (i < negative)
{
if (i % 2 == 0)
{
swapNode(arr, i, negative);
negative += 2;
}
}
else
{
swapNode(arr, i, negative);
negative += 2;
}
}
i++;
}
}
int main(int argc, char
const *argv[])
{
// Define array of integer elements
int arr1[] = {
-1 , -2 , -5 , 1 , -4 , 2 , 5 , 2 , 4 , -4
};
int arr2[] = {
8 , 7 , 0 , -5 , 1 , -4 , -9 , 5 , 2 , 4 , -4 , -6
};
int arr3[] = {
8 , 6 , 2 , 6 , -2 , 5 , 2 , -6 , 2 , -6 , 3
};
int arr4[] = {
1 , 2 , 3 , 4
};
int arr5[] = {
-1 , -2 , -3 , -4 , -5 , 1 , 2 , 3 , 4
};
// Test 1
int size = sizeof(arr1) / sizeof(arr1[0]);
printf("  Before Arrange \n");
printArray(arr1, size);
rearrange(arr1, size);
printf("  After Arrange \n");
printArray(arr1, size);
// Test 2
size = sizeof(arr2) / sizeof(arr2[0]);
printf("\n  Before Arrange \n");
printArray(arr2, size);
rearrange(arr2, size);
printf("  After Arrange \n");
printArray(arr2, size);
// Test 3
size = sizeof(arr3) / sizeof(arr3[0]);
printf("  Before Arrange \n");
printArray(arr3, size);
rearrange(arr3, size);
printf("  After Arrange \n");
printArray(arr3, size);
// Test 4
size = sizeof(arr4) / sizeof(arr4[0]);
printf("\n  Before Arrange \n");
printArray(arr4, size);
rearrange(arr4, size);
printf("  After Arrange \n");
printArray(arr4, size);
// Test 5
size = sizeof(arr5) / sizeof(arr5[0]);
printf("\n  Before Arrange \n");
printArray(arr5, size);
rearrange(arr5, size);
printf("  After Arrange \n");
printArray(arr5, size);
return 0;
}``````

#### Output

``````  Before Arrange
-1  -2  -5  1  -4  2  5  2  4  -4
After Arrange
1  -2  2  -1  5  -5  2  -4  4  -4

Before Arrange
8  7  0  -5  1  -4  -9  5  2  4  -4  -6
After Arrange
8  -5  0  -4  1  -9  5  -4  2  -6  4  7
Before Arrange
8  6  2  6  -2  5  2  -6  2  -6  3
After Arrange
8  -2  2  -6  6  -6  2  6  2  5  3

Before Arrange
1  2  3  4
After Arrange
1  2  3  4

Before Arrange
-1  -2  -3  -4  -5  1  2  3  4
After Arrange
1  -2  2  -4  3  -1  4  -5  -3``````
``````/*
Java Program
Rearrange positive and negative numbers in array
*/
public class Arrangement
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i] );
}
System.out.print("\n");
}
// Swapping of array elements
public void swapNode(int[] arr, int a, int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
// Arrange pairwise positive and negative elements
public void rearrange(int[] arr, int size)
{
int negative = 1;
int positive = 0;
// Loop controlling variable
int i = 1;
// Set positive elements in Even position in given arrays
while (i < size && positive < size)
{
if (arr[positive] > 0)
{
// When element already negative positive
positive += 2;
}
else if (arr[i] >= 0)
{
if (i < positive)
{
if (i % 2 == 1)
{
// When if swap the positive elements at odd position element
swapNode(arr, i, positive);
positive += 2;
}
}
else
{
swapNode(arr, i, positive);
positive += 2;
}
}
i++;
}
i = 0;
// Set negative elements in Odd position in given arrays
while (i < size && negative < size)
{
if (arr[negative] < 0)
{
negative += 2;
}
else if (arr[i] < 0)
{
if (i < negative)
{
if (i % 2 == 0)
{
swapNode(arr, i, negative);
negative += 2;
}
}
else
{
swapNode(arr, i, negative);
negative += 2;
}
}
i++;
}
}
public static void main(String args[])
{
// Define array of integer elements
int[] arr1 = {
-1 , -2 , -5 , 1 , -4 , 2 , 5 , 2 , 4 , -4
};
int[] arr2 = {
8 , 7 , 0 , -5 , 1 , -4 , -9 , 5 , 2 , 4 , -4 , -6
};
int[] arr3 = {
8 , 6 , 2 , 6 , -2 , 5 , 2 , -6 , 2 , -6 , 3
};
int[] arr4 = {
1 , 2 , 3 , 4
};
int[] arr5 = {
-1 , -2 , -3 , -4 , -5 , 1 , 2 , 3 , 4
};
// Test 1
int size = arr1.length;
System.out.print(" Before Arrange \n");
System.out.print(" After Arrange \n");
// Test 2
size = arr2.length;
System.out.print("\n Before Arrange \n");
System.out.print(" After Arrange \n");
// Test 3
size = arr3.length;
System.out.print(" Before Arrange \n");
System.out.print(" After Arrange \n");
// Test 4
size = arr4.length;
System.out.print("\n Before Arrange \n");
System.out.print(" After Arrange \n");
// Test 5
size = arr5.length;
System.out.print("\n Before Arrange \n");
System.out.print(" After Arrange \n");
}
}``````

#### Output

`````` Before Arrange
-1  -2  -5  1  -4  2  5  2  4  -4
After Arrange
1  -2  2  -1  5  -5  2  -4  4  -4

Before Arrange
8  7  0  -5  1  -4  -9  5  2  4  -4  -6
After Arrange
8  -5  0  -4  1  -9  5  -4  2  -6  4  7
Before Arrange
8  6  2  6  -2  5  2  -6  2  -6  3
After Arrange
8  -2  2  -6  6  -6  2  6  2  5  3

Before Arrange
1  2  3  4
After Arrange
1  2  3  4

Before Arrange
-1  -2  -3  -4  -5  1  2  3  4
After Arrange
1  -2  2  -4  3  -1  4  -5  -3``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program
Rearrange positive and negative numbers in array
*/
class Arrangement
{
public:
//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i];
}
cout << "\n";
}
// Swapping of array elements
void swapNode(int arr[], int a, int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
// Arrange pairwise positive and negative elements
void rearrange(int arr[], int size)
{
int negative = 1;
int positive = 0;
// Loop controlling variable
int i = 1;
// Set positive elements in Even position in given arrays
while (i < size && positive < size)
{
if (arr[positive] > 0)
{
positive += 2;
}
else if (arr[i] >= 0)
{
if (i < positive)
{
if (i % 2 == 1)
{
// When if swap the positive elements at odd position element
this->swapNode(arr, i, positive);
positive += 2;
}
}
else
{
this->swapNode(arr, i, positive);
positive += 2;
}
}
i++;
}
i = 0;
// Set negative elements in Odd position in given arrays
while (i < size && negative < size)
{
if (arr[negative] < 0)
{
// When element already negative number
negative += 2;
}
else if (arr[i] < 0)
{
if (i < negative)
{
if (i % 2 == 0)
{
this->swapNode(arr, i, negative);
negative += 2;
}
}
else
{
this->swapNode(arr, i, negative);
negative += 2;
}
}
i++;
}
}
};
int main()
{
// Define array of integer elements
int arr1[] = {
-1 , -2 , -5 , 1 , -4 , 2 , 5 , 2 , 4 , -4
};
int arr2[] = {
8 , 7 , 0 , -5 , 1 , -4 , -9 , 5 , 2 , 4 , -4 , -6
};
int arr3[] = {
8 , 6 , 2 , 6 , -2 , 5 , 2 , -6 , 2 , -6 , 3
};
int arr4[] = {
1 , 2 , 3 , 4
};
int arr5[] = {
-1 , -2 , -3 , -4 , -5 , 1 , 2 , 3 , 4
};
// Test 1
int size = sizeof(arr1) / sizeof(arr1[0]);
cout << " Before Arrange \n";
cout << " After Arrange \n";
// Test 2
size = sizeof(arr2) / sizeof(arr2[0]);
cout << "\n Before Arrange \n";
cout << " After Arrange \n";
// Test 3
size = sizeof(arr3) / sizeof(arr3[0]);
cout << " Before Arrange \n";
cout << " After Arrange \n";
// Test 4
size = sizeof(arr4) / sizeof(arr4[0]);
cout << "\n Before Arrange \n";
cout << " After Arrange \n";
// Test 5
size = sizeof(arr5) / sizeof(arr5[0]);
cout << "\n Before Arrange \n";
cout << " After Arrange \n";
return 0;
}``````

#### Output

`````` Before Arrange
-1  -2  -5  1  -4  2  5  2  4  -4
After Arrange
1  -2  2  -1  5  -5  2  -4  4  -4

Before Arrange
8  7  0  -5  1  -4  -9  5  2  4  -4  -6
After Arrange
8  -5  0  -4  1  -9  5  -4  2  -6  4  7
Before Arrange
8  6  2  6  -2  5  2  -6  2  -6  3
After Arrange
8  -2  2  -6  6  -6  2  6  2  5  3

Before Arrange
1  2  3  4
After Arrange
1  2  3  4

Before Arrange
-1  -2  -3  -4  -5  1  2  3  4
After Arrange
1  -2  2  -4  3  -1  4  -5  -3``````
``````// Include namespace system
using System;
/*
C# Program
Rearrange positive and negative numbers in array
*/
public class Arrangement
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("  " + arr[i]);
}
Console.Write("\n");
}
// Swapping of array elements
public void swapNode(int[] arr, int a, int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
// Arrange pairwise positive and negative elements
public void rearrange(int[] arr, int size)
{
int negative = 1;
int positive = 0;
// Loop controlling variable
int i = 1;
// Set positive elements in Even position in given arrays
while (i < size && positive < size)
{
if (arr[positive] > 0)
{
positive += 2;
}
else if (arr[i] >= 0)
{
if (i < positive)
{
if (i % 2 == 1)
{
// When if swap the positive elements at odd position element
swapNode(arr, i, positive);
positive += 2;
}
}
else
{
swapNode(arr, i, positive);
positive += 2;
}
}
i++;
}
i = 0;
// Set negative elements in Odd position in given arrays
while (i < size && negative < size)
{
if (arr[negative] < 0)
{
// When element already negative number
negative += 2;
}
else if (arr[i] < 0)
{
if (i < negative)
{
if (i % 2 == 0)
{
swapNode(arr, i, negative);
negative += 2;
}
}
else
{
swapNode(arr, i, negative);
negative += 2;
}
}
i++;
}
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] arr1 = {
-1 , -2 , -5 , 1 , -4 , 2 , 5 , 2 , 4 , -4
};
int[] arr2 = {
8 , 7 , 0 , -5 , 1 , -4 , -9 , 5 , 2 , 4 , -4 , -6
};
int[] arr3 = {
8 , 6 , 2 , 6 , -2 , 5 , 2 , -6 , 2 , -6 , 3
};
int[] arr4 = {
1 , 2 , 3 , 4
};
int[] arr5 = {
-1 , -2 , -3 , -4 , -5 , 1 , 2 , 3 , 4
};
// Test 1
int size = arr1.Length;
Console.Write(" Before Arrange \n");
Console.Write(" After Arrange \n");
// Test 2
size = arr2.Length;
Console.Write("\n Before Arrange \n");
Console.Write(" After Arrange \n");
// Test 3
size = arr3.Length;
Console.Write(" Before Arrange \n");
Console.Write(" After Arrange \n");
// Test 4
size = arr4.Length;
Console.Write("\n Before Arrange \n");
Console.Write(" After Arrange \n");
// Test 5
size = arr5.Length;
Console.Write("\n Before Arrange \n");
Console.Write(" After Arrange \n");
}
}``````

#### Output

`````` Before Arrange
-1  -2  -5  1  -4  2  5  2  4  -4
After Arrange
1  -2  2  -1  5  -5  2  -4  4  -4

Before Arrange
8  7  0  -5  1  -4  -9  5  2  4  -4  -6
After Arrange
8  -5  0  -4  1  -9  5  -4  2  -6  4  7
Before Arrange
8  6  2  6  -2  5  2  -6  2  -6  3
After Arrange
8  -2  2  -6  6  -6  2  6  2  5  3

Before Arrange
1  2  3  4
After Arrange
1  2  3  4

Before Arrange
-1  -2  -3  -4  -5  1  2  3  4
After Arrange
1  -2  2  -4  3  -1  4  -5  -3``````
``````<?php
/*
Php Program
Rearrange positive and negative numbers in array
*/
class Arrangement
{
//Display elements of given array
public	function printArray( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "  ". \$arr[\$i];
}
echo "\n";
}
// Swapping of array elements
public	function swapNode( & \$arr, \$a, \$b)
{
\$temp = \$arr[\$a];
\$arr[\$a] = \$arr[\$b];
\$arr[\$b] = \$temp;
}
// Arrange pairwise positive and negative elements
public	function rearrange( & \$arr, \$size)
{
\$negative = 1;
\$positive = 0;
// Loop controlling variable
\$i = 1;
// Set positive elements in Even position in given arrays
while (\$i < \$size && \$positive < \$size)
{
if (\$arr[\$positive] > 0)
{
\$positive += 2;
}
else if (\$arr[\$i] >= 0)
{
if (\$i < \$positive)
{
if (\$i % 2 == 1)
{
// When if swap the positive elements at odd position element
\$this->swapNode(\$arr, \$i, \$positive);
\$positive += 2;
}
}
else
{
\$this->swapNode(\$arr, \$i, \$positive);
\$positive += 2;
}
}
\$i++;
}
\$i = 0;
// Set negative elements in Odd position in given arrays
while (\$i < \$size && \$negative < \$size)
{
if (\$arr[\$negative] < 0)
{
// When element already negative number
\$negative += 2;
}
else if (\$arr[\$i] < 0)
{
if (\$i < \$negative)
{
if (\$i % 2 == 0)
{
\$this->swapNode(\$arr, \$i, \$negative);
\$negative += 2;
}
}
else
{
\$this->swapNode(\$arr, \$i, \$negative);
\$negative += 2;
}
}
\$i++;
}
}
}

function main()
{
// Define array of integer elements
\$arr1 = array(-1, -2, -5, 1, -4, 2, 5, 2, 4, -4);
\$arr2 = array(8, 7, 0, -5, 1, -4, -9, 5, 2, 4, -4, -6);
\$arr3 = array(8, 6, 2, 6, -2, 5, 2, -6, 2, -6, 3);
\$arr4 = array(1, 2, 3, 4);
\$arr5 = array(-1, -2, -3, -4, -5, 1, 2, 3, 4);
// Test 1
\$size = count(\$arr1);
echo " Before Arrange \n";
echo " After Arrange \n";
// Test 2
\$size = count(\$arr2);
echo "\n Before Arrange \n";
echo " After Arrange \n";
// Test 3
\$size = count(\$arr3);
echo " Before Arrange \n";
echo " After Arrange \n";
// Test 4
\$size = count(\$arr4);
echo "\n Before Arrange \n";
echo " After Arrange \n";
// Test 5
\$size = count(\$arr5);
echo "\n Before Arrange \n";
echo " After Arrange \n";
}
main();``````

#### Output

`````` Before Arrange
-1  -2  -5  1  -4  2  5  2  4  -4
After Arrange
1  -2  2  -1  5  -5  2  -4  4  -4

Before Arrange
8  7  0  -5  1  -4  -9  5  2  4  -4  -6
After Arrange
8  -5  0  -4  1  -9  5  -4  2  -6  4  7
Before Arrange
8  6  2  6  -2  5  2  -6  2  -6  3
After Arrange
8  -2  2  -6  6  -6  2  6  2  5  3

Before Arrange
1  2  3  4
After Arrange
1  2  3  4

Before Arrange
-1  -2  -3  -4  -5  1  2  3  4
After Arrange
1  -2  2  -4  3  -1  4  -5  -3``````
``````/*
Node Js Program
Rearrange positive and negative numbers in array
*/
class Arrangement
{
//Display elements of given array
printArray(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i]);
}
process.stdout.write("\n");
}
// Swapping of array elements
swapNode(arr, a, b)
{
var temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
// Arrange pairwise positive and negative elements
rearrange(arr, size)
{
var negative = 1;
var positive = 0;
// Loop controlling variable
var i = 1;
// Set positive elements in Even position in given arrays
while (i < size && positive < size)
{
if (arr[positive] > 0)
{
positive += 2;
}
else if (arr[i] >= 0)
{
if (i < positive)
{
if (i % 2 == 1)
{
// When if swap the positive elements at odd position element
this.swapNode(arr, i, positive);
positive += 2;
}
}
else
{
this.swapNode(arr, i, positive);
positive += 2;
}
}
i++;
}
i = 0;
// Set negative elements in Odd position in given arrays
while (i < size && negative < size)
{
if (arr[negative] < 0)
{
// When element already negative number
negative += 2;
}
else if (arr[i] < 0)
{
if (i < negative)
{
if (i % 2 == 0)
{
this.swapNode(arr, i, negative);
negative += 2;
}
}
else
{
this.swapNode(arr, i, negative);
negative += 2;
}
}
i++;
}
}
}

function main()
{
// Define array of integer elements
var arr1 = [-1, -2, -5, 1, -4, 2, 5, 2, 4, -4];
var arr2 = [8, 7, 0, -5, 1, -4, -9, 5, 2, 4, -4, -6];
var arr3 = [8, 6, 2, 6, -2, 5, 2, -6, 2, -6, 3];
var arr4 = [1, 2, 3, 4];
var arr5 = [-1, -2, -3, -4, -5, 1, 2, 3, 4];
// Test 1
var size = arr1.length;
process.stdout.write(" Before Arrange \n");
process.stdout.write(" After Arrange \n");
// Test 2
size = arr2.length;
process.stdout.write("\n Before Arrange \n");
process.stdout.write(" After Arrange \n");
// Test 3
size = arr3.length;
process.stdout.write(" Before Arrange \n");
process.stdout.write(" After Arrange \n");
// Test 4
size = arr4.length;
process.stdout.write("\n Before Arrange \n");
process.stdout.write(" After Arrange \n");
// Test 5
size = arr5.length;
process.stdout.write("\n Before Arrange \n");
process.stdout.write(" After Arrange \n");
}
main();``````

#### Output

`````` Before Arrange
-1  -2  -5  1  -4  2  5  2  4  -4
After Arrange
1  -2  2  -1  5  -5  2  -4  4  -4

Before Arrange
8  7  0  -5  1  -4  -9  5  2  4  -4  -6
After Arrange
8  -5  0  -4  1  -9  5  -4  2  -6  4  7
Before Arrange
8  6  2  6  -2  5  2  -6  2  -6  3
After Arrange
8  -2  2  -6  6  -6  2  6  2  5  3

Before Arrange
1  2  3  4
After Arrange
1  2  3  4

Before Arrange
-1  -2  -3  -4  -5  1  2  3  4
After Arrange
1  -2  2  -4  3  -1  4  -5  -3``````
``````#  Python 3 Program
#  Rearrange positive and negative numbers in array

class Arrangement :
# Display elements of given array
def printArray(self, arr, size) :
i = 0
while (i < size) :
print("  ", arr[i], end = "")
i += 1

print(end = "\n")

#  Swapping of array elements
def swapNode(self, arr, a, b) :
temp = arr[a]
arr[a] = arr[b]
arr[b] = temp

#  Arrange pairwise positive and negative elements
def rearrange(self, arr, size) :
negative = 1
positive = 0
#  Loop controlling variable
i = 1
#  Set positive elements in Even position in given arrays
while (i < size and positive < size) :
if (arr[positive] > 0) :
positive += 2

elif(arr[i] >= 0) :
if (i < positive) :
if (i % 2 == 1) :
#  When if swap the positive elements at odd position element
self.swapNode(arr, i, positive)
positive += 2

else :
self.swapNode(arr, i, positive)
positive += 2

i += 1

i = 0
#  Set negative elements in Odd position in given arrays
while (i < size and negative < size) :
if (arr[negative] < 0) :
#  When element already negative number
negative += 2

elif(arr[i] < 0) :
if (i < negative) :
if (i % 2 == 0) :
self.swapNode(arr, i, negative)
negative += 2

else :
self.swapNode(arr, i, negative)
negative += 2

i += 1

def main() :
#  Define array of integer elements
arr1 = [-1, -2, -5, 1, -4, 2, 5, 2, 4, -4]
arr2 = [8, 7, 0, -5, 1, -4, -9, 5, 2, 4, -4, -6]
arr3 = [8, 6, 2, 6, -2, 5, 2, -6, 2, -6, 3]
arr4 = [1, 2, 3, 4]
arr5 = [-1, -2, -3, -4, -5, 1, 2, 3, 4]
#  Test 1
size = len(arr1)
print(" Before Arrange ")
print(" After Arrange ")
#  Test 2
size = len(arr2)
print("\n Before Arrange ")
print(" After Arrange ")
#  Test 3
size = len(arr3)
print(" Before Arrange ")
print(" After Arrange ")
#  Test 4
size = len(arr4)
print("\n Before Arrange ")
print(" After Arrange ")
#  Test 5
size = len(arr5)
print("\n Before Arrange ")
print(" After Arrange ")

if __name__ == "__main__": main()``````

#### Output

`````` Before Arrange
-1   -2   -5   1   -4   2   5   2   4   -4
After Arrange
1   -2   2   -1   5   -5   2   -4   4   -4

Before Arrange
8   7   0   -5   1   -4   -9   5   2   4   -4   -6
After Arrange
8   -5   0   -4   1   -9   5   -4   2   -6   4   7
Before Arrange
8   6   2   6   -2   5   2   -6   2   -6   3
After Arrange
8   -2   2   -6   6   -6   2   6   2   5   3

Before Arrange
1   2   3   4
After Arrange
1   2   3   4

Before Arrange
-1   -2   -3   -4   -5   1   2   3   4
After Arrange
1   -2   2   -4   3   -1   4   -5   -3``````
``````#  Ruby Program
#  Rearrange positive and negative numbers in array

class Arrangement
# Display elements of given array
def printArray(arr, size)
i = 0
while (i < size)
print("  ", arr[i])
i += 1
end

print("\n")
end

#  Swapping of array elements
def swapNode(arr, a, b)
temp = arr[a]
arr[a] = arr[b]
arr[b] = temp
end

#  Arrange pairwise positive and negative elements
def rearrange(arr, size)
negative = 1
positive = 0
#  Loop controlling variable
i = 1
#  Set positive elements in Even position in given arrays
while (i < size && positive < size)
if (arr[positive] > 0)
positive += 2
elsif(arr[i] >= 0)
if (i < positive)
if (i % 2 == 1)
#  When if swap the positive elements at odd position element
self.swapNode(arr, i, positive)
positive += 2
end

else
self.swapNode(arr, i, positive)
positive += 2
end

end

i += 1
end

i = 0
#  Set negative elements in Odd position in given arrays
while (i < size && negative < size)
if (arr[negative] < 0)
#  When element already negative number
negative += 2
elsif(arr[i] < 0)
if (i < negative)
if (i % 2 == 0)
self.swapNode(arr, i, negative)
negative += 2
end

else
self.swapNode(arr, i, negative)
negative += 2
end

end

i += 1
end

end

end

def main()
#  Define array of integer elements
arr1 = [-1, -2, -5, 1, -4, 2, 5, 2, 4, -4]
arr2 = [8, 7, 0, -5, 1, -4, -9, 5, 2, 4, -4, -6]
arr3 = [8, 6, 2, 6, -2, 5, 2, -6, 2, -6, 3]
arr4 = [1, 2, 3, 4]
arr5 = [-1, -2, -3, -4, -5, 1, 2, 3, 4]
#  Test 1
size = arr1.length
print(" Before Arrange \n")
print(" After Arrange \n")
#  Test 2
size = arr2.length
print("\n Before Arrange \n")
print(" After Arrange \n")
#  Test 3
size = arr3.length
print(" Before Arrange \n")
print(" After Arrange \n")
#  Test 4
size = arr4.length
print("\n Before Arrange \n")
print(" After Arrange \n")
#  Test 5
size = arr5.length
print("\n Before Arrange \n")
print(" After Arrange \n")
end

main()``````

#### Output

`````` Before Arrange
-1  -2  -5  1  -4  2  5  2  4  -4
After Arrange
1  -2  2  -1  5  -5  2  -4  4  -4

Before Arrange
8  7  0  -5  1  -4  -9  5  2  4  -4  -6
After Arrange
8  -5  0  -4  1  -9  5  -4  2  -6  4  7
Before Arrange
8  6  2  6  -2  5  2  -6  2  -6  3
After Arrange
8  -2  2  -6  6  -6  2  6  2  5  3

Before Arrange
1  2  3  4
After Arrange
1  2  3  4

Before Arrange
-1  -2  -3  -4  -5  1  2  3  4
After Arrange
1  -2  2  -4  3  -1  4  -5  -3
``````
``````/*
Scala Program
Rearrange positive and negative numbers in array
*/
class Arrangement
{
//Display elements of given array
def printArray(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("  " + arr(i));
i += 1;
}
print("\n");
}
// Swapping of array elements
def swapNode(arr: Array[Int], a: Int, b: Int): Unit = {
var temp: Int = arr(a);
arr(a) = arr(b);
arr(b) = temp;
}
// Arrange pairwise positive and negative elements
def rearrange(arr: Array[Int], size: Int): Unit = {
var negative: Int = 1;
var positive: Int = 0;
// Loop controlling variable
var i: Int = 1;
// Set positive elements in Even position in given arrays
while (i < size && positive < size)
{
if (arr(positive) > 0)
{
positive += 2;
}
else if (arr(i) >= 0)
{
if (i < positive)
{
if (i % 2 == 1)
{
// When if swap the positive elements at odd position element
this.swapNode(arr, i, positive);
positive += 2;
}
}
else
{
this.swapNode(arr, i, positive);
positive += 2;
}
}
i += 1;
}
i = 0;
// Set negative elements in Odd position in given arrays
while (i < size && negative < size)
{
if (arr(negative) < 0)
{
// When element already negative number
negative += 2;
}
else if (arr(i) < 0)
{
if (i < negative)
{
if (i % 2 == 0)
{
this.swapNode(arr, i, negative);
negative += 2;
}
}
else
{
this.swapNode(arr, i, negative);
negative += 2;
}
}
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Arrangement = new Arrangement();
// Define array of integer elements
var arr1: Array[Int] = Array(-1, -2, -5, 1, -4, 2, 5, 2, 4, -4);
var arr2: Array[Int] = Array(8, 7, 0, -5, 1, -4, -9, 5, 2, 4, -4, -6);
var arr3: Array[Int] = Array(8, 6, 2, 6, -2, 5, 2, -6, 2, -6, 3);
var arr4: Array[Int] = Array(1, 2, 3, 4);
var arr5: Array[Int] = Array(-1, -2, -3, -4, -5, 1, 2, 3, 4);
// Test 1
var size: Int = arr1.length;
print(" Before Arrange \n");
print(" After Arrange \n");
// Test 2
size = arr2.length;
print("\n Before Arrange \n");
print(" After Arrange \n");
// Test 3
size = arr3.length;
print(" Before Arrange \n");
print(" After Arrange \n");
// Test 4
size = arr4.length;
print("\n Before Arrange \n");
print(" After Arrange \n");
// Test 5
size = arr5.length;
print("\n Before Arrange \n");
print(" After Arrange \n");
}
}``````

#### Output

`````` Before Arrange
-1  -2  -5  1  -4  2  5  2  4  -4
After Arrange
1  -2  2  -1  5  -5  2  -4  4  -4

Before Arrange
8  7  0  -5  1  -4  -9  5  2  4  -4  -6
After Arrange
8  -5  0  -4  1  -9  5  -4  2  -6  4  7
Before Arrange
8  6  2  6  -2  5  2  -6  2  -6  3
After Arrange
8  -2  2  -6  6  -6  2  6  2  5  3

Before Arrange
1  2  3  4
After Arrange
1  2  3  4

Before Arrange
-1  -2  -3  -4  -5  1  2  3  4
After Arrange
1  -2  2  -4  3  -1  4  -5  -3``````
``````/*
Swift 4 Program
Rearrange positive and negative numbers in array
*/
class Arrangement
{
//Display elements of given array
func printArray(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("  ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Swapping of array elements
func swapNode(_ arr: inout[Int], _ a: Int, _ b: Int)
{
let temp: Int = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
// Arrange pairwise positive and negative elements
func rearrange(_ arr: inout[Int], _ size: Int)
{
var negative: Int = 1;
var positive: Int = 0;
// Loop controlling variable
var i: Int = 1;
// Set positive elements in Even position in given arrays
while (i < size && positive < size)
{
if (arr[positive] > 0)
{
positive += 2;
}
else if (arr[i] >= 0)
{
if (i < positive)
{
if (i % 2 == 1)
{
// When if swap the positive elements at odd position element
self.swapNode(&arr, i, positive);
positive += 2;
}
}
else
{
self.swapNode(&arr, i, positive);
positive += 2;
}
}
i += 1;
}
i = 0;
// Set negative elements in Odd position in given arrays
while (i < size && negative < size)
{
if (arr[negative] < 0)
{
// When element already negative number
negative += 2;
}
else if (arr[i] < 0)
{
if (i < negative)
{
if (i % 2 == 0)
{
self.swapNode(&arr, i, negative);
negative += 2;
}
}
else
{
self.swapNode(&arr, i, negative);
negative += 2;
}
}
i += 1;
}
}
}
func main()
{
// Define array of integer elements
var arr1: [Int] = [-1, -2, -5, 1, -4, 2, 5, 2, 4, -4];
var arr2: [Int] = [8, 7, 0, -5, 1, -4, -9, 5, 2, 4, -4, -6];
var arr3: [Int] = [8, 6, 2, 6, -2, 5, 2, -6, 2, -6, 3];
var arr4: [Int] = [1, 2, 3, 4];
var arr5: [Int] = [-1, -2, -3, -4, -5, 1, 2, 3, 4];
// Test 1
var size: Int = arr1.count;
print(" Before Arrange ");
print(" After Arrange ");
// Test 2
size = arr2.count;
print("\n Before Arrange ");
print(" After Arrange ");
// Test 3
size = arr3.count;
print(" Before Arrange ");
print(" After Arrange ");
// Test 4
size = arr4.count;
print("\n Before Arrange ");
print(" After Arrange ");
// Test 5
size = arr5.count;
print("\n Before Arrange ");
print(" After Arrange ");
}
main();``````

#### Output

`````` Before Arrange
-1   -2   -5   1   -4   2   5   2   4   -4
After Arrange
1   -2   2   -1   5   -5   2   -4   4   -4

Before Arrange
8   7   0   -5   1   -4   -9   5   2   4   -4   -6
After Arrange
8   -5   0   -4   1   -9   5   -4   2   -6   4   7
Before Arrange
8   6   2   6   -2   5   2   -6   2   -6   3
After Arrange
8   -2   2   -6   6   -6   2   6   2   5   3

Before Arrange
1   2   3   4
After Arrange
1   2   3   4

Before Arrange
-1   -2   -3   -4   -5   1   2   3   4
After Arrange
1   -2   2   -4   3   -1   4   -5   -3``````
``````/*
Kotlin Program
Rearrange positive and negative numbers in array
*/
class Arrangement
{
//Display elements of given array
fun printArray(arr: Array<Int>, size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print("  " + arr[i]);
i += 1;
}
print("\n");
}
// Swapping of array elements
fun swapNode(arr: Array<Int>, a: Int, b: Int): Unit
{
var temp: Int = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
// Arrange pairwise positive and negative elements
fun rearrange(arr: Array<Int>, size: Int): Unit
{
var negative: Int = 1;
var positive: Int = 0;
// Loop controlling variable
var i: Int = 1;
// Set positive elements in Even position in given arrays
while (i<size && positive<size)
{
if (arr[positive] > 0)
{
positive += 2;
}
else
if (arr[i] >= 0)
{
if (i<positive)
{
if (i % 2 == 1)
{
// When if swap the positive elements at odd position element
this.swapNode(arr, i, positive);
positive += 2;
}
}
else
{
this.swapNode(arr, i, positive);
positive += 2;
}
}
i += 1;
}
i = 0;
// Set negative elements in Odd position in given arrays
while (i < size && negative < size)
{
if (arr[negative] < 0)
{
// When element already negative number
negative += 2;
}
else
if (arr[i] < 0)
{
if (i < negative)
{
if (i % 2 == 0)
{
this.swapNode(arr, i, negative);
negative += 2;
}
}
else
{
this.swapNode(arr, i, negative);
negative += 2;
}
}
i += 1;
}
}
}
fun main(args: Array<String>): Unit
{
// Define array of integer elements
var arr1: Array<Int> = arrayOf(-1, -2, -5, 1, -4, 2, 5, 2, 4, -4);
var arr2: Array<Int> = arrayOf(8, 7, 0, -5, 1, -4, -9, 5, 2, 4, -4, -6);
var arr3: Array<Int> = arrayOf(8, 6, 2, 6, -2, 5, 2, -6, 2, -6, 3);
var arr4: Array<Int> = arrayOf(1, 2, 3, 4);
var arr5: Array<Int> = arrayOf(-1, -2, -3, -4, -5, 1, 2, 3, 4);
// Test 1
var size: Int = arr1.count();
print(" Before Arrange \n");
print(" After Arrange \n");
// Test 2
size = arr2.count();
print("\n Before Arrange \n");
print(" After Arrange \n");
// Test 3
size = arr3.count();
print(" Before Arrange \n");
print(" After Arrange \n");
// Test 4
size = arr4.count();
print("\n Before Arrange \n");
print(" After Arrange \n");
// Test 5
size = arr5.count();
print("\n Before Arrange \n");
print(" After Arrange \n");
}``````

#### Output

`````` Before Arrange
-1  -2  -5  1  -4  2  5  2  4  -4
After Arrange
1  -2  2  -1  5  -5  2  -4  4  -4

Before Arrange
8  7  0  -5  1  -4  -9  5  2  4  -4  -6
After Arrange
8  -5  0  -4  1  -9  5  -4  2  -6  4  7
Before Arrange
8  6  2  6  -2  5  2  -6  2  -6  3
After Arrange
8  -2  2  -6  6  -6  2  6  2  5  3

Before Arrange
1  2  3  4
After Arrange
1  2  3  4

Before Arrange
-1  -2  -3  -4  -5  1  2  3  4
After Arrange
1  -2  2  -4  3  -1  4  -5  -3``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.