Smallest submatrix with Kth maximum XOR
Given a matrix m[][] of dimensions N × M and an integer K, calculate XOR(i, j) which is equal to the Bitwise Xor of all elements of submatrix from indices (1, 1) to (i, j)), for every index of the matrix. The task is to find the submatrix {(1, 1), …, (i, j)} having Kth maximum XOR(i, j) value. If multiple such submatrices exist, then print the smallest one.
Note: Consider the starting index of the matrix from (1, 1).
Examples:
Input: m[][] = {{1, 2}, {2, 3}}, K = 2
Output: 1 2
Explanation:
XOR(1, 1) : m[1][1] = 1
XOR(1, 2): m[1][1] xor m[1][2] = 3
XOR(2, 1): m[1][1] xor m[2][1] = 3
XOR(2, 2): m[1][1] xor m[1][2] xor m[2][1] xor m[2][2] = 2
Hence, the 2nd maximum value is 3 at position [1, 2].
Input: m[][] = {{1, 2, 3}, {2, 2, 1}, {2, 4, 2} }, k = 1
Output: 3 2
Approach: The idea is to find XOR (i, j) using Dynamic Programming.
- Calculate the bitwise XOR(i, j) as xor[i][j] = xor[i-1][j] ^ xor[i][j-1] ^ xor[i-1][j-1] ^ m[i][j].
- Store the XOR(i, j) values obtained for respective indices (i, j) in a Map.
- Find the Kth maximum of all XOR(i, j) values using a Min-heap of size K.
- Find the smallest index (i, j) for which XOR(i, j) is equal to the Kth maximum obtained in the above step using the Map.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void smallestPosition( int m[][3], int k, int row)
{
int n = row;
int mm = row;
int xors[n][mm];
priority_queue< int , vector< int >,
greater< int >> minHeap;
map< int , vector< int >> map;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < mm; j++)
{
int a = i - 1 >= 0 ? xors[i - 1][j] : 0;
int b = j - 1 >= 0 ? xors[i][j - 1] : 0;
int c = (i - 1 >= 0 && j - 1 >= 0) ?
xors[i - 1][j - 1] : 0;
xors[i][j] = m[i][j] ^ a ^ b ^ c;
minHeap.push(xors[i][j]);
if (minHeap.size() > k)
{
minHeap.pop();
}
if (map.find(xors[i][j]) == map.end())
map[xors[i][j]] = {i, j};
}
}
int kth_max_e = minHeap.top();
cout << (map[kth_max_e][0] + 1) << " "
<< (map[kth_max_e][1] + 1);
}
int main()
{
int m[][3] = { { 1, 2, 3 },
{ 2, 2, 1 },
{ 2, 4, 2 } };
int k = 1;
smallestPosition(m, k, 3);
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static void smallestPosition( int m[][], int k)
{
int n = m.length;
int mm = m[ 0 ].length;
int [][] xor = new int [n][mm];
PriorityQueue<Integer> minHeap
= new PriorityQueue<>();
Map<Integer, int []> map
= new HashMap<>();
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < mm; j++) {
int a = i - 1 >= 0
? xor[i - 1 ][j]
: 0 ;
int b = j - 1 >= 0
? xor[i][j - 1 ]
: 0 ;
int c = (i - 1 >= 0 && j - 1 >= 0 )
? xor[i - 1 ][j - 1 ]
: 0 ;
xor[i][j] = m[i][j] ^ a ^ b ^ c;
minHeap.add(xor[i][j]);
if (minHeap.size() > k) {
minHeap.poll();
}
if (!map.containsKey(xor[i][j]))
map.put(xor[i][j],
new int [] { i, j });
}
}
int kth_max_e = minHeap.poll();
System.out.println(
(map.get(kth_max_e)[ 0 ] + 1 )
+ " " + (map.get(kth_max_e)[ 1 ] + 1 ));
}
public static void main(String[] args)
{
int m[][] = { { 1 , 2 , 3 },
{ 2 , 2 , 1 },
{ 2 , 4 , 2 } };
int k = 1 ;
smallestPosition(m, k);
}
}
|
Python3
def smallestPosition(m, k) :
n = len (m)
mm = len (m[ 1 ])
xor = [[ 0 for i in range (mm)] for j in range (n)]
minHeap = []
Map = {}
for i in range (n) :
for j in range (mm) :
if i - 1 > = 0 :
a = xor[i - 1 ][j]
else :
a = 0
if j - 1 > = 0 :
b = xor[i][j - 1 ]
else :
b = 0
if i - 1 > = 0 and j - 1 > = 0 :
c = xor[i - 1 ][j - 1 ]
else :
c = 0
xor[i][j] = m[i][j] ^ a ^ b ^ c
minHeap.append(xor[i][j])
minHeap.sort()
if ( len (minHeap) > k) :
del minHeap[ 0 ]
if xor[i][j] not in Map :
Map [xor[i][j]] = [i, j]
minHeap.sort()
kth_max_e = minHeap[ 0 ]
print (( Map [kth_max_e][ 0 ] + 1 ), ( Map [kth_max_e][ 1 ] + 1 ))
m = [[ 1 , 2 , 3 ],
[ 2 , 2 , 1 ],
[ 2 , 4 , 2 ]]
k = 1
smallestPosition(m, k)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void smallestPosition( int [,]m,
int k)
{
int n = m.GetLength(0);
int mm = m.GetLength(1);
int [,] xor = new int [n, mm];
List< int > minHeap =
new List< int >();
Dictionary< int ,
int []> map = new Dictionary< int ,
int []>();
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < mm; j++)
{
int a = i - 1 >= 0 ?
xor[i - 1, j] : 0;
int b = j - 1 >= 0 ?
xor[i, j - 1] : 0;
int c = (i - 1 >= 0 &&
j - 1 >= 0) ?
xor[i - 1, j - 1] : 0;
xor[i, j] = m[i, j] ^
a ^ b ^ c;
minHeap.Add(xor[i, j]);
minHeap.Sort();
if (minHeap.Count > k)
{
minHeap.RemoveAt(0);
}
if (!map.ContainsKey(xor[i, j]))
map.Add(xor[i, j],
new int [] {i, j});
}
}
minHeap.Sort();
int kth_max_e = minHeap[0];
Console.WriteLine((map[kth_max_e][0] + 1) +
" " + (map[kth_max_e][1] + 1));
}
public static void Main(String[] args)
{
int [,]m = {{1, 2, 3},
{2, 2, 1},
{2, 4, 2}};
int k = 1;
smallestPosition(m, k);
}
}
|
Javascript
<script>
function smallestPosition(m, k){
let n = m.length
let mm = m[1].length
let xor = new Array(n).fill(0).map(()=> new Array(mm).fill(0))
let minHeap = []
let Map1 = new Map()
let a = 0,b = 0,c = 0
for (let i=0;i<n;i++){
for (let j=0;j<mm;j++){
if (i - 1 >= 0)
a = xor[i - 1][j]
else
a = 0
if (j - 1 >= 0)
b = xor[i][j - 1]
else
b = 0
if (i - 1 >= 0 && j - 1 >= 0)
c = xor[i - 1][j - 1]
else
c = 0
xor[i][j] = m[i][j] ^ a ^ b ^ c
minHeap.push(xor[i][j])
minHeap.sort()
if (minHeap.length > k){
minHeap.shift()
}
if (!Map1.has(xor[i][j]))
Map1.set(xor[i][j],[i, j])
}
}
minHeap.sort()
let kth_max_e = minHeap[0]
document.write((Map1.get(kth_max_e)[0] + 1), (Map1.get(kth_max_e)[1] + 1), "</br>" )
}
let m = [[1, 2, 3],
[2, 2, 1],
[2, 4, 2]]
let k = 1
smallestPosition(m, k)
</script>
|
Time Complexity: O(N * M * log K)
Auxiliary Space: O(N * M)
Last Updated :
02 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...