Dada una array de n enteros, tanto negativos como positivos, divídalos en dos arrays diferentes sin comparar ningún elemento con 0.
Ejemplos:
Input : arr[] = [1, -2, 6, -7, 8] Output : a[] = [1, 6, 8] b[] = [-2, -7]
Algoritmo:
- Inicialice dos vectores vacíos. Empuje el primer elemento de la array en cualquiera de los dos vectores. Supongamos el primer vector. Que se denote por x.
- Para cualquier otro elemento, arr[1] a arr[n-1], verifique si su signo y el signo de x son iguales o no. Si los signos son los mismos, empuje el elemento en el mismo vector. De lo contrario, empuje el elemento en el otro vector.
- Después de completar el recorrido de los dos vectores, imprima ambos vectores.
¿Cómo comprobar si sus signos son opuestos o no?
Deje que los números enteros se verifiquen para ser denotados por x e y. El bit de signo es 1 en números negativos y 0 en números positivos. El XOR de xey tendrá el bit de signo 1 si y solo si tienen signos opuestos. En otras palabras, el XOR de xey será un número negativo si y solo si xey tienen signos opuestos.
CPP
// CPP program to rearrange positive and // negative numbers without comparison // with 0. #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; // Push first element to a. a.push_back(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[0], 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[0]); 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<Integer> a = new Vector<Integer>(); Vector<Integer> b = new Vector<Integer>(); // 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[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[0], 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
<script> // Javascript program to rearrange positive and // negative numbers without comparison // with 0. function oppositeSigns(x, y) { return ((x ^ y) < 0); } function partitionNegPos(arr, n) { var a = [], b = []; // Push first element to a. a.push(arr[0]); // Now put all elements of same sign // in a[] and opposite sign in b[] for (var i = 1; i < n; i++) { if (oppositeSigns(a[0], arr[i])) b.push(arr[i]); else a.push(arr[i]); } // Print a[] and b[] 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>
Producción:
1 6 8 -2 -7
Publicación traducida automáticamente
Artículo escrito por aganjali10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA