# Move all negative numbers to beginning and positive to end with constant extra space

An array contains both positive and negative numbers in random order. Rearrange the array elements so that all negative numbers appear before all positive numbers.

Examples :

```Input : -12, 11, -13, -5, 6, -7, 5, -3, -6
Output :-12 -13 -5 -7 -3 -6 11 6 5
```

Note :- Order of elements is not important here.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to simply apply partition process of quicksort.

## C++

 `// A C++ program to put all negative ` `// numbers before positive numbers ` `#include ` `using` `namespace` `std; ` ` `  `void` `rearrange(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `j = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(arr[i] < 0) { ` `            ``if` `(i != j) ` `                ``swap(arr[i], arr[j]); ` `            ``j++; ` `        ``} ` `    ``} ` `} ` ` `  `// A utility function to print an array ` `void` `printArray(``int` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``printf``(``"%d "``, arr[i]); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { -1, 2, -3, 4, 5, 6, -7, 8, 9 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``rearrange(arr, n); ` `    ``printArray(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to put all negative ` `// numbers before positive numbers ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``static` `void` `rearrange(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``int` `j = ``0``, temp; ` `        ``for` `(``int` `i = ``0``; i < n; i++) { ` `            ``if` `(arr[i] < ``0``) { ` `                ``if` `(i != j) { ` `                    ``temp = arr[i]; ` `                    ``arr[i] = arr[j]; ` `                    ``arr[j] = temp; ` `                ``} ` `                ``j++; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// A utility function to print an array ` `    ``static` `void` `printArray(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``System.out.print(arr[i] + ``" "``); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `arr[] = { -``1``, ``2``, -``3``, ``4``, ``5``, ``6``, -``7``, ``8``, ``9` `}; ` `        ``int` `n = arr.length; ` ` `  `        ``rearrange(arr, n); ` `        ``printArray(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by Nikita Tiwari. `

## Python3

 `# A Python 3 program to put ` `# all negative numbers before ` `# positive numbers ` ` `  `def` `rearrange(arr, n ) : ` ` `  `    ``# Please refer partition() in  ` `    ``# below post ` `    ``# https://www.geeksforgeeks.org / quick-sort / j = 0 ` `    ``j ``=` `0`  `    ``for` `i ``in` `range``(``0``, n) : ` `        ``if` `(arr[i] < ``0``) : ` `            ``temp ``=` `arr[i] ` `            ``arr[i] ``=` `arr[j] ` `            ``arr[j]``=` `temp ` `            ``j ``=` `j ``+` `1` `    ``print``(arr) ` ` `  `# Driver code ` `arr ``=` `[``-``1``, ``2``, ``-``3``, ``4``, ``5``, ``6``, ``-``7``, ``8``, ``9``] ` `n ``=` `len``(arr) ` `rearrange(arr, n) ` ` `  ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# program to put all negative ` `// numbers before positive numbers ` `using` `System; ` ` `  `class` `GFG { ` `    ``static` `void` `rearrange(``int``[] arr, ``int` `n) ` `    ``{ ` ` `  `        ``int` `j = 0, temp; ` `        ``for` `(``int` `i = 0; i < n; i++) { ` `            ``if` `(arr[i] < 0) { ` `                ``temp = arr[i]; ` `                ``arr[i] = arr[j]; ` `                ``arr[j] = temp; ` `                ``j++; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// A utility function to print an array ` `    ``static` `void` `printArray(``int``[] arr, ``int` `n) ` `    ``{ ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``Console.Write(arr[i] + ``" "``); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] arr = { -1, 2, -3, 4, 5, 6, -7, 8, 9 }; ` `        ``int` `n = arr.Length; ` ` `  `        ``rearrange(arr, n); ` `        ``printArray(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal. `

## PHP

 ` `

Output:

``` -1 -3 -7 4 5 6 2 8 9
```

Time complexity: O(n)
Auxiliary Space: O(1)

The problem becomes difficult if we need to maintain order of elements. Please refer Rearrange positive and negative numbers with constant extra space for details.

This article is contributed by Apoorva. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.