📜  3D 矩阵的主要对角线元素的异或

📅  最后修改于: 2021-09-05 11:39:00             🧑  作者: Mango

给定一个由正整数组成的维度为N * N * N的 3D 矩阵mat[][][] ,任务是计算存在于主对角线上的所有矩阵元素的按位异或。

例子:

朴素方法:解决问题的最简单方法是使用三个嵌套循环遍历给定的 3D 矩阵mat[][][] ,使用变量,例如ijk ,并计算mat[i][ 的按位异或j][k]mat[i][j][N – k – 1] ,如果ijk的值相等。完成矩阵的遍历后,打印得到的Bitwise XOR的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate Bitwise XOR of
// major diagonal elements of 3D matrix
void findXOR(
    vector > >& mat,
    int N)
{
    // Stores the Bitwise XOR of
    // the major diagonal elements
    int XOR = 0;
 
    for (int i = 0; i < N; i++) {
 
        for (int j = 0; j < N; j++) {
 
            for (int k = 0; k < N; k++) {
 
                // If element is part
                // of major diagonal
                if ((i == j && j == k)) {
 
                    XOR ^= mat[i][j][k];
                    XOR ^= mat[i][j][N - k - 1];
                }
            }
        }
    }
 
    // Print the resultant Bitwise XOR
    cout << XOR << "\n";
}
 
// Driver Code
int main()
{
    vector > > mat
        = { { { 1, 2 }, { 3, 4 } },
            { { 5, 6 }, { 7, 8 } } };
    int N = mat.size();
 
    findXOR(mat, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to calculate Bitwise XOR of
// major diagonal elements of 3D matrix
static void findXOR(int mat[][][], int N)
{
     
    // Stores the Bitwise XOR of
    // the major diagonal elements
    int XOR = 0;
 
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            for(int k = 0; k < N; k++)
            {
                 
                // If element is part
                // of major diagonal
                if ((i == j && j == k))
                {
                    XOR ^= mat[i][j][k];
                    XOR ^= mat[i][j][N - k - 1];
                }
            }
        }
    }
 
    // Print the resultant Bitwise XOR
    System.out.println(XOR);
}
 
// Driver Code
public static void main(String[] args)
{
    int mat[][][] = { { { 1, 2 }, { 3, 4 } },
                      { { 5, 6 }, { 7, 8 } } };
    int N = mat.length;
 
    findXOR(mat, N);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to calculate Bitwise XOR of
# major diagonal elements of 3D matrix
def findXOR(mat, N):
     
    # Stores the Bitwise XOR of
    # the major diagonal elements
    XOR = 0
 
    for i in range(N):
        for j in range(N):
            for k in range(N):
                 
                # If element is part
                # of major diagonal
                if ((i == j and j == k)):
                    XOR ^= mat[i][j][k]
                    XOR ^= mat[i][j][N - k - 1]
                 
    # Print the resultant Bitwise XOR
    print(XOR)
 
# Driver Code
mat = [ [ [ 1, 2 ], [ 3, 4 ] ],
        [ [ 5, 6 ], [ 7, 8 ] ] ]
         
N = len(mat)
 
findXOR(mat, N)
 
# This code is contributed by splevel62


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate Bitwise XOR of
// major diagonal elements of 3D matrix
static void findXOR(int[, , ] mat, int N)
{
     
    // Stores the Bitwise XOR of
    // the major diagonal elements
    int XOR = 0;
 
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            for(int k = 0; k < N; k++)
            {
                 
                // If element is part
                // of major diagonal
                if ((i == j && j == k))
                {
                    XOR ^= mat[i, j, k];
                    XOR ^= mat[i, j, N - k - 1];
                }
            }
        }
    }
 
    // Print the resultant Bitwise XOR
    Console.WriteLine(XOR);
}
 
// Driver Code
public static void Main(string[] args)
{
    int[,,] mat = { { { 1, 2 }, { 3, 4 } },
                    { { 5, 6 }, { 7, 8 } } };
    int N = mat.GetLength(0);
 
    findXOR(mat, N);
}
}
 
// This code is contributed by ukasp


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the Bitwise XOR of
// both diagonal elements of 3D matrix
void findXOR(
    vector > >& mat,
    int N)
{
    // Stores the Bitwise XOR of the
    // major diagonal elements
    int XOR = 0;
 
    for (int i = 0; i < N; i++) {
 
        XOR ^= mat[i][i][i];
        XOR ^= mat[i][i][N - i - 1];
    }
 
    // Print the resultant Bitwise XOR
    cout << XOR << "\n";
}
 
