Dadas dos arrays binarias arr1[] y arr2[] del mismo tamaño, la tarea es igualar ambas arrays intercambiando pares de arr1[ ] solo si arr1[i] = 0 y arr1[j] = 1 ( 0 ≤ yo < j < N) ). Si es posible hacer que ambas arrays sean iguales, imprima «Sí» . De lo contrario, escriba “No” .
Ejemplos:
Entrada: arr1[] = {0, 0, 1, 1}, arr2[] = {1, 1, 0, 0}
Salida: Sí
Explicación:
Intercambiar arr1[1] y arr1[3], se convierte en arr1[] = {0, 1, 1, 0}.
Intercambie arr1[0] y arr1[2], se convierte en arr1[] = {1, 1, 0, 0}.Entrada: arr1[] = {1, 0, 1, 0, 1}, arr2[] = {0, 1, 0, 0, 1}
Salida: No
Enfoque: siga los pasos a continuación para resolver el problema:
- Inicialice dos variables, digamos contar y marcar (= verdadero ).
- Recorra la array y para cada elemento de la array, realice las siguientes operaciones:
- Si arr1[i] != arr2[i]:
- Si arr1[i] == 0 , incrementa el conteo en 1 .
- De lo contrario, disminuya el conteo en 1 y si el conteo < 0 , actualice el indicador = falso .
- Si arr1[i] != arr2[i]:
- Si flag es igual a true , imprime “Sí” . De lo contrario, escriba “No” .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for above approach #include <bits/stdc++.h> using namespace std; // Function to check if two arrays // can be made equal or not by swapping // pairs of only one of the arrays void checkArrays(int arr1[], int arr2[], int N) { // Stores elements required // to be replaced int count = 0; // To check if the arrays // can be made equal or not bool flag = true; // Traverse the array for (int i = 0; i < N; i++) { // If array elements are not equal if (arr1[i] != arr2[i]) { if (arr1[i] == 0) // Increment count by 1 count++; else { // Decrement count by 1 count--; if (count < 0) { flag = 0; break; } } } } // If flag is true and count is 0, // print "Yes". Otherwise "No" if (flag && count == 0) cout << "Yes" << endl; else cout << "No" << endl; } // Driver Code int main() { // Given arrays int arr1[] = { 0, 0, 1, 1 }; int arr2[] = { 1, 1, 0, 0 }; // Size of the array int N = sizeof(arr1) / sizeof(arr1[0]); checkArrays(arr1, arr2, N); return 0; }
Java
// Java program for above approach public class GFG { // Function to check if two arrays // can be made equal or not by swapping // pairs of only one of the arrays static void checkArrays(int arr1[], int arr2[], int N) { // Stores elements required // to be replaced int count = 0; // To check if the arrays // can be made equal or not boolean flag = true; // Traverse the array for (int i = 0; i < N; i++) { // If array elements are not equal if (arr1[i] != arr2[i]) { if (arr1[i] == 0) // Increment count by 1 count++; else { // Decrement count by 1 count--; if (count < 0) { flag = false; break; } } } } // If flag is true and count is 0, // print "Yes". Otherwise "No" if ((flag && (count == 0)) == true) System.out.println("Yes"); else System.out.println("No"); } // Driver Code public static void main (String[] args) { // Given arrays int arr1[] = { 0, 0, 1, 1 }; int arr2[] = { 1, 1, 0, 0 }; // Size of the array int N = arr1.length; checkArrays(arr1, arr2, N); } } // This code is contributed by AnkThon
Python3
# Python3 program for above approach # Function to check if two arrays # can be made equal or not by swapping # pairs of only one of the arrays def checkArrays(arr1, arr2, N): # Stores elements required # to be replaced count = 0 # To check if the arrays # can be made equal or not flag = True # Traverse the array for i in range(N): # If array elements are not equal if (arr1[i] != arr2[i]): if (arr1[i] == 0): # Increment count by 1 count += 1 else: # Decrement count by 1 count -= 1 if (count < 0): flag = 0 break # If flag is true and count is 0, # pr"Yes". Otherwise "No" if (flag and count == 0): print("Yes") else: print("No") # Driver Code if __name__ == '__main__': # Given arrays arr1 = [0, 0, 1, 1] arr2 = [1, 1, 0, 0] # Size of the array N = len(arr1) checkArrays(arr1, arr2, N) # This code is contributed by mohit kumar 29.
C#
// C# program for the above approach using System; class GFG{ // Function to check if two arrays // can be made equal or not by swapping // pairs of only one of the arrays static void checkArrays(int[] arr1, int[] arr2, int N) { // Stores elements required // to be replaced int count = 0; // To check if the arrays // can be made equal or not bool flag = true; // Traverse the array for (int i = 0; i < N; i++) { // If array elements are not equal if (arr1[i] != arr2[i]) { if (arr1[i] == 0) // Increment count by 1 count++; else { // Decrement count by 1 count--; if (count < 0) { flag = false; break; } } } } // If flag is true and count is 0, // print "Yes". Otherwise "No" if ((flag && (count == 0)) == true) Console.WriteLine("Yes"); else Console.WriteLine("No"); } // Driver Code static public void Main() { // Given arrays int[] arr1 = { 0, 0, 1, 1 }; int[] arr2 = { 1, 1, 0, 0 }; // Size of the array int N = arr1.Length; checkArrays(arr1, arr2, N); } } // This code is contributed by susmitakundugoaldanga.
Javascript
<script> // Java script program for above approach // Function to check if two arrays // can be made equal or not by swapping // pairs of only one of the arrays function checkArrays(arr1,arr2,N) { // Stores elements required // to be replaced let count = 0; // To check if the arrays // can be made equal or not let flag = true; // Traverse the array for (let i = 0; i < N; i++) { // If array elements are not equal if (arr1[i] != arr2[i]) { if (arr1[i] == 0) // Increment count by 1 count++; else { // Decrement count by 1 count--; if (count < 0) { flag = false; break; } } } } // If flag is true and count is 0, // print "Yes". Otherwise "No" if ((flag && (count == 0)) == true) document.write("Yes"); else document.write("No"); } // Driver Code // Given arrays let arr1 = [ 0, 0, 1, 1 ]; let arr2 = [ 1, 1, 0, 0 ]; // Size of the array let N = arr1.length; checkArrays(arr1, arr2, N); // This code is contributed by Gottumukkala Sravan Kumar (171fa07058) </script>
Yes
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por harshityadav460 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA