📌  相关文章
📜  重新排列数组以使两个数组的相似索引元素的按位XOR相同

📅  最后修改于: 2021-05-17 03:15:12             🧑  作者: Mango

给定两个阵列A []B []N个整数的(N为奇数),则任务是重新排列阵列B [],使得对于每个1≤I≤N,A [i]B的按位XOR [我]相同。如果无法进行这种重新排列,请打印“ -1” 。否则,请打印重新排列。

例子:

天真的方法:最简单的方法是生成数组B []的所有可能排列,并打印其按位XOR与对应元素相同的数组组合。

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

高效方法:想法是使用按位XOR的可交换属性。步骤如下:

  • 找到两个数组元素的按位XOR,让其值为xor_value
  • 将数组B []的元素的频率存储在映射M中
  • 要重新排列B []的数组元素,请遍历数组B []并执行以下操作:
    • 将此数组的当前元素更新为A [i] ^ xor_value
    • 如果更新的当前元素的频率大于1,则将其递减。
    • 否则,就没有这种可能的安排,请跳出循环并打印“ -1”
  • 完成上述步骤后,打印数组B [],因为它包含重新排列的数组。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to rearrange the array
// B[] such that A[i] ^ B[i] is same
// for each element
vector rearrangeArray(
    vector& A, vector& B, int N)
{
    // Store frequency of elements
    map m;
 
    // Stores xor value
    int xor_value = 0;
 
    for (int i = 0; i < N; i++) {
 
        // Taking xor of all the
        // values of both arrays
        xor_value ^= A[i];
        xor_value ^= B[i];
 
        // Store frequency of B[]
        m[B[i]]++;
    }
 
    for (int i = 0; i < N; i++) {
 
        // Find the array B[] after
        // rearrangement
        B[i] = A[i] ^ xor_value;
 
        // If the updated value is
        // present then decrement
        // its frequency
        if (m[B[i]]) {
            m[B[i]]--;
        }
 
        // Otherwise return empty vector
        else
            return vector{};
    }
 
    return B;
}
 
// Utility function to rearrange the
// array B[] such that A[i] ^ B[i]
// is same for each element
void rearrangeArrayUtil(
    vector& A, vector& B, int N)
{
    // Store rearranged array B
    vector ans
        = rearrangeArray(A, B, N);
 
    // If rearrangement possible
    if (ans.size()) {
        for (auto x : ans) {
            cout << x << " ";
        }
    }
 
    // Otherwise return -1
    else {
        cout << "-1";
    }
}
 
