# Segregating negative and positive maintaining order and O(1) space

Segregation of negative and positive numbers in an array without using extra space, and maintaining insertion order and in O(n) time complexity.

Examples:

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

Input :5
11 -13 6 -7 5
Output :-13 -7 11 6 5
```

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

We have discussed this problem in below posts.

1. ers-beginning-positive-end-constant-extra-space/”>Rearrange positive and negative numbers without maintaining order.
2. Rearrange positive and negative numbers with constant extra space

In this post a new approach is discussed that takes O(1) extra space. We firs count total negative numbers, then move negative numbers one by one to correct position.

## C++

 `// C++ program to move all negative numbers ` `// to beginning and positive numbers to end ` `// keeping order. ` `#include ` `using` `namespace` `std; ` ` `  `void` `segregate(``int` `arr[], ``int` `n) ` `{ ` `    ``// Count negative numbers ` `    ``int` `count_negative = 0; ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `        ``if` `(arr[i] < 0) ` `            ``count_negative++;     ` ` `  `    ``// Run a loop until all negative ` `    ``// numbers are moved to the beginning ` `    ``int` `i = 0, j = i + 1; ` `    ``while` `(i != count_negative) { ` ` `  `        ``// If number is negative, update ` `        ``// position of next positive number. ` `        ``if` `(arr[i] < 0) { ` `            ``i++; ` `            ``j = i + 1; ` `        ``} ` ` `  `        ``// If number is positive, move it to ` `        ``// index j and increment j. ` `        ``else` `if` `(arr[i] > 0 && j < n) { ` `            ``swap(arr[i], arr[j]); ` `            ``j++; ` `        ``} ` `    ``} ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `count_negative = 0; ` `    ``int` `arr[] = { -12, 11, -13, -5, 6, -7, 5, -3, -6 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``segregate(arr, n); ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << arr[i] << ``" "``;  ` `} `

## Java

 `// Java program to move all  ` `// negative numbers to beginning  ` `// and positive numbers to end ` `// keeping order. ` `class` `GFG ` `{ ` `static` `void` `segregate(``int` `arr[],  ` `                      ``int` `n) ` `{ ` `     `  `// Count negative numbers ` `int` `count_negative = ``0``; ` `for` `(``int` `i = ``0``; i < n; i++)  ` `    ``if` `(arr[i] < ``0``) ` `        ``count_negative++;  ` ` `  `// Run a loop until all  ` `// negative numbers are  ` `// moved to the beginning ` `int` `i = ``0``, j = i + ``1``; ` `while` `(i != count_negative) ` `{ ` ` `  `    ``// If number is negative,  ` `    ``// update position of next ` `    ``// positive number. ` `    ``if` `(arr[i] < ``0``)  ` `    ``{ ` `        ``i++; ` `        ``j = i + ``1``; ` `    ``} ` ` `  `    ``// If number is positive, move  ` `    ``// it to index j and increment j. ` `    ``else` `if` `(arr[i] > ``0` `&& j < n)  ` `    ``{ ` `        ``int` `t = arr[i]; ` `        ``arr[i] = arr[j]; ` `        ``arr[j] = t; ` `        ``j++; ` `    ``} ` `} ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `count_negative = ``0``; ` `    ``int` `arr[] = { -``12``, ``11``, -``13``, -``5``, ` `                   ``6``, -``7``, ``5``, -``3``, -``6` `}; ` `    ``int` `n = arr.length; ` `    ``segregate(arr, n); ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``System.out.print(arr[i] + ``" "``);  ` `} ` `}  ` ` `  `// This code is contributed  ` `// by ChitraNayal `

