给定大小为N * N的方阵mat [] [] ,任务是计算每个对角元素的XOR值,并找到获得的第K个最大XOR值。
注意:矩阵中有2 * N – 1个对角线。第i个对角线的起点是(min(N,i),(1 + max(i – N,0)))。
例子:
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.
方法:按照以下步骤解决问题
- 沿对角线遍历矩阵。
- 对于第i个对角线,起点是(min(N,i),(1 + max(i – N,0)))。
- 将每个对角线的XOR存储在向量中。
- 排序向量。
- 打印获得的第K个最大值。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to find K-th maximum XOR
// of any diagonal in the matrix
void findXOR(vector > mat, int K)
{
// Number or rows
int N = mat.size();
// Number of columns
int M = mat[0].size();
// Store XOR of diagonals
vector digXOR;
// Traverse each diagonal
for (int l = 1; l <= (N + M - 1); l++) {
// Starting column of diagonal
int s_col = max(0, l - N);
// Count total elements in the diagonal
int count = min({ l, (M - s_col), N });
// Store XOR of current diagonal
int currXOR = 0;
for (int j = 0; j < count; j++) {
currXOR
= (currXOR
^ mat[min(N, l) - j - 1][s_col + j]);
}
// Push XOR of current diagonal
digXOR.push_back(currXOR);
}
// Sort XOR values of diagonals
sort(digXOR.begin(), digXOR.end());
// Print the K-th Maximum XOR
cout << digXOR[N + M - 1 - K];
}
// Driver Code
int main()
{
vector > mat
= { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int K = 3;
findXOR(mat, K);
return 0;
}
Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to find K-th maximum XOR
// of any diagonal in the matrix
static void findXOR(int[][] mat, int K)
{
// Number or rows
int N = mat.length;
// Number of columns
int M = mat[0].length;
// Store XOR of diagonals
ArrayList digXOR
= new ArrayList();
// Traverse each diagonal
for (int l = 1; l <= (N + M - 1); l++) {
// Starting column of diagonal
int s_col = Math.max(0, l - N);
// Count total elements in the diagonal
int count = Math.min( l, Math.min((M - s_col), N));
// Store XOR of current diagonal
int currXOR = 0;
for (int j = 0; j < count; j++) {
currXOR
= (currXOR
^ mat[Math.min(N, l) - j - 1][s_col + j]);
}
// Push XOR of current diagonal
digXOR.add(currXOR);
}
// Sort XOR values of diagonals
Collections.sort(digXOR);
// Print the K-th Maximum XOR
System.out.print(digXOR.get(N + M - 1 - K));
}
// Driver Code
public static void main(String[] args)
{
int[][] mat
= { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int K = 3;
findXOR(mat, K);
}
}
// This code is contributed by code_hunt.
Python3
# Python3 Program to implement
# the above approach
# Function to find K-th maximum XOR
# of any diagonal in the matrix
def findXOR(mat, K):
# Number or rows
N = len(mat)
# Number of columns
M = len(mat[0])
# Store XOR of diagonals
digXOR = []
# Traverse each diagonal
for l in range(1, N + M, 1):
# Starting column of diagonal
s_col = max(0, l - N)
# Count total elements in the diagonal
count = min([l, (M - s_col), N])
# Store XOR of current diagonal
currXOR = 0
for j in range(count):
currXOR = (currXOR ^ mat[min(N, l) - j - 1][s_col + j])
# Push XOR of current diagonal
digXOR.append(currXOR)
# Sort XOR values of diagonals
digXOR.sort(reverse=False)
# Print the K-th Maximum XOR
print(digXOR[N + M - 1 - K])
# Driver Code
if __name__ == '__main__':
mat = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
K = 3
findXOR(mat, K)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find K-th maximum XOR
// of any diagonal in the matrix
static void findXOR(int[,]mat, int K)
{
// Number or rows
int N = mat.GetLength(0);
// Number of columns
int M = mat.GetLength(1);
// Store XOR of diagonals
List digXOR = new List();
// Traverse each diagonal
for(int l = 1; l <= (N + M - 1); l++)
{
// Starting column of diagonal
int s_col = Math.Max(0, l - N);
// Count total elements in the diagonal
int count = Math.Min(l, Math.Min((M - s_col), N));
// Store XOR of current diagonal
int currXOR = 0;
for(int j = 0; j < count; j++)
{
currXOR = (currXOR ^ mat[Math.Min(N, l) - j - 1,
s_col + j]);
}
// Push XOR of current diagonal
digXOR.Add(currXOR);
}
// Sort XOR values of diagonals
digXOR.Sort();
// Print the K-th Maximum XOR
Console.Write(digXOR[N + M - 1 - K]);
}
// Driver Code
public static void Main(String[] args)
{
int[,] mat = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int K = 3;
findXOR(mat, K);
}
}
// This code is contributed by shikhasingrajput
输出:
6
时间复杂度: O(N * M)
空间复杂度: O(N * M)