// Driver Code
int main()
{
    vector > > mat
        = { { { 1, 2 }, { 3, 4 } },
            { { 5, 6 }, { 7, 8 } } };
    int N = mat.size();
 
    findXOR(mat, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to calculate Bitwise XOR of
// major diagonal elements of 3D matrix
static void findXOR(int mat[][][], int N)
{
     
    // Stores the Bitwise XOR of the
    // major diagonal elements
    int XOR = 0;
 
    for(int i = 0; i < N; i++)
    {
        XOR ^= mat[i][i][i];
        XOR ^= mat[i][i][N - i - 1];
    }
     
    // Print the resultant Bitwise XOR
    System.out.println(XOR);
}
 
// Driver Code
public static void main(String[] args)
{
    int mat[][][] = { { { 1, 2 }, { 3, 4 } },
                      { { 5, 6 }, { 7, 8 } } };
    int N = mat.length;
 
    findXOR(mat, N);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to find the Bitwise XOR of
# both diagonal elements of 3D matrix
def findXOR(mat, N):
     
    # Stores the Bitwise XOR of the
    # major diagonal elements
    XOR = 0
  
    for i in range(N):
        XOR ^= mat[i][i][i]
        XOR ^= mat[i][i][N - i - 1]
     
    # Print the resultant Bitwise XOR
    print(XOR)
 
# Driver Code
mat = [ [ [ 1, 2 ], [ 3, 4 ] ],
        [ [ 5, 6 ], [ 7, 8 ] ] ] 
N = len(mat)
  
findXOR(mat, N)
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate Bitwise XOR of
// major diagonal elements of 3D matrix
static void findXOR(int[,,] mat, int N)
{
     
    // Stores the Bitwise XOR of the
    // major diagonal elements
    int XOR = 0;
 
    for(int i = 0; i < N; i++)
    {
        XOR ^= mat[i, i, i];
        XOR ^= mat[i, i, N - i - 1];
    }
     
    // Print the resultant Bitwise XOR
    Console.Write(XOR);
}
 
// Driver Code
static public void Main ()
{
    int[,,] mat = { { { 1, 2 }, { 3, 4 } },
                    { { 5, 6 }, { 7, 8 } } };
    int N = mat.GetLength(0);
 
    findXOR(mat, N);
}
}
 
// This code is contributed by avijitmondal1998


Javascript


输出:
12

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

高效方法:上述方法可以通过使用索引ijk与对角线元素相同的元素来优化。因此,想法是使用变量i迭代索引[0, N – 1]的范围,并计算作为索引(i, i, i)(i )主要对角线一部分的所有元素的按位异或, i, N – i – 1)在给定的矩阵mat[][][] 中分别mat[i][i][i]mat[i][i][N – i – 1]

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the Bitwise XOR of
// both diagonal elements of 3D matrix
void findXOR(
    vector > >& mat,
    int N)
{
    // Stores the Bitwise XOR of the
    // major diagonal elements
    int XOR = 0;
 
    for (int i = 0; i < N; i++) {
 
        XOR ^= mat[i][i][i];
        XOR ^= mat[i][i][N - i - 1];
    }
 
    // Print the resultant Bitwise XOR
    cout << XOR << "\n";
}
 
// Driver Code
int main()
{
    vector > > mat
        = { { { 1, 2 }, { 3, 4 } },
            { { 5, 6 }, { 7, 8 } } };
    int N = mat.size();
 
    findXOR(mat, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to calculate Bitwise XOR of
// major diagonal elements of 3D matrix
static void findXOR(int mat[][][], int N)
{
     
    // Stores the Bitwise XOR of the
    // major diagonal elements
    int XOR = 0;
 
    for(int i = 0; i < N; i++)
    {
        XOR ^= mat[i][i][i];
        XOR ^= mat[i][i][N - i - 1];
    }
     
    // Print the resultant Bitwise XOR
    System.out.println(XOR);
}
 
// Driver Code
public static void main(String[] args)
{
    int mat[][][] = { { { 1, 2 }, { 3, 4 } },
                      { { 5, 6 }, { 7, 8 } } };
    int N = mat.length;
 
    findXOR(mat, N);
}
}
 
// This code is contributed by Kingash

蟒蛇3

# Python3 program for the above approach
 
# Function to find the Bitwise XOR of
# both diagonal elements of 3D matrix
def findXOR(mat, N):
     
    # Stores the Bitwise XOR of the
    # major diagonal elements
    XOR = 0
  
    for i in range(N):
        XOR ^= mat[i][i][i]
        XOR ^= mat[i][i][N - i - 1]
     
    # Print the resultant Bitwise XOR
    print(XOR)
 
# Driver Code
mat = [ [ [ 1, 2 ], [ 3, 4 ] ],
        [ [ 5, 6 ], [ 7, 8 ] ] ] 
N = len(mat)
  
findXOR(mat, N)
 
# This code is contributed by sanjoy_62

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate Bitwise XOR of
// major diagonal elements of 3D matrix
static void findXOR(int[,,] mat, int N)
{
     
    // Stores the Bitwise XOR of the
    // major diagonal elements
    int XOR = 0;
 
    for(int i = 0; i < N; i++)
    {
        XOR ^= mat[i, i, i];
        XOR ^= mat[i, i, N - i - 1];
    }
     
    // Print the resultant Bitwise XOR
    Console.Write(XOR);
}
 
// Driver Code
static public void Main ()
{
    int[,,] mat = { { { 1, 2 }, { 3, 4 } },
                    { { 5, 6 }, { 7, 8 } } };
    int N = mat.GetLength(0);
 
    findXOR(mat, N);
}
}
 
// This code is contributed by avijitmondal1998

Javascript


输出:
12

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live