## C#

 `// C# program to move all  ` `// negative numbers to beginning ` `// and positive numbers to end ` `// keeping order. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `static` `void` `segregate(``int``[] arr,  ` `                      ``int` `n) ` `{ ` `     `  `// Count negative numbers ` `int` `count_negative = 0,i; ` `for` `(i = 0; i < n; i++)  ` `    ``if` `(arr[i] < 0) ` `        ``count_negative++;  ` ` `  `// Run a loop until all  ` `// negative numbers are ` `// moved to the beginning ` `i = 0; ` `int` `j = i + 1; ` `while` `(i != count_negative)  ` `{ ` ` `  `    ``// If number is negative,  ` `    ``// update position of next  ` `    ``// positive number. ` `    ``if` `(arr[i] < 0)  ` `    ``{ ` `        ``i++; ` `        ``j = i + 1; ` `    ``} ` ` `  `    ``// If number is positive, move  ` `    ``// it to index j and increment j. ` `    ``else` `if` `(arr[i] > 0 && j < n) ` `    ``{ ` `        ``int` `t = arr[i]; ` `        ``arr[i] = arr[j]; ` `        ``arr[j] = t; ` `        ``j++; ` `    ``} ` `} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main()  ` `{ ` `    ``int``[] arr = { -12, 11, -13, -5,  ` `                    ``6, -7, 5, -3, -6 }; ` `    ``int` `n = arr.Length; ` `    ``segregate(arr, n); ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``Console.Write(arr[i] + ``" "``);  ` `} ` `}  ` ` `  `// This code is contributed  ` `// by ChitraNayal `

## Python 3

 `# Python 3 program to move all  ` `# negative numbers to beginning ` `# and positive numbers to end ` `# keeping order. ` ` `  `def` `segregate(arr, n): ` `     `  `    ``# Count negative numbers ` `    ``count_negative ``=` `0` `    ``for` `i ``in` `range``(n):  ` `        ``if` `(arr[i] < ``0``): ` `            ``count_negative ``+``=` `1` ` `  `    ``# Run a loop until all  ` `    ``# negative numbers are  ` `    ``# moved to the beginning ` `    ``i ``=` `0` `    ``j ``=` `i ``+` `1` `    ``while` `(i !``=` `count_negative): ` ` `  `        ``# If number is negative,  ` `        ``# update position of next  ` `        ``# positive number. ` `        ``if` `(arr[i] < ``0``) : ` `            ``i ``+``=` `1` `            ``j ``=` `i ``+` `1` ` `  `        ``# If number is positive, move  ` `        ``# it to index j and increment j. ` `        ``elif` `(arr[i] > ``0` `and` `j < n): ` `            ``t ``=` `arr[i] ` `            ``arr[i] ``=` `arr[j] ` `            ``arr[j] ``=` `t ` `            ``j ``+``=` `1` `         `  `# Driver Code ` `count_negative ``=` `0` `arr ``=` `[``-``12``, ``11``, ``-``13``, ``-``5``,  ` `        ``6``, ``-``7``, ``5``, ``-``3``, ``-``6` `] ` `segregate(arr, ``9``) ` `for` `i ``in` `range``(``9``): ` `    ``print``(arr[i] , end ``=``" "``) ` ` `  `# This code is contributed ` `# by ChitraNayal `

## PHP

 ` 0 && ``\$j` `< ``\$n``) ` `        ``{ ` `            ``\$t` `= ``\$arr``[``\$i``]; ` `            ``\$arr``[``\$i``] = ``\$arr``[``\$j``]; ` `            ``\$arr``[``\$j``] = ``\$t``; ` `            ``\$j``++; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver Code ` `\$count_negative` `= 0; ` `\$arr` `= ``array``(-12, 11, -13, -5,  ` `              ``6, -7, 5, -3, -6); ` `\$n` `= sizeof(``\$arr``); ` `segregate(``\$arr``, ``\$n``); ` `for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++) ` `    ``echo` `\$arr``[``\$i``] .``" "``;  ` `     `  `// This code is contributed ` `// by ChitraNayal ` `?> `

Output:

```-12 -13 -5 -7 -3 -6 11 6 5
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : chitranayal

Article Tags :
Practice Tags :

2

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