📜  从给定数组生成的矩阵的子矩阵的按位XOR

📅  最后修改于: 2021-05-17 04:21:49             🧑  作者: Mango

给定的阵列ARR []的长度N,尺寸N * N的矩阵是在该阵列ARR定义[]其中,M I,J =常用3 I&ARRĴ。给定四个整数X,Y,ST ,任务是找到子矩阵从左上角(X,Y)右下角(S,T)的所有元素的按位XOR。

例子:

天真的方法:最简单的方法是从给定的数组生成矩阵M ,并计算M的给定子矩阵中存在的所有元素的按位XOR。

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

高效方法:想法是使用“ XOR”和“ AND”运算的以下分布属性:

因此,可以从下式计算子矩阵从左上角(X,Y)到右下角(S,T)的最终XOR:

  • 从索引YT遍历数组,然后计算元素的XOR
  • 从索引XS遍历数组,并计算元素的XOR。
  • 最后,计算所计算的XOR的按位与,等于从(X,Y)到(S,T)的子矩阵的按位XOR。

下面是上述方法的实现:

C++
// C++ program of the
// above approach
 
#include 
using namespace std;
 
// Function to find the submatrix
// XOR of the given matrix
int submatrix_xor(int* A, int N,
                  int X, int Y,
                  int S, int T)
{
    int left_xor = 0, i, right_xor = 0;
 
    // Calculating left xor
    // i.e A[Y]^A[Y+1]^. . .^A[T]
    for (i = Y; i <= T; i++) {
        left_xor ^= A[i];
    }
 
    // Calculating right xor
    // i.e A[X]^A[X+1]^. . .^A[S]
    for (i = X; i <= S; i++) {
        right_xor ^= A[i];
    }
 
    // Bitwise AND of left_xor and
    // right_xor gives required result
    return left_xor & right_xor;
}
 
// Driver Code
int main()
{
    int A[3] = { 2, 3, 4 }, X = 0,
        Y = 1, S = 2, T = 2, N = 3;
 
    // Printing xor of submatrix
    cout << submatrix_xor(A, N, X,
                          Y, S, T);
    return 0;
}


Java
// Java program of the
// above approach
import java.io.*;
 
class GFG{
  
// Function to find the submatrix
// XOR of the given matrix
static int submatrix_xor(int[] A, int N,
                         int X, int Y,
                         int S, int T)
{
    int left_xor = 0, i, right_xor = 0;
  
    // Calculating left xor
    // i.e A[Y]^A[Y+1]^. . .^A[T]
    for(i = Y; i <= T; i++)
    {
        left_xor ^= A[i];
    }
  
    // Calculating right xor
    // i.e A[X]^A[X+1]^. . .^A[S]
    for(i = X; i <= S; i++)
    {
        right_xor ^= A[i];
    }
  
    // Bitwise AND of left_xor and
    // right_xor gives required result
    return left_xor & right_xor;
}
  
// Driver Code
public static void main (String[] args)
{
    int[] A = { 2, 3, 4 };
    int X = 0, Y = 1, S = 2,
        T = 2, N = 3;
  
    // Printing xor of submatrix
    System.out.print(submatrix_xor(A, N, X,
                                   Y, S, T));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program of the
# above approach
 
# Function to find the submatrix
# XOR of the given matrix
def submatrix_xor(A, N, X, Y, S, T):
     
    left_xor = 0
    i = 0
    right_xor = 0
 
    # Calculating left xor
    # i.e A[Y]^A[Y+1]^. . .^A[T]
    for i in range(Y, T + 1):
        left_xor ^= A[i]
 
    # Calculating right xor
    # i.e A[X]^A[X+1]^. . .^A[S]
    for i in range(X, S + 1):
        right_xor ^= A[i]
 
    # Bitwise AND of left_xor and
    # right_xor gives required result
    return left_xor & right_xor
 
# Driver Code
if __name__ == '__main__':
     
    A = [ 2, 3, 4 ]
    X = 0
    Y = 1
    S = 2
    T = 2
    N = 3
 
    # Printing xor of submatrix
    print(submatrix_xor(A, N, X, Y, S, T))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach 
using System;
 
class GFG{
  
// Function to find the submatrix
// XOR of the given matrix
static int submatrix_xor(int[] A, int N,
                         int X, int Y,
                         int S, int T)
{
    int left_xor = 0, i, right_xor = 0;
  
    // Calculating left xor
    // i.e A[Y]^A[Y+1]^. . .^A[T]
    for(i = Y; i <= T; i++)
    {
        left_xor ^= A[i];
    }
  
    // Calculating right xor
    // i.e A[X]^A[X+1]^. . .^A[S]
    for(i = X; i <= S; i++)
    {
        right_xor ^= A[i];
    }
  
    // Bitwise AND of left_xor and
    // right_xor gives required result
    return left_xor & right_xor;
}
  
// Driver Code
public static void Main ()
{
    int[] A = { 2, 3, 4 };
    int X = 0, Y = 1, S = 2,
        T = 2, N = 3;
  
    // Printing xor of submatrix
    Console.Write(submatrix_xor(A, N, X,
                                Y, S, T));
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
5

时间复杂度: O(N),其中N是数组的大小
辅助空间: O(1)