Dada una array arr y un número K , encuentre la nueva array formada realizando XOR del elemento correspondiente de la array dada con el número K dado.
Ejemplos:
Entrada: arr[] = { 2, 4, 1, 3, 5 }, K = 5
Salida: 7 1 4 6 0
Explicación:
2 XOR 5 = 7
4 XOR 5 = 1
1 XOR 5 = 4
3 XOR 5 = 6
5 XOR 5 = 0
Entrada: arr[] = { 4, 75, 45, 42 }, K = 2
Salida: 6 73 47 40
Acercarse:
- Recorre la array dada .
- Luego calcula el XOR de cada elemento con K.
- Luego guárdelo como el elemento en ese índice en la array de salida.
- Imprime la array actualizada.
A continuación se muestra la implementación del enfoque anterior.
CPP
// C++ program to find XOR of every element // of an array with a given number K #include <bits/stdc++.h> using namespace std; // Function to construct new array void constructXORArray(int A[], int n, int K) { int B[n]; // Traverse the array and // compute XOR with K for (int i = 0; i < n; i++) B[i] = A[i] ^ K; // Print new array for (int i = 0; i < n; i++) cout << B[i] << " "; cout << endl; } // Driver code int main() { int A[] = { 2, 4, 1, 3, 5 }; int K = 5; int n = sizeof(A) / sizeof(A[0]); constructXORArray(A, n, K); int B[] = { 4, 75, 45, 42 }; K = 2; n = sizeof(B) / sizeof(B[0]); constructXORArray(B, n, K); return 0; }
Java
// Java program to find XOR of every element // of an array with a given number K import java.util.*; class GFG { // Function to construct new array static void constructXORArray(int A[], int n, int K) { int[] B = new int[n]; // Traverse the array and // compute XOR with K for (int i = 0; i < n; i++) B[i] = A[i] ^ K; // Print new array for (int i = 0; i < n; i++) System.out.print( B[i] +" "); System.out.println(); } // Driver code public static void main(String args[]) { int A[] = { 2, 4, 1, 3, 5 }; int K = 5; int n = A.length; constructXORArray(A, n, K); int B[] = { 4, 75, 45, 42 }; K = 2; n = B.length; constructXORArray(B, n, K); } } // This code is contributed by shivanisinghss2110
Python3
# Python program to find XOR of every element # of an array with a given number K # Function to construct new array def constructXORArray(A, n, K): B = [0]*n; # Traverse the array and # compute XOR with K for i in range(n): B[i] = A[i] ^ K; # Print new array for i in range(n): print(B[i], end=" "); print(); # Driver code if __name__ == '__main__': A = [ 2, 4, 1, 3, 5 ]; K = 5; n = len(A); constructXORArray(A, n, K); B = [ 4, 75, 45, 42 ]; K = 2; n = len(B); constructXORArray(B, n, K); # This code contributed by sapnasingh4991
C#
// C# program to find XOR of every element // of an array with a given number K using System; class GFG { // Function to construct new array static void constructXORArray(int []A, int n, int K) { int[] B = new int[n]; // Traverse the array and // compute XOR with K for (int i = 0; i < n; i++) B[i] = A[i] ^ K; // Print new array for (int i = 0; i < n; i++) Console.Write( B[i] +" "); Console.WriteLine(); } // Driver code public static void Main(String []args) { int []A = { 2, 4, 1, 3, 5 }; int K = 5; int n = A.Length; constructXORArray(A, n, K); int []B = { 4, 75, 45, 42 }; K = 2; n = B.Length; constructXORArray(B, n, K); } } // This code is contributed by Rajput-Ji
Javascript
<script> // Javascript program to find XOR of every element // of an array with a given number K // Function to construct new array function constructXORArray(A, n, K) { let B = new Array(n); // Traverse the array and // compute XOR with K for (let i = 0; i < n; i++) B[i] = A[i] ^ K; // Print new array for (let i = 0; i < n; i++) document.write(B[i] + " "); document.write("</br>") } let A = [ 2, 4, 1, 3, 5 ]; let K = 5; let n = A.length; constructXORArray(A, n, K); let B = [ 4, 75, 45, 42 ]; K = 2; n = B.length; constructXORArray(B, n, K); // This code is contributed by divyeshrabadiya07. </script>
Producción:
7 1 4 6 0 6 73 47 40
Complejidad temporal: O(n)
Espacio auxiliar: O(n)