Kth highest XOR of diagonal elements from a Matrix
Last Updated :
24 Mar, 2023
Given a square matrix mat[][] of size N * N, the task is to calculate XOR value of every diagonal elements and find the Kth maximum XOR value obtained.
Note: There are 2 * N – 1 diagonals in the matrix. Starting point of ith diagonal is (min(N, i), (1 + max(i – N, 0))).
Examples:
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, K = 3
Output: 6
Explanation:
XOR of 1st diagonal = 1.
XOR of 2nd diagonal = 4 ^ 2 = 6.
XOR of 3rd diagonal = 7 ^ 5 ^ 3 = 1.
XOR of 4th diagonal = 8 ^ 6 = 14.
XOR of 5th diagonal = 9.
Input: mat[][] = {{1, 2}, {4, 5}}, K = 2
Output: 6
Explanation:
XOR of 1st diagonal =1.
XOR of 2nd diagonal = 4 ^ 2 = 6.
XOR of 3rd diagonal = 5.
Approach: Follow the steps below to solve the problem
- Traverse the matrix diagonally.
- For every ith diagonal, starting point is (min(N, i), (1 + max(i – N, 0))).
- Store XOR of each diagonal in a vector.
- Sort the vector.
- Print the Kth maximum value obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findXOR(vector<vector< int > > mat, int K)
{
int N = mat.size();
int M = mat[0].size();
vector< int > digXOR;
for ( int l = 1; l <= (N + M - 1); l++) {
int s_col = max(0, l - N);
int count = min({ l, (M - s_col), N });
int currXOR = 0;
for ( int j = 0; j < count; j++) {
currXOR
= (currXOR
^ mat[min(N, l) - j - 1][s_col + j]);
}
digXOR.push_back(currXOR);
}
sort(digXOR.begin(), digXOR.end());
cout << digXOR[N + M - 1 - K];
}
int main()
{
vector<vector< int > > mat
= { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int K = 3;
findXOR(mat, K);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findXOR( int [][] mat, int K)
{
int N = mat.length;
int M = mat[ 0 ].length;
ArrayList<Integer> digXOR
= new ArrayList<Integer>();
for ( int l = 1 ; l <= (N + M - 1 ); l++) {
int s_col = Math.max( 0 , l - N);
int count = Math.min( l, Math.min((M - s_col), N));
int currXOR = 0 ;
for ( int j = 0 ; j < count; j++) {
currXOR
= (currXOR
^ mat[(Math.min(N, l) - j - 1 )][s_col + j]);
}
digXOR.add(currXOR);
}
Collections.sort(digXOR);
System.out.print(digXOR.get(N + M - 1 - K));
}
public static void main(String[] args)
{
int [][] mat
= { { 1 , 2 , 3 },
{ 4 , 5 , 6 },
{ 7 , 8 , 9 } };
int K = 3 ;
findXOR(mat, K);
}
}
|
Python3
def findXOR(mat, K):
N = len (mat)
M = len (mat[ 0 ])
digXOR = []
for l in range ( 1 , N + M, 1 ):
s_col = max ( 0 , l - N)
count = min ([l, (M - s_col), N])
currXOR = 0
for j in range (count):
currXOR = (currXOR ^ mat[ min (N, l) - j - 1 ][s_col + j])
digXOR.append(currXOR)
digXOR.sort(reverse = False )
print (digXOR[N + M - 1 - K])
if __name__ = = '__main__' :
mat = [[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]]
K = 3
findXOR(mat, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findXOR( int [,]mat, int K)
{
int N = mat.GetLength(0);
int M = mat.GetLength(1);
List< int > digXOR = new List< int >();
for ( int l = 1; l <= (N + M - 1); l++)
{
int s_col = Math.Max(0, l - N);
int count = Math.Min(l, Math.Min((M - s_col), N));
int currXOR = 0;
for ( int j = 0; j < count; j++)
{
currXOR = (currXOR ^ mat[(Math.Min(N, l) - j - 1),
s_col + j]);
}
digXOR.Add(currXOR);
}
digXOR.Sort();
Console.Write(digXOR[N + M - 1 - K]);
}
public static void Main(String[] args)
{
int [,] mat = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int K = 3;
findXOR(mat, K);
}
}
|
Javascript
<script>
function findXOR(mat, K)
{
let N = mat.length;
let M = mat[0].length;
let digXOR = [];
for (let l = 1; l <= (N + M - 1); l++) {
let s_col = Math.max(0, l - N);
let count = Math.min(l, (M - s_col), N);
let currXOR = 0;
for (let j = 0; j < count; j++) {
currXOR
= (currXOR
^ mat[(Math.min(N, l) - j - 1)][s_col + j]);
}
digXOR.push(currXOR);
}
digXOR.sort((a, b) => a - b);
document.write(digXOR[N + M - 1 - K]);
}
let mat
= [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]];
let K = 3;
findXOR(mat, K);
</script>
|
Time Complexity: O(N * M)
Space Complexity: O(N * M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...