# XOR of major diagonal elements of a 3D Matrix

Given a 3D matrix mat[][][] of dimensions N * N * N consisting of positive integers, the task is to calculate Bitwise XOR of all matrix elements present in the major diagonal.

Examples:

Input: arr[][][] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}Output: 12Explanation: The major diagonal elements are {1, 8, 2, 7}. Therefore, the Bitwise XOR of all these elements is 12.

Input: arr[][][]={{{1}}}Output: 0Explanation: There 2 major diagonals of the matrix are {1}, {1}. Therefore, the Bitwise XOR of the major diagonal elements is 0.

Naive Approach: The simplest approach top solve the problem is to traverse the given 3D matrix mat[][][] using three nested loops, using variables, say i, j, and k, and calculate Bitwise XOR of mat[i][j][k] and mat[i][j][N – k – 1], if the value of i, j, and k are equal. After completing the traversal of the matrix, print the value of Bitwise XOR obtained.Below is the implementation of the above approach:

C++

#include

using namespace std;

void findXOR(

vector& mat,

int N)

{

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 ((i == j && j == k)) { XOR ^= mat[i][j][k]; XOR ^= mat[i][j][N - k - 1]; } } } } cout