Dadas dos arrays ordenadas A[] y B[] que constan de N enteros distintos, la tarea es reorganizar los elementos de la array B[] de modo que, para cada i -ésimo índice , A[i] no sea igual a B[i] . Si existen varios arreglos de este tipo, imprima cualquiera de ellos. Si no existe tal arreglo, imprima -1 .
Ejemplos:
Entrada: A[] = {2, 4, 5, 8}, B[] = {2, 4, 5, 8}
Salida: 4 2 8 5
Explicación:
Los arreglos posibles que satisfacen las condiciones requeridas son {4, 2, 8, 5}, {8, 5, 4, 2} y {8, 5, 4, 2}.Entrada: A[] = {1, 3, 4, 5}, B[] = {2, 4, 6, 7}
Salida: 7 6 2 4
Enfoque ingenuo: el enfoque más simple es encontrar todas las permutaciones posibles de la array B[] e imprimir cualquier permutación entre ellas de modo que, para cada i -ésimo índice, A[i]) no sea igual a B[i] .
Complejidad temporal: O(N*N!)
Espacio auxiliar: O(N)
Enfoque eficiente: para optimizar el enfoque anterior, la idea es usar un enfoque codicioso para encontrar el arreglo requerido de la array B[] usando la condición de que ambas arrays constan de N elementos distintos en orden ascendente. Siga los pasos a continuación para resolver el problema:
- Invierta la array dada B[] .
- Si N es 1 y A[0] = B[0] , imprima -1 .
- De lo contrario, itere sobre las arrays y verifique si A[i] es igual a B[i] o no.
- Si A[i] es igual a B[i] , intercambie B[i] con B[i+1] y rompa el bucle.
- Después de los pasos anteriores, imprima la array B[] .
A continuación se muestra la implementación del enfoque anterior:
C++14
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the arrangement // of array B[] such that element at // each index of A[] and B[] are not equal void RearrangeB(int A[], vector<int> B, int n) { // Print not possible, if arrays // only have single equal element if (n == 1 && A[0] == B[0]) { cout << "-1" << endl; return; } // Reverse array B for(int i = 0; i < n / 2; i++) { int t = B[i]; B[i] = B[n - i - 1]; B[n - i - 1] = t; } // Traverse over arrays to check // if there is any index where // A[i] and B[i] are equal for(int i = 0; i < n - 1; i++) { // Swap B[i] with B[i - 1] if (B[i] == A[i]) { int t = B[i + 1]; B[i + 1] = B[i]; B[i] = t; // Break the loop break; } } // Print required arrangement // of array B for(int k : B) cout << k << " "; } // Driver Code int main() { // Given arrays A[] and B[] int A[] = { 2, 4, 5, 8 }; vector<int> B = { 2, 4, 5, 8 }; // Length of array A[] int n = sizeof(A) / sizeof(A[0]); // Function call RearrangeB(A, B, n); } // This code is contributed by sanjoy_62
Java
// Java program for the above approach import java.util.*; import java.lang.*; class GFG { // Function to find the arrangement // of array B[] such that element at // each index of A[] and B[] are not equal static void RearrangeB(int[] A, int[] B) { // Length of array int n = A.length; // Print not possible, if arrays // only have single equal element if (n == 1 && A[0] == B[0]) { System.out.println("-1"); return; } // Reverse array B for (int i = 0; i < n / 2; i++) { int t = B[i]; B[i] = B[n - i - 1]; B[n - i - 1] = t; } // Traverse over arrays to check // if there is any index where // A[i] and B[i] are equal for (int i = 0; i < n - 1; i++) { // Swap B[i] with B[i - 1] if (B[i] == A[i]) { int t = B[i + 1]; B[i + 1] = B[i]; B[i] = t; // Break the loop break; } } // Print required arrangement // of array B for (int k : B) System.out.print(k + " "); } // Driver Code public static void main(String[] args) { // Given arrays A[] and B[] int[] A = { 2, 4, 5, 8 }; int[] B = { 2, 4, 5, 8 }; // Function Call RearrangeB(A, B); } }
Python3
# Python3 program for the above approach # Function to find the arrangement # of array B[] such that element at # each index of A[] and B[] are not equal def RearrangeB(A, B): # Length of array n = len(A) # Print not possible, if arrays # only have single equal element if (n == 1 and A[0] == B[0]): print(-1) return # Reverse array B for i in range(n // 2): t = B[i] B[i] = B[n - i - 1] B[n - i - 1] = t # Traverse over arrays to check # if there is any index where # A[i] and B[i] are equal for i in range(n - 1): # Swap B[i] with B[i - 1] if (B[i] == A[i]): B[i], B[i - 1] = B[i - 1], B[i] break # Print required arrangement # of array B for k in B: print(k, end = " ") # Driver Code # Given arrays A[] and B[] A = [ 2, 4, 5, 8 ] B = [ 2, 4, 5, 8 ] # Function call RearrangeB(A, B) # This code is contributed by Shivam Singh
C#
// C# program for // the above approach using System; class GFG{ // Function to find the arrangement // of array []B such that element at // each index of []A and []B // are not equal static void RearrangeB(int[] A, int[] B) { // Length of array int n = A.Length; // Print not possible, if arrays // only have single equal element if (n == 1 && A[0] == B[0]) { Console.WriteLine("-1"); return; } // Reverse array B for (int i = 0; i < n / 2; i++) { int t = B[i]; B[i] = B[n - i - 1]; B[n - i - 1] = t; } // Traverse over arrays to check // if there is any index where // A[i] and B[i] are equal for (int i = 0; i < n - 1; i++) { // Swap B[i] with B[i - 1] if (B[i] == A[i]) { int t = B[i + 1]; B[i + 1] = B[i]; B[i] = t; // Break the loop break; } } // Print required arrangement // of array B foreach (int k in B) Console.Write(k + " "); } // Driver Code public static void Main(String[] args) { // Given arrays []A and []B int[] A = {2, 4, 5, 8}; int[] B = {2, 4, 5, 8}; // Function Call RearrangeB(A, B); } } // This code is contributed by 29AjayKumar
Javascript
<script> // javascript program for the // above approach // Function to find the arrangement // of array B[] such that element at // each index of A[] and B[] are not equal function RearrangeB( A, B) { // Length of array let n = A.length; // Print not possible, if arrays // only have single equal element if (n == 1 && A[0] == B[0]) { document.write("-1"); return; } // Reverse array B for (let i = 0; i < n / 2; i++) { let t = B[i]; B[i] = B[n - i - 1]; B[n - i - 1] = t; } // Traverse over arrays to check // if there is any index where // A[i] and B[i] are equal for (let i = 0; i < n - 1; i++) { // Swap B[i] with B[i - 1] if (B[i] == A[i]) { let t = B[i + 1]; B[i + 1] = B[i]; B[i] = t; // Break the loop break; } } // Print required arrangement // of array B for (let k in B) document.write(B[k] + " "); } // Driver Code // Given arrays A[] and B[] let A = [ 2, 4, 5, 8 ]; let B = [ 2, 4, 5, 8 ]; // Function Call RearrangeB(A, B); // This code is contributed by avijitmondal1998. </script>
8 5 4 2
Complejidad temporal: O(N)
Espacio auxiliar: O(1)