Given an unsorted array arr[] of both negative and positive integer. The task is place all negative element at the end of array without changing the order of positive element and negative element.
Examples:
Input : arr[] = {1, -1, 3, 2, -7, -5, 11, 6 }
Output : 1 3 2 11 6 -1 -7 -5
Input : arr[] = {-5, 7, -3, -4, 9, 10, -1, 11}
Output : 7 9 10 11 -5 -3 -4 -1
We have discussed different approaches to this problem in below post.
Rearrange positive and negative numbers with constant extra space
The problem becomes easier if we are allowed to use extra space. Idea is create an empty array (temp[]). First we store all positive element of given array and then we store all negative element of array in Temp[]. Finally we copy temp[] to original array.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void segregateElements( int arr[], int n)
{
int temp[n];
int j = 0;
for ( int i = 0; i < n ; i++)
if (arr[i] >= 0 )
temp[j++] = arr[i];
if (j == n || j == 0)
return ;
for ( int i = 0 ; i < n ; i++)
if (arr[i] < 0)
temp[j++] = arr[i];
memcpy (arr, temp, sizeof (temp));
}
int main()
{
int arr[] = {1 ,-1 ,-3 , -2, 7, 5, 11, 6 };
int n = sizeof (arr)/ sizeof (arr[0]);
segregateElements(arr, n);
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static void segregateElements( int arr[], int n)
{
int temp[] = new int [n];
int j = 0 ;
for ( int i = 0 ; i < n; i++)
if (arr[i] >= 0 )
temp[j++] = arr[i];
if (j == n || j == 0 )
return ;
for ( int i = 0 ; i < n; i++)
if (arr[i] < 0 )
temp[j++] = arr[i];
for ( int i = 0 ; i < n; i++)
arr[i] = temp[i];
}
public static void main(String arg[])
{
int arr[] = { 1 , - 1 , - 3 , - 2 , 7 , 5 , 11 , 6 };
int n = arr.length;
segregateElements(arr, n);
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
}
|
Python3
def move(arr,n):
j = 0
ans = [ None ] * n
i = 0 ;j = n - 1
for k in range (n):
if arr[k]> = 0 :
ans[i] = arr[k]
i + = 1
else :
ans[j] = arr[k]
j - = 1
ans[i:] = ans[n - 1 :i - 1 : - 1 ]
return ans
arr = [ 1 , - 1 , - 3 , - 2 , 7 , 5 , 11 , 6 ]
n = len (arr)
print (move(arr, n))
|
C#
using System;
class GFG {
static void segregateElements( int [] arr, int n)
{
int [] temp = new int [n];
int j = 0;
for ( int i = 0; i < n; i++)
if (arr[i] >= 0)
temp[j++] = arr[i];
if (j == n || j == 0)
return ;
for ( int i = 0; i < n; i++)
if (arr[i] < 0)
temp[j++] = arr[i];
for ( int i = 0; i < n; i++)
arr[i] = temp[i];
}
public static void Main()
{
int [] arr = { 1, -1, -3, -2, 7, 5, 11, 6 };
int n = arr.Length;
segregateElements(arr, n);
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
}
|
PHP
<?php
function segregateElements(& $arr , $n )
{
$temp = array (0, $n , NULL);
$j = 0;
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] >= 0 )
$temp [ $j ++] = $arr [ $i ];
if ( $j == $n || $j == 0)
return ;
for ( $i = 0 ; $i < $n ; $i ++)
if ( $arr [ $i ] < 0)
$temp [ $j ++] = $arr [ $i ];
for ( $i = 0; $i < $n ; $i ++)
$arr [ $i ] = $temp [ $i ];
}
$arr = array (1 ,-1 ,-3 , -2, 7, 5, 11, 6 );
$n = sizeof( $arr );
segregateElements( $arr , $n );
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ] . " " ;
?>
|
Javascript
<script>
function segregateElements(arr, n)
{
let temp= new Array(n);
let j = 0;
for (let i = 0; i < n ; i++)
if (arr[i] >= 0 )
temp[j++] = arr[i];
if (j == n || j == 0)
return ;
for (let i = 0 ; i < n ; i++)
if (arr[i] < 0)
temp[j++] = arr[i];
for (let i = 0; i < n ; i++) arr[i] = temp[i];
}
let arr= [1 ,-1 ,-3 , -2, 7, 5, 11, 6];
let n = arr.length;
segregateElements(arr, n);
for (let i = 0; i < n; i++)
document.write(arr[i] + " " );
</script>
|
Output1 7 5 11 6 -1 -3 -2
Time Complexity : O(n)
Auxiliary space : O(n), since n extra space has been taken.
Related Articles:
Rearrange positive and negative numbers with constant extra space
Rearrange positive and negative numbers in O(n) time and O(1) extra space
This article is contributed by Nishant_Singh (Pintu). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.