// Driver Code
int main()
{
    // Given vector A
    vector A = { 13, 21, 33, 49, 53 };
 
    // Given vector B
    vector B = { 54, 50, 34, 22, 14 };
 
    // Size of the vector
    int N = (int)A.size();
 
    // Function Call
    rearrangeArrayUtil(A, B, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to rearrange the array
// B[] such that A[i] ^ B[i] is same
// for each element
static ArrayList rearrangeArray(
       ArrayList A,
       ArrayList B, int N)
{
     
    // Store frequency of elements
    HashMap m = new HashMap();
 
    // Stores xor value
    int xor_value = 0;
 
    for(int i = 0; i < N; i++)
    {
         
        // Taking xor of all the
        // values of both arrays
        xor_value ^= A.get(i);
        xor_value ^= B.get(i);
 
        // Store frequency of B[]
        m.put(B.get(i),
              m.getOrDefault(B.get(i) + 1, 0));
    }
 
    for(int i = 0; i < N; i++)
    {
         
        // Find the array B[] after
        // rearrangement
        B.set(i, A.get(i) ^ xor_value);
 
        // If the updated value is
        // present then decrement
        // its frequency
        if (m.getOrDefault(B.get(i), -1) != -1)
        {
            m.put(B.get(i),
                  m.getOrDefault(B.get(i), 0) - 1);
        }
 
        // Otherwise return empty vector
        else
            return (new ArrayList());
    }
    return B;
}
 
// Utility function to rearrange the
// array B[] such that A[i] ^ B[i]
// is same for each element
static void rearrangeArrayUtil(ArrayList A,
                               ArrayList B, int N)
{
     
    // Store rearranged array B
    ArrayList ans = rearrangeArray(A, B, N);
 
    // If rearrangement possible
    if (ans.size() != 0)
    {
        for(int i = 0; i < ans.size(); i++)
        {
            System.out.print(ans.get(i) + " ");
        }
    }
 
    // Otherwise return -1
    else
    {
        System.out.println("-1");
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given vector A
    ArrayList A = new ArrayList(
        Arrays.asList(13, 21, 33, 49, 53));
 
    // Given vector B
    ArrayList B = new ArrayList(
        Arrays.asList(54, 50, 34, 22, 14));
 
    // Size of the vector
    int N = (int)A.size();
 
    // Function Call
    rearrangeArrayUtil(A, B, N);
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
 
# Function to rearrange the array
# B[] such that A[i] ^ B[i] is same
# for each element
def rearrangeArray(A, B, N):
   
  # Store frequency of elements
  m = {}
 
  # Stores xor value
  xor_value = 0
 
  for i in range(0, N):
     
    # Taking xor of all the
    # values of both arrays
    xor_value ^= A[i]
    xor_value ^= B[i]
 
    # Store frequency of B[]
    if B[i] in m:
      m[B[i]] = m[B[i]] + 1
    else:
      m[B[i]] = 1
     
  for i in range(0, N):
     
    # Find the array B[] after
    # rearrangement
    B[i] = A[i] ^ xor_value
 
    # If the updated value is
    # present then decrement
    # its frequency
    if B[i] in m:
      m[B[i]] = m[B[i]] - 1
       
    # Otherwise return empty vector
    else:
      X = []
      return X
 
  return B
 
# Utility function to rearrange the
# array B[] such that A[i] ^ B[i]
# is same for each element
def rearrangeArrayUtil(A, B, N):
   
  # Store rearranged array B
  ans = rearrangeArray(A, B, N)
   
  # If rearrangement possible
  if (len(ans) > 0):
     for x in ans:
        print(x, end = ' ')
         
  # Otherwise return -1
  else:
    print("-1")
     
# Driver Code
if __name__ == '__main__':
   
  # Given vector A
  A = [ 13, 21, 33, 49, 53 ]
 
  # Given vector B
  B = [ 54, 50, 34, 22, 14 ]
 
  # Size of the vector
  N = len(A)
 
  # Function Call
  rearrangeArrayUtil(A, B, N)
   
# This code is contributed by akhilsaini


C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
 
// Function to rearrange the array
// B[] such that A[i] ^ B[i] is same
// for each element
static ArrayList rearrangeArray(ArrayList A,
                                ArrayList B, int N)
{
     
    // Store frequency of elements
    Dictionary m = new Dictionary();
 
    // Stores xor value
    int xor_value = 0;
 
    for(int i = 0; i < N; i++)
    {
         
        // Taking xor of all the
        // values of both arrays
        xor_value ^= (int)A[i];
        xor_value ^= (int)B[i];
 
        // Store frequency of B[]
        if (!m.ContainsKey((int)B[i]))
            m.Add((int)B[i], 1);
        else
            m[(int)B[i]] = m[(int)B[i]] + 1;
    }
 
    for(int i = 0; i < N; i++)
    {
         
        // Find the array B[] after
        // rearrangement
        B[i] = (int)A[i] ^ xor_value;
 
        // If the updated value is
        // present then decrement
        // its frequency
        if (m.ContainsKey((int)B[i]))
        {
            m[(int)B[i]]--;
        }
 
        // Otherwise return empty vector
        else
            return (new ArrayList());
    }
    return B;
}
 
// Utility function to rearrange the
// array B[] such that A[i] ^ B[i]
// is same for each element
static void rearrangeArrayUtil(ArrayList A,
                               ArrayList B,
                               int N)
{
     
    // Store rearranged array B
    ArrayList ans = rearrangeArray(A, B, N);
 
    // If rearrangement possible
    if (ans.Count != 0)
    {
        for(int i = 0; i < ans.Count; i++)
        {
            Console.Write(ans[i] + " ");
        }
    }
     
    // Otherwise return -1
    else
    {
        Console.WriteLine("-1");
    }
}
 
// Driver Code
public static void Main()
{
     
    // Given vector A
    ArrayList A = new ArrayList{ 13, 21, 33, 49, 53 };
 
    // Given vector B
    ArrayList B = new ArrayList{ 54, 50, 34, 22, 14 };
 
    // Size of the vector
    int N = A.Count;
 
    // Function Call
    rearrangeArrayUtil(A, B, N);
}
}
 
// This code is contributed by akhilsaini


输出:
14 22 34 50 54



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