XOR of major diagonal elements of a 3D Matrix - GeeksforGeeks

08 Jun, 2021

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++
Java
Python3
C#
Javascript
 

 
 










// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate Bitwise XOR of
// major diagonal elements of 3D matrix
void findXOR(
    vector<vector<vector<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
    cout << XOR << "\n";
}
 
// Driver Code
int main()
{
    vector<vector<vector<int> > > mat
        = { { { 1, 2 }, { 3, 4 } },
            { { 5, 6 }, { 7, 8 } } };
    int N = mat.size();
 
    findXOR(mat, N);
 
    return 0;
}






 

 
 










// 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 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# 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






 

 
 










<script>
// JavaScript program for the above approach
 
// Function to calculate Bitwise XOR of
// major diagonal elements of 3D matrix
function findXOR(mat, N)
{
      
    // Stores the Bitwise XOR of
    // the major diagonal elements
    let XOR = 0;
  
    for(let i = 0; i < N; i++)
    {
        for(let j = 0; j < N; j++)
        {
            for(let 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
    document.write(XOR);
}
 
 
// Driver Code
 
    let mat = [[[1, 2 ], [ 3, 4 ]],
                     [[ 5, 6 ], [ 7, 8 ]]];
    let N = mat.length;
  
    findXOR(mat, N);
     
</script>






12
 
Time Complexity: O(N3)Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using the fact that the element whose indices i, j, and k are same as the diagonal elements. Therefore, the idea is to iterate over the range of indices [0, N – 1] using a variable i and calculate Bitwise XOR of all the elements that are a part of major diagonals at indices (i, i, i) and (i, i, N – i – 1) in the given matrix mat[][][] as mat[i][i][i] and mat[i][i][N – i – 1] respectively.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
 

 
 










// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the Bitwise XOR of
// both diagonal elements of 3D matrix
void findXOR(
    vector<vector<vector<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
    cout << XOR << "\n";
}
 
// Driver Code
int main()
{
    vector<vector<vector<int> > > mat
        = { { { 1, 2 }, { 3, 4 } },
            { { 5, 6 }, { 7, 8 } } };
    int N = mat.size();
 
    findXOR(mat, N);
 
    return 0;
}






 

 
 










// 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 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# 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






 

 
 










<script>
 
// Javascript program for the above approach
 
// Function to calculate Bitwise XOR of
// major diagonal elements of 3D matrix
function findXOR( mat,  N)
{
     
    // Stores the Bitwise XOR of the
    // major diagonal elements
    let XOR = 0;
 
    for(let i = 0; i < N; i++)
    {
        XOR ^= mat[i][i][i];
        XOR ^= mat[i][i][N - i - 1];
    }
     
    // Print the resultant Bitwise XOR
    document.write(XOR);
}
 
// Driver Code
 
let mat = [ [ [ 1, 2 ], [ 3, 4 ] ],
          [ [ 5, 6 ], [ 7, 8 ] ] ];
let N = mat.length;
 
findXOR(mat, N);;
     
</script>






12
 
Time Complexity: O(N)Auxiliary Space: O(1)
Kingash
ukasp
splevel62
avijitmondal1998
sanjoy_62
chinmoy1997pal
jana_sayantan
simmytarika5

Bitwise-XOR


Technical Scripter 2020


Bit Magic


Mathematical


Matrix


Technical Scripter

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
Set, Clear and Toggle a given bit of a number in C
Hamming code Implementation in C/C++
Program to find parity
Check whether K-th bit is set or not
Write an Efficient Method to Check if a Number is Multiple of 3
Program for Fibonacci numbers
Write a  program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7