Dada una array binaria arr[N] , (donde N = 2 ) de tamaño dos que tiene al menos un elemento como cero. La tarea es escribir una función de una sola línea para establecer ambos elementos de la array en cero. Hay una restricción para escribir la función. No se puede utilizar el operador ternario ni la asignación directa de elementos.
Según las restricciones del problema, solo son posibles tres combinaciones de elementos de array:
- array[0] = 1 y array[1] = 0
- array[0] = 0 y array[1] = 1
- array[0] = 0 y array[1] = 0
En este artículo se describen los siguientes métodos:
- Usando solo el operador de asignación.
- Usando el operador de asignación dos veces.
- operador de negación (!) (NO lógico).
Comencemos discutiendo cada uno de estos métodos en detalle.
1. Usando solo el operador de asignación:
El operador de asignación se puede usar para establecer ambos elementos de la array binaria dada en 0, pero en este enfoque, los índices no se usan directamente.
Acercarse:
Hay tres formas de lograr esto:
1. arr[arr[1]] = arr[arr[0]]
Si arr={0, 1}, entonces arr[0] se asignará a arr[1].
Si arr={1, 0}, entonces arr[1] se asignará a arr[0].2. arr[arr[1]] = 0
Si arr[1]=0, entonces arr[0] será 1, entonces arr[arr[1]] hará que arr[0]=0.
Si arr[1]=1, entonces arr[1] será 1, entonces arr [ arr[1]] hará que arr[1]=0.3. arr[1 – arr[0]] = arr[1 – arr[1]]
Si arr[1]=0 y arr[0]=1, entonces 1-arr[1] será 1, entonces arr[ 1] se asignará a arr[0].
Si arr[1]=1 y arr[0]=0, entonces 1-arr[1] será 0, por lo que arr[0] se asignará a arr[1].
A continuación se muestra el código C++ para implementar el enfoque:
C++
// C++ program to set both elements // to 0 in binary array[2]. #include <iostream> using namespace std; void MakeBothZeros(int arr[]) { arr[arr[1]] = arr[arr[0]]; // Two other approaches to solve // the problem // arr[arr[1]] = 0; // arr[1 - arr[0]] = arr[1 - arr[1]]; } // Driver code int main() { int First_Arr[] = {0, 1}; MakeBothZeros(First_Arr); cout << First_Arr[0] << " " << First_Arr[1] << endl; int Second_Arr[] = {1, 0}; MakeBothZeros(Second_Arr); cout << Second_Arr[0] << " " << Second_Arr[1] << endl; int Thrd_Arr[] = {0, 0}; MakeBothZeros(Thrd_Arr); cout << Thrd_Arr[0] << " " << Thrd_Arr[1] << endl; return 0; }
Java
// Java program to set both elements // to 0 in binary array[2] import java.util.*; class GFG{ static void MakeBothZeros(int arr[]) { arr[arr[1]] = arr[arr[0]]; // Two other approaches to solve // the problem // arr[arr[1]] = 0; // arr[1 - arr[0]] = arr[1 - arr[1]]; } // Driver code public static void main(String[] args) { int First_Arr[] = {0, 1}; MakeBothZeros(First_Arr); System.out.print(First_Arr[0]+ " " + First_Arr[1] +"\n"); int Second_Arr[] = {1, 0}; MakeBothZeros(Second_Arr); System.out.print(Second_Arr[0]+ " " + Second_Arr[1] +"\n"); int Thrd_Arr[] = {0, 0}; MakeBothZeros(Thrd_Arr); System.out.print(Thrd_Arr[0]+ " " + Thrd_Arr[1] +"\n"); } } // This code is contributed by 29AjayKumar
Python3
# Python code for the above approach def MakeBothZeros(arr): arr[arr[1]] = arr[arr[0]] # Two other approaches to solve # the problem # arr[arr[1]] = 0; # arr[1 - arr[0]] = arr[1 - arr[1]]; # Driver code First_Arr = [0, 1] MakeBothZeros(First_Arr) print(f"{First_Arr[0]} {First_Arr[1]} ") Second_Arr = [1, 0] MakeBothZeros(Second_Arr) print(f"{Second_Arr[0]} {Second_Arr[1]} ") Thrd_Arr = [0, 0] MakeBothZeros(Thrd_Arr) print(f"{Thrd_Arr[0]} {Thrd_Arr[1]} ") # This code is contributed by GFGKING
C#
// C# program to set both elements // to 0 in binary array[2] using System; class GFG { static void MakeBothZeros(int[] arr) { arr[arr[1]] = arr[arr[0]]; // Two other approaches to solve // the problem // arr[arr[1]] = 0; // arr[1 - arr[0]] = arr[1 - arr[1]]; } // Driver code public static void Main() { int[] First_Arr = { 0, 1 }; MakeBothZeros(First_Arr); Console.WriteLine(First_Arr[0] + " " + First_Arr[1]); int[] Second_Arr = { 1, 0 }; MakeBothZeros(Second_Arr); Console.WriteLine(Second_Arr[0] + " " + Second_Arr[1]); int[] Thrd_Arr = { 0, 0 }; MakeBothZeros(Thrd_Arr); Console.WriteLine(Thrd_Arr[0] + " " + Thrd_Arr[1]); } } // This code is contributed by ukasp.
Javascript
<script> // JavaScript code for the above approach function MakeBothZeros(arr) { arr[arr[1]] = arr[arr[0]]; // Two other approaches to solve // the problem // arr[arr[1]] = 0; // arr[1 - arr[0]] = arr[1 - arr[1]]; } // Driver code let First_Arr = [0, 1]; MakeBothZeros(First_Arr); document.write(First_Arr[0] + " " + First_Arr[1] + "<br>"); let Second_Arr = [1, 0]; MakeBothZeros(Second_Arr); document.write(Second_Arr[0] + " " + Second_Arr[1] + '<br>'); let Thrd_Arr = [0, 0]; MakeBothZeros(Thrd_Arr); document.write(Thrd_Arr[0] + " " + Thrd_Arr[1] + '<br>'); // This code is contributed by Potta Lokesh </script>
0 0 0 0 0 0
Complejidad de tiempo: O(1), el código se ejecutará en un tiempo constante.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.
2. Usando el operador de asignación dos veces:
Como se indica en las restricciones, no se permite la asignación directa. Por lo tanto, arr[0]=0 y arr[1]=0 no son sentencias válidas. El operador de asignación se utilizará dos veces para poner a cero ambos elementos.
Acercarse:
Hay tres formas de lograr esto:
1. arr[0] = arr[1] = arr[0] & arr[1]
si cualquiera de los elementos es 1.
Y de 1 y 0 siempre es 0. Entonces, ambos obtienen el valor 0.2. arr[0] = arr[1] -= arr[1]
Si arr[1]=1 entonces arr[1] obtiene 1-1=0 entonces, ambos se convierten en 0.3. arr[1] = arr[0] -= arr[0]
Si arr[0]=1, entonces arr[0] obtiene 1-1=0.
else arr[0]= 0-0 = 0. Entonces, ambos se convierten en 0.
A continuación se muestra el programa C++ para implementar el enfoque:
C++
// C++ program to set both elements // to 0 in binary array[2]. #include <iostream> using namespace std; void MakeBothZeros(int arr[]) { arr[0] = arr[1] = arr[0] & arr[1]; // Two other approaches to solve // the problem // arr[0] = arr[1] -= arr[1]; // arr[1] = arr[0] -= arr[0]; } // Driver code int main() { int First_Arr[] = {0, 1}; MakeBothZeros(First_Arr); cout << First_Arr[0] << " " << First_Arr[1] << endl; int Second_Arr[] = {1, 0}; MakeBothZeros(Second_Arr); cout << Second_Arr[0] << " " << Second_Arr[1] << endl; int Thrd_Arr[] = {0, 0}; MakeBothZeros(Thrd_Arr); cout << Thrd_Arr[0] << " " << Thrd_Arr[1] << endl; return 0; }
Java
// Java program to set both elements // to 0 in binary array[2]. import java.util.*; class GFG{ static void MakeBothZeros(int arr[]) { arr[0] = arr[1] = arr[0] & arr[1]; // Two other approaches to solve // the problem // arr[0] = arr[1] -= arr[1]; // arr[1] = arr[0] -= arr[0]; } // Driver code public static void main(String[] args) { int First_Arr[] = {0, 1}; MakeBothZeros(First_Arr); System.out.print(First_Arr[0]+ " " + First_Arr[1] +"\n"); int Second_Arr[] = {1, 0}; MakeBothZeros(Second_Arr); System.out.print(Second_Arr[0]+ " " + Second_Arr[1] +"\n"); int Thrd_Arr[] = {0, 0}; MakeBothZeros(Thrd_Arr); System.out.print(Thrd_Arr[0]+ " " + Thrd_Arr[1] +"\n"); } } // This code is contributed by 29AjayKumar
Python3
# Python program to set both elements # to 0 in binary array[2]. def MakeBothZeros(arr): arr[0] = arr[1] = arr[0] & arr[1] # Two other approaches to solve # the problem # arr[0] = arr[1] -= arr[1]; # arr[1] = arr[0] -= arr[0]; # Driver code First_Arr = [0, 1] MakeBothZeros(First_Arr) print(First_Arr[0], end=" ") print(First_Arr[1]) Second_Arr = [0, 1] MakeBothZeros(Second_Arr) print(Second_Arr[0], end=" ") print(Second_Arr[1]) Thrd_Arr = [0, 0] MakeBothZeros(Thrd_Arr) print(Thrd_Arr[0], end=" ") print(Thrd_Arr[1]) # This code is contributed by Samim Hossain Mondal.
C#
// C# program to set both elements // to 0 in binary array[2]. using System; public class GFG { static void MakeBothZeros(int []arr) { arr[0] = arr[1] = arr[0] & arr[1]; // Two other approaches to solve // the problem // arr[0] = arr[1] -= arr[1]; // arr[1] = arr[0] -= arr[0]; } // Driver code public static void Main(String[] args) { int[] First_Arr = { 0, 1 }; MakeBothZeros(First_Arr); Console.Write(First_Arr[0] + " " + First_Arr[1] + "\n"); int []Second_Arr = { 1, 0 }; MakeBothZeros(Second_Arr); Console.Write(Second_Arr[0] + " " + Second_Arr[1] + "\n"); int []Thrd_Arr = { 0, 0 }; MakeBothZeros(Thrd_Arr); Console.Write(Thrd_Arr[0] + " " + Thrd_Arr[1] + "\n"); } } // This code is contributed by Rajput-Ji
Javascript
<script> // Javascript program to set both elements // to 0 in binary array[2]. function MakeBothZeros(arr) { arr[0] = arr[1] = arr[0] & arr[1] // Two other approaches to solve // the problem // arr[0] = arr[1] -= arr[1]; // arr[1] = arr[0] -= arr[0]; } // Driver code let First_Arr = [0, 1]; MakeBothZeros(First_Arr); document.write(First_Arr[0] + " " + First_Arr[1]); let Second_Arr = [1, 0]; MakeBothZeros(Second_Arr); document.write(Second_Arr[0] + " " + Second_Arr[1]); let Thrd_Arr = [0, 0]; MakeBothZeros(Thrd_Arr); document.write(Thrd_Arr[0] + " " + Thrd_Arr[1]); // This code is contributed by Samim Hossain Mondal. </script>
0 0 0 0 0 0
Complejidad de tiempo: O(1), el código se ejecutará en un tiempo constante.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.
Nota:
la complejidad del tiempo es O(1) ya que solo se usa una declaración.
3. Usando el operador de negación (!) (NO lógico):
En este enfoque, el operador de asignación se usa con un operador de negación para hacer que ambos elementos de la array dada sean 0 en una sola línea de código.
Acercarse:
Hay tres maneras de hacer esto:
1. arr[!arr[0]] = arr[arr[0]]
Si arr={0, 1} entonces al índice 1 se le asigna el valor del índice 0.
Si arr={1, 0} entonces el índice 0 recibe el valor del índice 1.2. arr[arr[1]] = arr[!arr[1]]
Si arr={0, 1} entonces el valor del índice 0 se asigna al índice 1.
Si arr={1, 0} entonces el valor del índice 1 es asignado al índice 0.3. arr[!arr[0]] = arr[!arr[1]]
Si arr={0, 1}, dado que 1 es el valor en el índice 1, el valor del índice 0, que vuelve a ser 0, se
asignará a índice 1, haciendo que la array esté llena de ceros.
Si arr={1, 0}, el valor del índice 1 se asigna al índice 0.
A continuación se muestra el programa C++ para implementar el enfoque:
C++
// C++ program to set both elements // to 0 in binary array[2]. #include <iostream> using namespace std; void MakeBothZeros(int arr[]) { arr[!arr[0]] = arr[arr[0]]; // Two other approaches to solve // the problem // arr[arr[1]] = arr[!arr[1]] // arr[!arr[0]] = arr[!arr[1]] } // Driver code int main() { int First_Arr[] = {0, 1}; MakeBothZeros(First_Arr); cout << First_Arr[0] << " " << First_Arr[1] << endl; int Second_Arr[] = {1, 0}; MakeBothZeros(Second_Arr); cout << Second_Arr[0] << " " << Second_Arr[1] << endl; int Thrd_Arr[] = {0, 0}; MakeBothZeros(Thrd_Arr); cout << Thrd_Arr[0] << " " << Thrd_Arr[1] << endl; return 0; }
Java
// Java program to set both elements // to 0 in binary array[2]. import java.util.*; class GFG{ static void MakeBothZeros(int arr[]) { int index = arr[0] == 0?1:0; arr[index] = arr[arr[0]]; // Two other approaches to solve // the problem // arr[arr[1]] = arr[!arr[1]] // arr[!arr[0]] = arr[!arr[1]] } // Driver code public static void main(String[] args) { int First_Arr[] = {0, 1}; MakeBothZeros(First_Arr); System.out.print(First_Arr[0]+ " " + First_Arr[1] +"\n"); int Second_Arr[] = {1, 0}; MakeBothZeros(Second_Arr); System.out.print(Second_Arr[0]+ " " + Second_Arr[1] +"\n"); int Thrd_Arr[] = {0, 0}; MakeBothZeros(Thrd_Arr); System.out.print(Thrd_Arr[0]+ " " + Thrd_Arr[1] +"\n"); } } // This code is contributed by shikhasingrajput
Python3
# Python program to set both elements to 0 in binary array[2]. def MakeBothZeros(arr): if (arr[0] == 0): index = 1 else: index = 0 arr[index] = arr[arr[0]] First_Arr = [0, 1] MakeBothZeros(First_Arr) print(First_Arr[0], end=" ") print(First_Arr[1]) Second_Arr = [0, 1] MakeBothZeros(Second_Arr) print(Second_Arr[0], end=" ") print(Second_Arr[1]) Thrd_Arr = [0, 0] MakeBothZeros(Thrd_Arr) print(Thrd_Arr[0], end=" ") print(Thrd_Arr[1]) # This code is contributed by lokesh (lokeshmvs21).
C#
// C# program to set both elements // to 0 in binary array[2]. using System; public class GFG{ static void MakeBothZeros(int []arr) { int index = arr[0] == 0?1:0; arr[index] = arr[arr[0]]; // Two other approaches to solve // the problem // arr[arr[1]] = arr[!arr[1]] // arr[!arr[0]] = arr[!arr[1]] } // Driver code public static void Main(String[] args) { int []First_Arr = {0, 1}; MakeBothZeros(First_Arr); Console.Write(First_Arr[0]+ " " + First_Arr[1] +"\n"); int []Second_Arr = {1, 0}; MakeBothZeros(Second_Arr); Console.Write(Second_Arr[0]+ " " + Second_Arr[1] +"\n"); int []Thrd_Arr = {0, 0}; MakeBothZeros(Thrd_Arr); Console.Write(Thrd_Arr[0]+ " " + Thrd_Arr[1] +"\n"); } } // This code is contributed by shikhasingrajput
Javascript
<script> // JavaScript program to set both elements // to 0 in binary array[2]. function MakeBothZeros(arr) { arr[Number(!arr[0])] = arr[arr[0]]; // Two other approaches to solve // the problem // arr[arr[1]] = arr[!arr[1]] // arr[!arr[0]] = arr[!arr[1]] } // Driver code let First_Arr = [0, 1]; MakeBothZeros(First_Arr); document.write(First_Arr[0] + " " + First_Arr[1],"</br>"); let Second_Arr = [1, 0]; MakeBothZeros(Second_Arr); document.write(Second_Arr[0] + " " + Second_Arr[1],"</br>") let Third_Arr = [0, 0]; MakeBothZeros(Third_Arr); document.write(Third_Arr[0] + " " + Third_Arr[1],"</br>") // This code is contributed by shinjanpatra </script>
0 0 0 0 0 0
Complejidad de tiempo: O(1), el código se ejecutará en un tiempo constante.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.
Publicación traducida automáticamente
Artículo escrito por pintusaini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA