Partition negative and positive without comparison with 0
Last Updated :
30 Jan, 2023
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:
- 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.
- For every other element, arr[1] 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.
- 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
#include <bits/stdc++.h>
using namespace std;
bool oppositeSigns( int x, int y) { return ((x ^ y) < 0); }
void partitionNegPos( int arr[], int n)
{
vector< int > a, b;
a.push_back(arr[0]);
for ( int i = 1; i < n; i++) {
if (oppositeSigns(a[0], arr[i]))
b.push_back(arr[i]);
else
a.push_back(arr[i]);
}
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[0]);
partitionNegPos(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean oppositeSigns( int x, int y)
{
return ((x ^ y) < 0 );
}
static void partitionNegPos( int arr[], int n)
{
Vector<Integer> a = new Vector<Integer>();
Vector<Integer> b = new Vector<Integer>();
a.add(arr[ 0 ]);
for ( int i = 1 ; i < n; i++) {
if (oppositeSigns(a.get( 0 ), arr[i]))
b.add(arr[i]);
else
a.add(arr[i]);
}
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);
}
}
|
Python3
def oppositeSigns(x, y):
return ((x ^ y) < 0 )
def partitionNegPos(arr, n):
a = []
b = []
a = a + [arr[ 0 ]]
for i in range ( 1 , n):
if (oppositeSigns(a[ 0 ], arr[i])):
b = b + [arr[i]]
else :
a = a + [arr[i]]
for i in range ( 0 , len (a)):
print (a[i], end = ' ' )
print ("")
for i in range ( 0 , len (b)):
print (b[i], end = ' ' )
arr = [ 1 , - 2 , 6 , - 7 , 8 ]
n = len (arr)
partitionNegPos(arr, n)
|
C#
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 >();
a.Add(arr[0]);
for ( int i = 1; i < n; i++) {
if (oppositeSigns(a[0], arr[i]))
b.Add(arr[i]);
else
a.Add(arr[i]);
}
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] + " " );
}
public static void Main()
{
int [] arr = { 1, -2, 6, -7, 8 };
int n = arr.Length;
partitionNegPos(arr, n);
}
}
|
Javascript
<script>
function oppositeSigns(x, y)
{
return ((x ^ y) < 0);
}
function partitionNegPos(arr, n)
{
var a = [], b = [];
a.push(arr[0]);
for ( var i = 1; i < n; i++) {
if (oppositeSigns(a[0], arr[i]))
b.push(arr[i]);
else
a.push(arr[i]);
}
for ( var i = 0; i < a.length; i++)
document.write( a[i] + ' ' );
document.write( "<br>" );
for ( var i = 0; i < b.length; i++)
document.write( b[i] + ' ' );
}
var arr = [1, -2, 6, -7, 8];
var n = arr.length;
partitionNegPos(arr, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Another approach : Using startswith()
Steps to solve the problem:
1. declare two vector po and ne to store the positive and negative element.
2. iterate through every element of the array as i:
* declare a string s and store i as string using to_string function.
* check if last occurrence if “-” in s from 0 is equal to 0 than push i in the ne.
* else push i in po.
3. iterate through i=0 until end in po:
* check if i == size of po -1 than print po[i] and break.
* else just print po[i].
4. iterate through i=0 until end in ne:
* check if i == size of ne -1 than print ne[i] and break.
* else just print ne[i].
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, -2, 6, -7, 8 };
vector< int > po, ne;
for ( int i : arr) {
string s = to_string(i);
if (s.rfind( "-" , 0) == 0) {
ne.push_back(i);
}
else
po.push_back(i);
}
cout << "[" ;
for ( int i = 0; i < po.size(); i++) {
if (i == po.size() - 1) {
cout << po[i] << "]\n" ;
break ;
}
cout << po[i] << ", " ;
}
cout << "[" ;
for ( int i = 0; i < ne.size(); i++) {
if (i == ne.size() - 1) {
cout << ne[i] << "]\n" ;
break ;
}
cout << ne[i] << ", " ;
}
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
int [] arr = { 1 , - 2 , 6 , - 7 , 8 };
List<Integer> po = new ArrayList<>();
List<Integer> ne = new ArrayList<>();
for ( int i : arr) {
if ((i + "" ).startsWith( "-" )) {
ne.add(i);
}
else
po.add(i);
}
System.out.println(po);
System.out.println(ne);
}
}
|
Python3
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)
|
C#
using System;
using System.Collections;
public class GFG {
static public void Main()
{
int [] arr = { 1, -2, 6, -7, 8 };
var po = new ArrayList();
var ne = new ArrayList();
foreach ( int i in arr)
{
if ((i + "" ).StartsWith( "-" )) {
ne.Add(i);
}
else {
po.Add(i);
}
}
Console.Write( "[" );
for ( int i = 0; i < po.Count; i++) {
if (i == po.Count - 1) {
Console.WriteLine(po[i] + "]" );
break ;
}
Console.Write(po[i] + ", " );
}
Console.Write( "[" );
for ( int i = 0; i < ne.Count; i++) {
if (i == ne.Count - 1) {
Console.WriteLine(ne[i] + "]" );
break ;
}
Console.Write(ne[i] + ", " );
}
}
}
|
Javascript
let arr = [1, -2, 6, -7, 8 ];
let po = [];
let ne = [];
for (const i of arr){
if ((i+ "" ).startsWith( "-" )){
ne.push(i);
} else po.push(i);
}
console.log(JSON.stringify(po));
console.log(JSON.stringify(ne));
|
Output
[1, 6, 8]
[-2, -7]
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...