# Partition negative and positive without comparison with 0

• Difficulty Level : Easy
• Last Updated : 19 Aug, 2022

Given an array of n integers, both negative and positive, partition them into two different arrays without comparing any element with 0.

Examples:

```Input : arr[] = [1, -2, 6, -7, 8]
Output : a[] = [1, 6, 8]
b[] = [-2, -7]```

Algorithm:

1. Initialize two empty vectors. Push the first element of the array into any of the two vectors. Suppose the first vector. Let it be denoted by x.
2. For every other element, arr to arr[n-1], check if its sign and the sign of x are the same or not. If the signs are the same, then push the element in the same vector. Else, push the element into the other vector.
3. After the traversal of the two vectors has been completed, print both the vectors.

How to check if their signs are opposite or not?
Let the integers be checked to be denoted by x and y. The sign bit is 1 in negative numbers, and 0 in positive numbers. The XOR of x and y will have the sign bit is 1 if and only if they have opposite signs. In other words, the XOR of x and y will be a negative number if and only if x and y have opposite signs.

Implementation:

## CPP

 `// CPP program to rearrange positive and``// negative numbers without comparison``// with 0.``#include ``using` `namespace` `std;` `bool` `oppositeSigns(``int` `x, ``int` `y)``{``    ``return` `((x ^ y) < 0);``}` `void` `partitionNegPos(``int` `arr[], ``int` `n)``{``    ``vector<``int``> a, b;` `    ``// Push first element to a.``    ``a.push_back(arr);` `    ``// Now put all elements of same sign``    ``// in a[] and opposite sign in b[]``    ``for` `(``int` `i = 1; i < n; i++) {``        ``if` `(oppositeSigns(a, arr[i]))``            ``b.push_back(arr[i]);``        ``else``            ``a.push_back(arr[i]);``    ``}` `    ``// Print a[] and b[]``    ``for` `(``int` `i = 0; i < a.size(); i++)``        ``cout << a[i] << ``' '``;``    ``cout << ``'\n'``;``    ``for` `(``int` `i = 0; i < b.size(); i++)``        ``cout << b[i] << ``' '``;``}` `int` `main()``{``    ``int` `arr[] = { 1, -2, 6, -7, 8 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``partitionNegPos(arr, n);``    ``return` `0;``}`

## Java

 `// Java program to rearrange positive and``// negative numbers without comparison``// with 0.``import` `java.util.*;` `class` `GFG``{``    ` `static` `boolean` `oppositeSigns(``int` `x, ``int` `y)``{``    ``return` `((x ^ y) < ``0``);``}` `static` `void` `partitionNegPos(``int` `arr[], ``int` `n)``{``    ``Vector a = ``new` `Vector();``    ``Vector b = ``new` `Vector();` `    ``// Push first element to a.``    ``a.add(arr[``0``]);` `    ``// Now put all elements of same sign``    ``// in a[] and opposite sign in b[]``    ``for` `(``int` `i = ``1``; i < n; i++)``    ``{``        ``if` `(oppositeSigns(a.get(``0``), arr[i]))``            ``b.add(arr[i]);``        ``else``            ``a.add(arr[i]);``    ``}` `    ``// Print a[] and b[]``    ``for` `(``int` `i = ``0``; i < a.size(); i++)``        ``System.out.print(a.get(i) + ``" "``);``    ``System.out.println(``""``);``    ``for` `(``int` `i = ``0``; i < b.size(); i++)``        ``System.out.print(b.get(i) + ``" "``);``}` `public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``1``, -``2``, ``6``, -``7``, ``8` `};``    ``int` `n = arr.length;``    ``partitionNegPos(arr, n);``}``}` `// This code has been contributed by 29AjayKumar`

## Python3

 `# Python3 program to rearrange positive``# and negative numbers without comparison``# with 0.` `def` `oppositeSigns(x, y):``    ` `    ``return` `((x ^ y) < ``0``)` `def` `partitionNegPos(arr, n):``    ` `    ``a ``=` `[]``    ``b ``=` `[]``    ` `    ``# Push first element to a.``    ``a ``=` `a ``+` `[arr[``0``]]``    ` `    ``# Now put all elements of same sign``    ``# in a[] and opposite sign in b[]``    ``for` `i ``in` `range``(``1``, n) :``        ``if` `(oppositeSigns(a[``0``], arr[i])):``            ``b ``=` `b ``+` `[arr[i]]``        ``else``:``            ``a ``=` `a ``+` `[arr[i]]``            ` `    ``# Print a[] and b[]``    ``for` `i ``in` `range``(``0``, ``len``(a)):``        ``print``(a[i], end ``=` `' '``)``    ``print``("")``    ` `    ``for` `i ``in` `range``(``0``, ``len``(b)):``        ``print``(b[i], end ``=` `' '``)` `# Driver code``arr ``=` `[``1``, ``-``2``, ``6``, ``-``7``, ``8` `]``n ``=` `len``(arr)``partitionNegPos(arr, n)` `# This code is contributed by Smitha`

## C#

 `// C# program to rearrange positive and``// negative numbers without comparison``// with 0.``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{``    ` `static` `bool` `oppositeSigns(``int` `x, ``int` `y)``{``    ``return` `((x ^ y) < 0);``}` `static` `void` `partitionNegPos(``int` `[]arr, ``int` `n)``{``    ``List<``int``> a = ``new` `List<``int``> ();``    ``List<``int``> b = ``new` `List<``int``> ();` `    ``// Push first element to a.``    ``a.Add(arr);` `    ``// Now put all elements of same sign``    ``// in a[] and opposite sign in b[]``    ``for` `(``int` `i = 1; i < n; i++)``    ``{``        ``if` `(oppositeSigns(a, arr[i]))``            ``b.Add(arr[i]);``        ``else``            ``a.Add(arr[i]);``    ``}` `    ``// Print a[] and b[]``    ``for` `(``int` `i = 0; i < a.Count; i++)``        ``Console.Write(a[i] + ``" "``);``    ``Console.WriteLine(``""``);``    ``for` `(``int` `i = 0; i < b.Count; i++)``        ``Console.Write(b[i] + ``" "``);``}` `// Driver code``public` `static` `void` `Main()``{``    ``int` `[]arr = { 1, -2, 6, -7, 8 };``    ``int` `n = arr.Length;``    ``partitionNegPos(arr, n);``}``}` `/* This code contributed by PrinciRaj1992 */`

## Javascript

 ``

Output

```1 6 8
-2 -7 ```

Another approach in Python : Using startswith()

Implementation:

## Python3

 `# Python3 program to rearrange positive``# and negative numbers without comparison``# with 0.``arr ``=` `[``1``, ``-``2``, ``6``, ``-``7``, ``8` `]``x``=``list``(``map``(``str``,arr))``po``=``[]``ne``=``[]``for` `i ``in` `x:``    ``if``(i.startswith(``"-"``)):``        ``ne.append(``int``(i))``    ``else``:``        ``po.append(``int``(i))``print``(po)``print``(ne)`

Output

```[1, 6, 8]
[-2, -7]```

My Personal Notes arrow_drop_up