Open In App

Queries in a Matrix

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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. 

  1. R(x, y): swaps the x-th and y-th rows of M where x and y vary from 1 to m.
  2. C(x, y): swaps the x-th and y-th columns of M where x and y vary from 1 to n.
  3. 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++




// C++ implementation of program
#include<bits/stdc++.h>
using namespace std;
 
// Fills initial values in rows[] and cols[]
void preprocessMatrix(int rows[], int cols[],
                     int m, int n)
{
    // Fill rows with 1 to m-1
    for (int i=0; i<m; i++)
        rows[i] = i;
 
    // Fill columns with 1 to n-1
    for (int i=0; i<n; i++)
        cols[i] = i;
}
 
// Function to perform queries on matrix
// m --> number of rows
// n --> number of columns
// ch --> type of query
// x --> number of row for query
// y --> number of column for query
void queryMatrix(int rows[], int cols[], int m,
                 int n, char ch, int x, int y)
{
    // perform queries
    int tmp;
    switch(ch)
    {
    case 'R':
 
        // swap row x with y
        swap(rows[x-1], rows[y-1]);
        break;
 
    case 'C':
 
        // swap column x with y
        swap(cols[x-1], cols[y-1]);
        break;
 
    case 'P':
 
        // Print value at (x, y)
        printf("value at (%d, %d) = %d\n", x, y,
                   rows[x-1]*n + cols[y-1]+1);
        break;
    }
    return ;
}
 
// Driver program to run the case
int main()
{
    int m = 1234, n = 5678;
 
    // row[] is array for rows and cols[]
    // is array for columns
    int rows[m], cols[n];
 
    // Fill initial values in rows[] and cols[]
    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




// Java implementation of program
import java.io.*;
class GFG
{
 
    // Fills initial values in rows[] and cols[]
    static void preprocessMatrix(int rows[], int cols[],
                                        int m, int n)
    {
        // Fill rows with 1 to m-1
        for (int i = 0; i < m; i++)
        {
            rows[i] = i;
        }
 
        // Fill columns with 1 to n-1
        for (int i = 0; i < n; i++)
        {
            cols[i] = i;
        }
    }
 
    // Function to perform queries on matrix
    // m --> number of rows
    // n --> number of columns
    // ch --> type of query
    // x --> number of row for query
    // y --> number of column for query
    static void queryMatrix(int rows[], int cols[], int m,
                            int n, char ch, int x, int y)
    {
        // perform queries
        int tmp;
        switch (ch)
        {
            case 'R':
 
                // swap row x with y
                swap(rows, x - 1, y - 1);
                break;
 
            case 'C':
 
                // swap column x with y
                swap(cols, x - 1, y - 1);
                break;
 
            case 'P':
 
                // Print value at (x, y)
                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;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int m = 1234, n = 5678;
 
        // row[] is array for rows and cols[]
        // is array for columns
        int rows[] = new int[m], cols[] = new int[n];
 
        // Fill initial values in rows[] and cols[]
        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);
    }
}
 
// This code contributed by Rajput-Ji


Python3




# Python3 implementation of program
 
# Fills initial values in rows[] and cols[]
def preprocessMatrix(rows, cols, m, n):
 
    # Fill rows with 1 to m-1
    for i in range(m):
        rows[i] = i;
 
    # Fill columns with 1 to n-1
    for i in range(n):
        cols[i] = i;
 
# Function to perform queries on matrix
# m --> number of rows
# n --> number of columns
# ch --> type of query
# x --> number of row for query
# y --> number of column for query
def queryMatrix(rows, cols, m, n, ch, x, y):
 
    # perform queries
    tmp = 0;
     
    if ch == 'R':
 
        # swap row x with y
        rows[x-1], rows[y-1] = rows[y-1], rows[x-1];
 
    elif ch == 'C':
 
        # swap column x with y
        cols[x-1], cols[y-1] = cols[y-1],cols[x-1];
 
    elif ch == 'P':
 
        # Print value at (x, y)
        print('value at (',x,',',y,') = ',rows[x-1]*n + cols[y-1]+1, sep='');
         
    return ;
 
# Driver program to run the case
m = 1234
n = 5678;
 
# row[] is array for rows and cols[]
# is array for columns
rows = [0 for i in range(m)]
cols = [0 for i in range(n)];
 
# Fill initial values in rows[] and cols[]
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);
 
# This code is contributed by rutvik_56.


C#




// C# implementation of program
using System;
 
class GFG
{
 
    // Fills initial values in rows[] and cols[]
    static void preprocessMatrix(int []rows, int []cols,
                                        int m, int n)
    {
        // Fill rows with 1 to m-1
        for (int i = 0; i < m; i++)
        {
            rows[i] = i;
        }
 
        // Fill columns with 1 to n-1
        for (int i = 0; i < n; i++)
        {
            cols[i] = i;
        }
    }
 
    // Function to perform queries on matrix
    // m --> number of rows
    // n --> number of columns
    // ch --> type of query
    // x --> number of row for query
    // y --> number of column for query
    static void queryMatrix(int []rows, int []cols, int m,
                            int n, char ch, int x, int y)
    {
        // perform queries
        int tmp;
        switch (ch)
        {
            case 'R':
 
                // swap row x with y
                swap(rows, x - 1, y - 1);
                break;
 
            case 'C':
 
                // swap column x with y
                swap(cols, x - 1, y - 1);
                break;
 
            case 'P':
 
                // Print value at (x, y)
                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;
    }
 
    // Driver code
    public static void Main()
    {
        int m = 1234, n = 5678;
 
        // row[] is array for rows and cols[]
        // is array for columns
        int []rows = new int[m]; int []cols = new int[n];
 
        // Fill initial values in rows[] and cols[]
        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);
    }
}
 
/* This code contributed by PrinciRaj1992 */


Javascript




<script>
 
// JavaScript implementation of program
// Fills initial values in rows[] and cols[]
function preprocessMatrix(rows, cols, m, n)
{
    // Fill rows with 1 to m-1
    for (var i = 0; i < m; i++)
    {
        rows[i] = i;
    }
    // Fill columns with 1 to n-1
    for (var i = 0; i < n; i++)
    {
        cols[i] = i;
    }
}
// Function to perform queries on matrix
// m --> number of rows
// n --> number of columns
// ch --> type of query
// x --> number of row for query
// y --> number of column for query
function queryMatrix(rows, cols, m, n, ch, x, y)
{
    // perform queries
    var tmp;
    switch (ch)
    {
        case 'R':
            // swap row x with y
            swap(rows, x - 1, y - 1);
            break;
        case 'C':
            // swap column x with y
            swap(cols, x - 1, y - 1);
            break;
        case 'P':
            // Print value at (x, y)
            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;
}
// Driver code
var m = 1234, n = 5678;
// row[] is array for rows and cols[]
// is array for columns
var rows = Array(m).fill(0)
var cols = Array(n).fill(0)
// Fill initial values in rows[] and cols[]
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)

 



Last Updated : 12 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads