Queries in a Matrix
Last Updated :
12 Dec, 2022
Given a matrix M of size m x n ( 1 <= m,n <= 1000 ). It is initially filled with integers from 1 to m x n sequentially in a row major order. The task is to process a list of queries manipulating M such that every query is one of the following three.
- R(x, y): swaps the x-th and y-th rows of M where x and y vary from 1 to m.
- C(x, y): swaps the x-th and y-th columns of M where x and y vary from 1 to n.
- P(x, y): prints the element at x-th row and y-th column where x varies from 1 to m and y varies from 1 to n.
Note that the given matrix is stored as a typical 2D array with indexes start from 0, but values of x and y start from 1.
Examples:
Input : m = 3, n = 3
R(1, 2)
P(1, 1)
P(2, 1)
C(1, 2)
P(1, 1)
P(2, 1)
Output: value at (1, 1) = 4
value at (2, 1) = 1
value at (1, 1) = 5
value at (2, 1) = 2
Explanation:
The matrix is {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
After first R(1, 2) matrix becomes,
{{4, 5, 6},
{1, 2, 3},
{7, 8, 9}}
After first C(1, 2) matrix becomes,
{{5, 4, 6},
{2, 1, 3},
{8, 7, 9}}
Input : m = 1234, n = 5678
R(1, 2)
P(1, 1)
P(2, 1)
C(1, 2)
P(1, 1)
P(2, 1)
Output: value at (1, 1) = 5679
value at (2, 1) = 1
value at (1, 1) = 5680
value at (2, 1) = 2
A simple solution for this problem is to finish all the queries manually, that means when we have to swap the rows just swap the elements of x’th row and y’th row and similarly for column swapping. But this approach may have time complexity of q*O(m) or q*O(n) where ‘q’ is number of queries and auxiliary space required O(m*n).
An efficient approach for this problem requires little bit mathematical observation. Here we are given that elements in matrix are filled from 1 to mxn sequentially in row major order, so we will take advantage of this given scenario and can solve this problem.
- Create an auxiliary array rows[m] and fill it with values 0 to m-1 sequentially.
- Create another auxiliary array cols[n] and fill it with values 0 to n-1 sequentially.
- Now for query ‘R(x, y)’ just swap the value of rows[x-1] with rows[y-1].
- Now for query ‘C(x, y)’ just swap the value of cols[x-1] with cols[y-1].
- Now for query ‘P(x, y)’ just skip the number of columns you have seen and calculate the value at (x, y) by rows[x-1]*n + cols[y-1] + 1.
Below is implementation of above idea.
C++
#include<bits/stdc++.h>
using namespace std;
void preprocessMatrix( int rows[], int cols[],
int m, int n)
{
for ( int i=0; i<m; i++)
rows[i] = i;
for ( int i=0; i<n; i++)
cols[i] = i;
}
void queryMatrix( int rows[], int cols[], int m,
int n, char ch, int x, int y)
{
int tmp;
switch (ch)
{
case 'R' :
swap(rows[x-1], rows[y-1]);
break ;
case 'C' :
swap(cols[x-1], cols[y-1]);
break ;
case 'P' :
printf ( "value at (%d, %d) = %d\n" , x, y,
rows[x-1]*n + cols[y-1]+1);
break ;
}
return ;
}
int main()
{
int m = 1234, n = 5678;
int rows[m], cols[n];
preprocessMatrix(rows, cols, m, n);
queryMatrix(rows, cols, m, n, 'R' , 1, 2);
queryMatrix(rows, cols, m, n, 'P' , 1, 1);
queryMatrix(rows, cols, m, n, 'P' , 2, 1);
queryMatrix(rows, cols, m, n, 'C' , 1, 2);
queryMatrix(rows, cols, m, n, 'P' , 1, 1);
queryMatrix(rows, cols, m, n, 'P' , 2, 1);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void preprocessMatrix( int rows[], int cols[],
int m, int n)
{
for ( int i = 0 ; i < m; i++)
{
rows[i] = i;
}
for ( int i = 0 ; i < n; i++)
{
cols[i] = i;
}
}
static void queryMatrix( int rows[], int cols[], int m,
int n, char ch, int x, int y)
{
int tmp;
switch (ch)
{
case 'R' :
swap(rows, x - 1 , y - 1 );
break ;
case 'C' :
swap(cols, x - 1 , y - 1 );
break ;
case 'P' :
System.out.printf( "value at (%d, %d) = %d\n" , x, y,
rows[x - 1 ] * n + cols[y - 1 ] + 1 );
break ;
}
return ;
}
static int [] swap( int [] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
public static void main(String[] args)
{
int m = 1234 , n = 5678 ;
int rows[] = new int [m], cols[] = new int [n];
preprocessMatrix(rows, cols, m, n);
queryMatrix(rows, cols, m, n, 'R' , 1 , 2 );
queryMatrix(rows, cols, m, n, 'P' , 1 , 1 );
queryMatrix(rows, cols, m, n, 'P' , 2 , 1 );
queryMatrix(rows, cols, m, n, 'C' , 1 , 2 );
queryMatrix(rows, cols, m, n, 'P' , 1 , 1 );
queryMatrix(rows, cols, m, n, 'P' , 2 , 1 );
}
}
|
Python3
def preprocessMatrix(rows, cols, m, n):
for i in range (m):
rows[i] = i;
for i in range (n):
cols[i] = i;
def queryMatrix(rows, cols, m, n, ch, x, y):
tmp = 0 ;
if ch = = 'R' :
rows[x - 1 ], rows[y - 1 ] = rows[y - 1 ], rows[x - 1 ];
elif ch = = 'C' :
cols[x - 1 ], cols[y - 1 ] = cols[y - 1 ],cols[x - 1 ];
elif ch = = 'P' :
print ( 'value at (' ,x, ',' ,y, ') = ' ,rows[x - 1 ] * n + cols[y - 1 ] + 1 , sep = '');
return ;
m = 1234
n = 5678 ;
rows = [ 0 for i in range (m)]
cols = [ 0 for i in range (n)];
preprocessMatrix(rows, cols, m, n);
queryMatrix(rows, cols, m, n, 'R' , 1 , 2 );
queryMatrix(rows, cols, m, n, 'P' , 1 , 1 );
queryMatrix(rows, cols, m, n, 'P' , 2 , 1 );
queryMatrix(rows, cols, m, n, 'C' , 1 , 2 );
queryMatrix(rows, cols, m, n, 'P' , 1 , 1 );
queryMatrix(rows, cols, m, n, 'P' , 2 , 1 );
|
C#
using System;
class GFG
{
static void preprocessMatrix( int []rows, int []cols,
int m, int n)
{
for ( int i = 0; i < m; i++)
{
rows[i] = i;
}
for ( int i = 0; i < n; i++)
{
cols[i] = i;
}
}
static void queryMatrix( int []rows, int []cols, int m,
int n, char ch, int x, int y)
{
int tmp;
switch (ch)
{
case 'R' :
swap(rows, x - 1, y - 1);
break ;
case 'C' :
swap(cols, x - 1, y - 1);
break ;
case 'P' :
Console.Write( "value at ({0}, {1}) = {2}\n" , x, y,
rows[x - 1] * n + cols[y - 1] + 1);
break ;
}
return ;
}
static int [] swap( int [] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
public static void Main()
{
int m = 1234, n = 5678;
int []rows = new int [m]; int []cols = new int [n];
preprocessMatrix(rows, cols, m, n);
queryMatrix(rows, cols, m, n, 'R' , 1, 2);
queryMatrix(rows, cols, m, n, 'P' , 1, 1);
queryMatrix(rows, cols, m, n, 'P' , 2, 1);
queryMatrix(rows, cols, m, n, 'C' , 1, 2);
queryMatrix(rows, cols, m, n, 'P' , 1, 1);
queryMatrix(rows, cols, m, n, 'P' , 2, 1);
}
}
|
Javascript
<script>
function preprocessMatrix(rows, cols, m, n)
{
for ( var i = 0; i < m; i++)
{
rows[i] = i;
}
for ( var i = 0; i < n; i++)
{
cols[i] = i;
}
}
function queryMatrix(rows, cols, m, n, ch, x, y)
{
var tmp;
switch (ch)
{
case 'R' :
swap(rows, x - 1, y - 1);
break ;
case 'C' :
swap(cols, x - 1, y - 1);
break ;
case 'P' :
document.write(`value at
(${x}, ${y}) = ${rows[x - 1] * n + cols[y - 1] + 1}<br>`);
break ;
}
return ;
}
function swap(arr, i, j)
{
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
var m = 1234, n = 5678;
var rows = Array(m).fill(0)
var cols = Array(n).fill(0)
preprocessMatrix(rows, cols, m, n);
queryMatrix(rows, cols, m, n, 'R' , 1, 2);
queryMatrix(rows, cols, m, n, 'P' , 1, 1);
queryMatrix(rows, cols, m, n, 'P' , 2, 1);
queryMatrix(rows, cols, m, n, 'C' , 1, 2);
queryMatrix(rows, cols, m, n, 'P' , 1, 1);
queryMatrix(rows, cols, m, n, 'P' , 2, 1);
</script>
|
Output
value at (1, 1) = 5679
value at (2, 1) = 1
value at (1, 1) = 5680
value at (2, 1) = 2
Time Complexity : O(q) , q = number of queries
Axillary Space : O(m+n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...