Queries in a Matrix

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 swamping. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 coloumn 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 coloumns
    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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of program 
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 coloumn 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 coloumns 
        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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 coloumn 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 coloumns 
        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 */

chevron_right


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)

This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Rajput-Ji, princiraj1992



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.