Rearrange Array in negative numbers, zero and then positive numbers order
Last Updated :
03 Feb, 2022
Given an arr[ ] of size N, containing positive, negative integers and one zero. The task is to rearrange the array in such a way that all negative numbers are on the left of 0 and all positive numbers are on the right.
Note: It is not mandatory to maintain the order of the numbers. If there are multiple possible answers, print any of them.
Examples:
Input: arr[] = {1, 0, -2, 3, 4, -5, -7, 9, -3}
Output: -2 -3 -5 -7 0 1 9 3 4
Explanation: The negative numbers are -2, -5, -7 and -3.
Input: arr[] = {-10, 5, -2, 0, -4, 5, 17, -9, -13, 11}
Output: -10 -2 -4 -13 -9 0 17 5 5 11
Naive approach: Create an array of size N and push negative numbers from starting and positive numbers from the end. Also, fill the single remaining place with 0.
Time complexity: O(N)
Auxiliary Space: O(N)
Efficient approach: The idea is to find the index where 0 is stored in the array. Once the 0 is found, consider that index as a pivot. Move all the negative values to the left side of the pivot and thus the right side values will be positive automatically.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void rearrangeArray( int arr[], int N)
{
int i, ind;
for (i = 0; i < N; i++) {
if (arr[i] == 0) {
ind = i;
break ;
}
}
int j = -1, k, temp;
for (k = 0; k < N; k++) {
if (arr[k] < 0) {
j += 1;
if (arr[j] == 0)
j += 1;
swap(arr[j], arr[k]);
}
}
swap(arr[ind], arr[j]);
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 0, -2, 3, 4, -5,
-7, 9, -3 };
int N = sizeof (arr) / sizeof ( int );
rearrangeArray(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void rearrangeArray( int arr[], int N)
{
int i, ind = - 1 ;
for (i = 0 ; i < N; i++) {
if (arr[i] == 0 ) {
ind = i;
break ;
}
}
int j = - 1 , k, temp;
for (k = 0 ; k < N; k++) {
if (arr[k] < 0 ) {
j += 1 ;
if (arr[j] == 0 )
j += 1 ;
temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
}
}
int temp2 = arr[j];
arr[j] = arr[ind];
arr[ind] = temp2;
for (j = 0 ; j < N; j++) {
System.out.print(arr[j] + " " );
}
}
public static void main (String[] args) {
int arr[] = { 1 , 0 , - 2 , 3 , 4 , - 5 ,
- 7 , 9 , - 3 };
int N = arr.length;
rearrangeArray(arr, N);
}
}
|
Python3
def rearrangeArray(arr, N):
ind = None
for i in range (N):
if (arr[i] = = 0 ):
ind = i;
break ;
j = - 1
temp = None
for k in range (N):
if (arr[k] < 0 ):
j + = 1 ;
if (arr[j] = = 0 ):
j + = 1 ;
temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
temp = arr[ind];
arr[ind] = arr[j];
arr[j] = temp;
for i in range (N):
print (arr[i], end = " " );
arr = [ 1 , 0 , - 2 , 3 , 4 , - 5 , - 7 , 9 , - 3 ];
N = len (arr)
rearrangeArray(arr, N);
|
C#
using System;
class GFG
{
static void rearrangeArray( int [] arr, int N)
{
int i, ind = -1;
for (i = 0; i < N; i++)
{
if (arr[i] == 0)
{
ind = i;
break ;
}
}
int j = -1, k, temp;
for (k = 0; k < N; k++)
{
if (arr[k] < 0)
{
j += 1;
if (arr[j] == 0)
j += 1;
temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
}
}
int temp2 = arr[j];
arr[j] = arr[ind];
arr[ind] = temp2;
for (j = 0; j < N; j++)
{
Console.Write(arr[j] + " " );
}
}
public static void Main()
{
int [] arr = { 1, 0, -2, 3, 4, -5, -7, 9, -3 };
int N = arr.Length;
rearrangeArray(arr, N);
}
}
|
Javascript
<script>
function rearrangeArray(arr, N) {
let i, ind;
for (i = 0; i < N; i++) {
if (arr[i] == 0) {
ind = i;
break ;
}
}
let j = -1, k, temp;
for (k = 0; k < N; k++) {
if (arr[k] < 0) {
j += 1;
if (arr[j] == 0)
j += 1;
let temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
}
}
temp = arr[ind];
arr[ind] = arr[j];
arr[j] = temp;
for (let i = 0; i < N; i++) {
document.write(arr[i] + " " );
}
}
let arr = [1, 0, -2, 3, 4, -5,
-7, 9, -3];
let N = arr.length;
rearrangeArray(arr, N);
</script>
|
Output
-2 -3 -5 -7 0 1 3 9 4
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...