📜  查找其元素是给定数组中相邻元素的 XOR 的数组

📅  最后修改于: 2022-05-13 01:56:05.210000             🧑  作者: Mango

查找其元素是给定数组中相邻元素的 XOR 的数组

给定一个由N个整数组成的数组arr[] ,任务是重新构造一个数组arr[] ,使得arr [] 中的值通过对数组中的相邻元素进行异或得到。打印数组元素。

例子:

方法:解决给定问题的主要思想是执行以下步骤:

  1. 从第 0索引到第 (N – 2) 个索引遍历给定数组arr[]
  2. 对于第 i 个位置的每个元素 arr[i],计算arr[i] ^ arr[i+1]并将其存储在位置 i。

下面是上述方法的实现:

C++
// C++ implementation
// of the above approach
#include 
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


输出
1 10 3 1 3 

时间复杂度: O(N)
辅助空间: O(1)