Dada una array arr[] que consiste en N enteros, la tarea es reconstruir una array arr[] de modo que los valores en arr[] se obtengan haciendo XOR de los elementos adyacentes en la array. Imprime los elementos de la array.
Ejemplos:
Entrada: arr[ ] = {10, 11, 1, 2, 3}
Salida: 1 10 3 1 3
Explicación:
En el índice 0, arr[0] xor arr[1] = 1
En el índice 1, arr[1] xor arr[2] = 10
En el índice 2, arr[2] xor arr[3] = 3
…
En el índice 4, No queda ningún elemento Por lo tanto, permanecerá como está.
Nueva array será {1, 10, 3, 1, 3}Entrada: arr[ ] = {5, 9, 7, 6}
Salida: 12 14 1 6
Explicación:
En el índice 0, arr[0] xor arr[1] = 12
En el índice 1, arr[1] xor arr[2 ] = 14
En el índice 2, arr[2] xor arr[3] = 1
En el índice 3, No queda ningún elemento Por lo tanto, permanecerá como está.
Nueva array será {12, 14, 1, 6}
Planteamiento: La idea principal para resolver el problema planteado es realizar los siguientes pasos:
- Atraviese la array dada arr[] desde el índice 0 hasta el (N – 2) índice.
- Para cada elemento arr[i] en la i-ésima posición, calcule arr[i] ^ arr[i+1] y guárdelo en la posición i.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation // of the above approach #include <iostream> using namespace std; // Function to reconstruct the array // arr[] with xor of adjacent elements int* game_with_number(int arr[], int n) { // Iterate through each element for (int i = 0; i < n - 1; i++) { // Store the xor of current // and next element in arr[i] arr[i] = arr[i] ^ arr[i + 1]; } return arr; } // Function to print the array void print(int arr[], int n) { for (int i = 0; i < n; i++) { cout << arr[i] << " "; } } // Driver Code int main() { // Inputs int arr[] = { 10, 11, 1, 2, 3 }; // Length of the array given int n = sizeof(arr) / sizeof(arr[0]); // Function call to reconstruct the arr[] int* new_arr = game_with_number(arr, n); // Function call to print arr[] print(new_arr, n); }
Java
// Java implementation // of the above approach import java.io.*; class GFG{ // Function to reconstruct the array // arr[] with xor of adjacent elements static int[] game_with_number(int arr[], int n) { // Iterate through each element for(int i = 0; i < n - 1; i++) { // Store the xor of current // and next element in arr[i] arr[i] = arr[i] ^ arr[i + 1]; } return arr; } // Function to print the array static void print(int arr[], int n) { for(int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } // Driver Code public static void main(String[] args) { // Inputs int arr[] = { 10, 11, 1, 2, 3 }; // Length of the array given int n = arr.length; // Function call to reconstruct the arr[] int[] new_arr = game_with_number(arr, n); // Function call to print arr[] print(new_arr, n); } } // This code is contributed by subhammahato348
Python3
# Python3 implementation # of the above approach # Function to reconstruct the array # arr[] with xor of adjacent elements def game_with_number(arr, n): # Iterate through each element for i in range(n-1): # Store the xor of current #and next element in arr[i] arr[i] = arr[i] ^ arr[i + 1] return arr # Function to print array def printt(arr, n): print(*arr) # Driver Code if __name__ == '__main__': # Inputs arr= [10, 11, 1, 2, 3] # Length of the array given n = len(arr) # Function call to reconstruct the arr[] new_arr = game_with_number(arr, n); # Function call to prarr[] printt(new_arr, n) # This code is contributed by mohit kumar 29.
C#
// C# program for the above approach using System; class GFG{ // Function to reconstruct the array // arr[] with xor of adjacent elements static int[] game_with_number(int[] arr, int n) { // Iterate through each element for(int i = 0; i < n - 1; i++) { // Store the xor of current // and next element in arr[i] arr[i] = arr[i] ^ arr[i + 1]; } return arr; } // Function to print the array static void print(int[] arr, int n) { for(int i = 0; i < n; i++) { Console.Write(arr[i] + " "); } } // Driver Code public static void Main() { // Inputs int[] arr = { 10, 11, 1, 2, 3 }; // Length of the array given int n = arr.Length; // Function call to reconstruct the arr[] int[] new_arr = game_with_number(arr, n); // Function call to print arr[] print(new_arr, n); } } // This code is contributed by target_2.
Javascript
<script> // Javascript program for the above approach // Function to reconstruct the array // arr[] with xor of adjacent elements function game_with_number(arr,n) { // Iterate through each element for (let i = 0; i < n - 1; i++) { // Store the xor of current // and next element in arr[i] arr[i] = arr[i] ^ arr[i + 1]; } return arr; } // Function to print the array function print(arr,n) { for (let i = 0; i < n; i++) { document.write(arr[i]+" "); } } // Driver Code //Inputs let arr = [10, 11, 1, 2, 3 ]; // Length of the array given let n = arr.length; // Function call to reconstruct the arr[] let new_arr = game_with_number(arr, n); // Function call to print arr[] print(new_arr, n); // This code is contributed by // Potta Lokesh </script>
1 10 3 1 3
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por UtkarshPandey6 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA