Given a matrix, mat[][] of size N * M, where N and M is always even, and two integers X and Y, the task is to swap all the elements of the quadrant X quadrant with all the corresponding elements of quadrant Y of the given matrix.
Note: The top-left, top-right, bottom-left, and bottom-right quadrants are numbered 1, 2, 3, and 4 respectively.
Examples:Â
Â
Input: mat[][] = {{99, 10, 11, 12, 13, 14, 15, 16}, {17, 18, 19, 20, 21, 22, 23, 24}, {25, 26, 27, 28, 29, 30, 31, 32}, {33, 34, 35, 36, 37, 38, 39, 40}, {41, 42, 43, 44, 45, 46, 47, 48}, {49, 50, 51, 52, 53, 54, 55, 56}}, X = 1, Y = 4Â
Output: {{37, 38, 39, 40, 13, 14, 15, 16}, { 45, 46, 47, 48, 21, 22, 23, 24}, { 53, 54, 55, 56, 29, 30, 31, 32}, {33, 34, 35, 36, 99, 10, 11, 12}, {41, 42, 43, 44, 17, 18, 19, 20}, {49, 50, 51, 52, 25, 26, 27, 28}}Â
Explanation:Â
Given Matrix:Â
Â
Swap the 1st quadrant of the matrix with 4th quadrant of the matrix:Â
Â
Â
Input: mat[][] = {{1, 2}, {3, 4}}, X = 1, Y = 4Â
Output: {{4, 2}, {3, 1}}Â
Â
Approach: The idea is to iterate over the X quadrant of the matrix and swap its elements with the corresponding elements of the Y quadrant. To get the starting positions of any quadrant when a matrix mat[][] of size N*M below is the condition for the same:Â Â
- Quadrant 1: The starting position of the quadrant is (0, 0).
- Quadrant 2: The starting position of the quadrant is (0, M/2).
- Quadrant 3: The starting position of the quadrant is (N/2, 0).
- Quadrant 4: The starting position of the quadrant is (N/2, M/2).
Follow the steps below to solve the problem:Â Â
- Iterate over the X and Y quadrants of the matrix simultaneously from their starting positions.
- Now, swap the elements of X quadrant with the corresponding indexed elements of Y quadrant.
- Print all elements of the matrix mat[][] after performing all the swap operations.
Below is the implementation of the above approach:
Â
C++
// C++ program for // the above approach #include<bits/stdc++.h> using namespace std; #define N 6 #define M 6 Â
// Function to iterate over the X // quadrant and swap its element // with Y quadrant void swap( int mat[N][M], Â Â Â Â Â Â Â Â Â Â int startx_X, int starty_X, Â Â Â Â Â Â Â Â Â Â int startx_Y, int starty_Y) { Â Â int row = 0; Â Â int col = 0; Â
  // Iterate over X quadrant   for ( int i = startx_X;; i++)   {     col = 0; Â
    for ( int j = startx_X;; j++)     {       // Swap operations       int temp = mat[i][j]; Â
      mat[i][j] = mat[startx_Y + row][starty_Y + col];       mat[startx_Y + row][starty_Y + col] = temp;       col++;       if (col >= M / 2)         break ;     }     row++; Â
    if (row >= N / 2)       break ;   } } Â
// Function to print the matrix void printMat( int mat[N][M]) {   // Iterate over the rows   for ( int i = 0; i < N; i++)   {     // Iterate over the cols     for ( int j = 0; j < M; j++)     {       cout << mat[i][j] << " " ;     }     cout << endl;   } } Â
// Function to swap the elements // of the two given quadrants static void swapQuadOfMatrix( int mat[N][M],                              int X, int Y) {   // Swapping the coordinates on   // basis of the value of X and Y Â
  // For Swapping 1st and 2nd Quadrant   if (X == 1 && Y == 2)   {     swap(mat, 0, 0, 0, M / 2);   } Â
  // For Swapping 1st and 3rd Quadrant   else if (X == 1 && Y == 3)   {     swap(mat, 0, 0, N / 2, 0);   } Â
  // For Swapping 1st and 4th Quadrant   else if (X == 1 && Y == 4)   {     swap(mat, 0, 0, N / 2, M / 2);   } Â
  // For Swapping 2nd and 3rd Quadrant   else if (X == 2 && Y == 3)   {     swap(mat, 0, M / 2, N / 2, 0);   } Â
  // For Swapping 2nd and 4th Quadrant   else if (X == 2 && Y == 4)   {     swap(mat, 0, M / 2, N / 2, M / 2);   } Â
  // For Swapping 3rd and 4th Quadrant   else if (X == 3 && Y == 4)   {     swap(mat, N / 2, 0, N / 2, M / 2);   } Â
  // Print the resultant matrix   printMat(mat); } Â
// Driver Code int main() { Â Â // Given matrix Â
  int mat[][M] = {{1, 2, 3, 4, 5, 6},                   {7, 8, 9, 10, 11, 12},                   {13, 14, 15, 16, 17, 18},                   {19, 20, 21, 22, 23, 24},                   {25, 26, 27, 28, 29, 30},                   {31, 32, 33, 34, 35, 36}}; Â
  // Given quadrants   int X = 1, Y = 4; Â
  // Function Call   swapQuadOfMatrix(mat, X, Y); } Â
// This code is contributed by shikhasingrajput |
Java
// Java program for the above approach Â
public class Main { Â
    // Function to iterate over the X     // quadrant and swap its element     // with Y quadrant     static void swap(         int N, int M, int mat[][],         int startx_X, int starty_X,         int startx_Y, int starty_Y)     { Â
        int row = 0 ;         int col = 0 ; Â
        // Iterate over X quadrant         for ( int i = startx_X;; i++) { Â
            col = 0 ; Â
            for ( int j = startx_X;; j++) { Â
                // Swap operations                 int temp = mat[i][j]; Â
                mat[i][j]                     = mat[startx_Y + row][starty_Y + col];                 mat[startx_Y + row][starty_Y + col] = temp; Â
                col++;                 if (col >= M / 2 )                     break ;             }             row++; Â
            if (row >= N / 2 )                 break ;         }     } Â
    // Function to swap the elements     // of the two given quadrants     static void swapQuadOfMatrix(         int N, int M,         int mat[][], int X, int Y)     {         // Swapping the coordinates on         // basis of the value of X and Y Â
        // For Swapping 1st and 2nd Quadrant         if (X == 1 && Y == 2 ) {             swap(N, M, mat, 0 , 0 , 0 , M / 2 );         } Â
        // For Swapping 1st and 3rd Quadrant         else if (X == 1 && Y == 3 ) {             swap(N, M, mat, 0 , 0 , N / 2 , 0 );         } Â
        // For Swapping 1st and 4th Quadrant         else if (X == 1 && Y == 4 ) {             swap(N, M, mat, 0 , 0 , N / 2 , M / 2 );         } Â
        // For Swapping 2nd and 3rd Quadrant         else if (X == 2 && Y == 3 ) {             swap(N, M, mat, 0 ,                  M / 2 , N / 2 , 0 );         } Â
        // For Swapping 2nd and 4th Quadrant         else if (X == 2 && Y == 4 ) {             swap(N, M, mat, 0 , M / 2 ,                  N / 2 , M / 2 );         } Â
        // For Swapping 3rd and 4th Quadrant         else if (X == 3 && Y == 4 ) {             swap(N, M, mat, N / 2 , 0 ,                  N / 2 , M / 2 );         } Â
        // Print the resultant matrix         printMat(N, M, mat);     } Â
    // Function to print the matrix     static void printMat( int N, int M,                          int mat[][])     {         // Iterate over the rows         for ( int i = 0 ; i < N; i++) { Â
            // Iterate over the cols             for ( int j = 0 ; j < M; j++) { Â
                System.out.print(                     mat[i][j] + " " );             }             System.out.println();         }     } Â
    // Driver Code     public static void main(String[] args)     {         // Given matrix         int N = 6 , M = 6 ;         int [][] mat = { { 1 , 2 , 3 , 4 , 5 , 6 },                         { 7 , 8 , 9 , 10 , 11 , 12 },                         { 13 , 14 , 15 , 16 , 17 , 18 },                         { 19 , 20 , 21 , 22 , 23 , 24 },                         { 25 , 26 , 27 , 28 , 29 , 30 },                         { 31 , 32 , 33 , 34 , 35 , 36 } }; Â
        // Given quadrants         int X = 1 , Y = 4 ; Â
        // Function Call         swapQuadOfMatrix(N, M, mat, X, Y);     } } |
Python3
# Python3 program for # the above approach N, M = 6 , 6 Â
# Function to iterate over # the X quadrant and swap # its element with Y quadrant def swap(mat, startx_X, starty_X, Â Â Â Â Â Â Â Â Â startx_Y, starty_Y): Â Â Â Â Â Â Â Â Â row,col = 0 , 0 Â
    # Iterate over X quadrant     i = startx_X          while ( bool ( True )):              col = 0                j = startx_X                  while ( bool ( True )):                        # Swap operations             temp = mat[i][j]                      mat[i][j] = mat[startx_Y + row][starty_Y + col]             mat[startx_Y + row][starty_Y + col] = temp             col + = 1             if col > = M / / 2 :                 break                          j + = 1                      row + = 1              if row > = N / / 2 :             break                  i + = 1 Â
# Function to print the # matrix def printMat(mat): Â
    # Iterate over the rows     for i in range (N):              # Iterate over the cols         for j in range (M):                   print (mat[i][j],                   end = " " )                    print () Â
# Function to swap the # elements of the two # given quadrants def swapQuadOfMatrix(mat, X, Y): Â
    # Swapping the coordinates     # on basis of the value of     # X and Y          # For Swapping 1st and     # 2nd Quadrant     if (X = = 1 and Y = = 2 ):              swap(mat, 0 , 0 ,              0 , M / / 2 )          # For Swapping 1st and     # 3rd Quadrant     elif (X = = 1 and Y = = 3 ):              swap(mat, 0 , 0 ,              N / / 2 , 0 )          # For Swapping 1st and     # 4th Quadrant     elif (X = = 1 and Y = = 4 ):              swap(mat, 0 , 0 ,              N / / 2 , M / / 2 )          # For Swapping 2nd and     # 3rd Quadrant     elif (X = = 2 and Y = = 3 ):              swap(mat, 0 , M / / 2 ,              N / / 2 , 0 )          # For Swapping 2nd and     # 4th Quadrant     elif (X = = 2 and Y = = 4 ):              swap(mat, 0 , M / / 2 ,              N / / 2 , M / / 2 )          # For Swapping 3rd and     # 4th Quadrant     elif (X = = 3 and Y = = 4 ):              swap(mat, N / / 2 , 0 ,              N / / 2 , M / / 2 )          # Print the resultant     # matrix     printMat(mat)   Â
# Driver code # Given matrix mat = [[ 1 , 2 , 3 , 4 , 5 , 6 ], Â Â Â Â Â Â Â [ 7 , 8 , 9 , 10 , 11 , 12 ], Â Â Â Â Â Â Â [ 13 , 14 , 15 , 16 , 17 , 18 ], Â Â Â Â Â Â Â [ 19 , 20 , 21 , 22 , 23 , 24 ], Â Â Â Â Â Â Â [ 25 , 26 , 27 , 28 , 29 , 30 ], Â Â Â Â Â Â Â [ 31 , 32 , 33 , 34 , 35 , 36 ]] Â
# Given quadrants X, Y = 1 , 4 Â
# Function Call swapQuadOfMatrix(mat, X, Y) Â
# This code is contributed by divyeshrabadiya07 |
C#
// C# program for // the above approach using System; class GFG{ Â
// Function to iterate over the X // quadrant and swap its element // with Y quadrant static void swap( int N, int M, int [,]mat, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int startx_X, int starty_X, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int startx_Y, int starty_Y) { Â Â int row = 0; Â Â int col = 0; Â
  // Iterate over X quadrant   for ( int i = startx_X;; i++)   {     col = 0; Â
    for ( int j = startx_X;; j++)     {       // Swap operations       int temp = mat[i, j]; Â
      mat[i, j] = mat[startx_Y + row,                       starty_Y + col];       mat[startx_Y + row, starty_Y + col] = temp; Â
      col++;       if (col >= M / 2)         break ;     }     row++; Â
    if (row >= N / 2)       break ;   } } Â
// Function to swap the elements // of the two given quadrants static void swapQuadOfMatrix( int N, int M,                              int [,]mat,                              int X, int Y) {   // Swapping the coordinates on   // basis of the value of X and Y Â
  // For Swapping 1st and 2nd Quadrant   if (X == 1 && Y == 2)   {     swap(N, M, mat, 0, 0, 0, M / 2);   } Â
  // For Swapping 1st and 3rd Quadrant   else if (X == 1 && Y == 3)   {     swap(N, M, mat, 0, 0, N / 2, 0);   } Â
  // For Swapping 1st and 4th Quadrant   else if (X == 1 && Y == 4)   {     swap(N, M, mat, 0, 0, N / 2, M / 2);   } Â
  // For Swapping 2nd and 3rd Quadrant   else if (X == 2 && Y == 3)   {     swap(N, M, mat, 0, M / 2, N / 2, 0);   } Â
  // For Swapping 2nd and 4th Quadrant   else if (X == 2 && Y == 4)   {     swap(N, M, mat, 0, M / 2, N / 2, M / 2);   } Â
  // For Swapping 3rd and 4th Quadrant   else if (X == 3 && Y == 4)   {     swap(N, M, mat, N / 2, 0, N / 2, M / 2);   } Â
  // Print the resultant matrix   printMat(N, M, mat); } Â
// Function to print the matrix static void printMat( int N, int M,                      int [,]mat) {   // Iterate over the rows   for ( int i = 0; i < N; i++)   {     // Iterate over the cols     for ( int j = 0; j < M; j++)     {       Console.Write(mat[i, j] + " " );     }     Console.WriteLine();   } } Â
// Driver Code public static void Main(String[] args) {   // Given matrix   int N = 6, M = 6;   int [,] mat = {{1, 2, 3, 4, 5, 6},                 {7, 8, 9, 10, 11, 12},                 {13, 14, 15, 16, 17, 18},                 {19, 20, 21, 22, 23, 24},                 {25, 26, 27, 28, 29, 30},                 {31, 32, 33, 34, 35, 36}}; Â
  // Given quadrants   int X = 1, Y = 4; Â
  // Function Call   swapQuadOfMatrix(N, M, mat, X, Y); } } Â
// This code is contributed by gauravrajput1 |
Javascript
<script> // javascript program for the // above approach Â
   // Function to iterate over the X     // quadrant and swap its element     // with Y quadrant     function swap(         N, M, mat,         startx_X, starty_X,         startx_Y, starty_Y)     {           let row = 0;         let col = 0;           // Iterate over X quadrant         for (let i = startx_X;; i++)         {             col = 0;             for (let j = startx_X;; j++) {                   // Swap operations                 let temp = mat[i][j];                   mat[i][j]                     = mat[startx_Y + row][starty_Y + col];                 mat[startx_Y + row][starty_Y + col] = temp;                   col++;                 if (col >= M / 2)                     break ;             }             row++;               if (row >= N / 2)                 break ;         }     }       // Function to swap the elements     // of the two given quadrants     function swapQuadOfMatrix(         N, M, mat, X, Y)     {         // Swapping the coordinates on         // basis of the value of X and Y           // For Swapping 1st and 2nd Quadrant         if (X == 1 && Y == 2) {             swap(N, M, mat, 0, 0, 0, M / 2);         }           // For Swapping 1st and 3rd Quadrant         else if (X == 1 && Y == 3) {             swap(N, M, mat, 0, 0, N / 2, 0);         }           // For Swapping 1st and 4th Quadrant         else if (X == 1 && Y == 4) {             swap(N, M, mat, 0, 0, N / 2, M / 2);         }           // For Swapping 2nd and 3rd Quadrant         else if (X == 2 && Y == 3) {             swap(N, M, mat, 0,                  M / 2, N / 2, 0);         }           // For Swapping 2nd and 4th Quadrant         else if (X == 2 && Y == 4) {             swap(N, M, mat, 0, M / 2,                  N / 2, M / 2);         }           // For Swapping 3rd and 4th Quadrant         else if (X == 3 && Y == 4) {             swap(N, M, mat, N / 2, 0,                  N / 2, M / 2);         }           // Print the resultant matrix         prletMat(N, M, mat);     }       // Function to print the matrix     function prletMat(N, M, mat)     {         // Iterate over the rows         for (let i = 0; i < N; i++) {               // Iterate over the cols             for (let j = 0; j < M; j++) {                   document.write(                     mat[i][j] + " " );             }             document.write( "<br/>" );         }     }   // Driver Code Â
     // Given matrix         let N = 6, M = 6;         let mat = [[ 1, 2, 3, 4, 5, 6 ],                         [ 7, 8, 9, 10, 11, 12 ],                         [ 13, 14, 15, 16, 17, 18 ],                         [ 19, 20, 21, 22, 23, 24 ],                         [ 25, 26, 27, 28, 29, 30 ],                         [ 31, 32, 33, 34, 35, 36 ]];           // Given quadrants         let X = 1, Y = 4;           // Function Call         swapQuadOfMatrix(N, M, mat, X, Y); Â
// This code is contributed by avijitmondal1998. </script> |
22 23 24 4 5 6 28 29 30 10 11 12 34 35 36 16 17 18 19 20 21 1 2 3 25 26 27 7 8 9 31 32 33 13 14 15
Â
Time Complexity: O(N*M)Â // N is the no of rows and M is the no of columns
Auxiliary Space: O(1)Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!