Thursday, January 9, 2025
Google search engine
HomeData Modelling & AIModify a matrix by rotating ith row exactly i times in clockwise...

Modify a matrix by rotating ith row exactly i times in clockwise direction

Given a matrix mat[][] of dimensions M * N, the task is to print the matrix obtained after rotating every ith row of the matrix i times in a clockwise direction.

Examples:

Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Output:
1 2 3
6 4 5
8 9 7
Explanation:
The 0th row is rotated 0 times. Therefore, the 0th row remains the same as {1, 2, 3}.
The 1st row is rotated 1 times. Therefore, the 1st row modifies to {6, 4, 5}.
The 2nd row is rotated 2 times. Therefore, the 2nd row modifies to {8, 9, 7}.
After completing the above operations, the given matrix modifies to {{1, 2, 3}, {6, 4, 5}, {8, 9, 7}}.

Input: mat[][] = {{1, 2, 3, 4}, {4, 5, 6, 7}, {7, 8, 9, 8}, {7, 8, 9, 8}}
Output:
1 2 3 4
7 4 5 6
9 8 7 8
8 9 8 7

Approach: Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to rotate every i-th
// row of the matrix i times
void rotateMatrix(vector<vector<int> >& mat)
{
    int i = 0;
 
    // Traverse the matrix row-wise
    for (auto& it : mat) {
 
        // Reverse the current row
        reverse(it.begin(), it.end());
 
        // Reverse the first i elements
        reverse(it.begin(), it.begin() + i);
 
        // Reverse the last (N - i) elements
        reverse(it.begin() + i, it.end());
 
        // Increment count
        i++;
    }
 
    // Print final matrix
    for (auto rows : mat) {
        for (auto cols : rows) {
            cout << cols << " ";
        }
        cout << "\n";
    }
}
 
// Driver Code
int main()
{
    vector<vector<int> > mat
        = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    rotateMatrix(mat);
 
    return 0;
}


Java




// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to reverse arr[] from start to end
  static void reverse(int arr[], int start, int end)
  {
    while (start < end) {
      int temp = arr[start];
      arr[start] = arr[end];
      arr[end] = temp;
      start++;
      end--;
    }
  }
 
  // Function to rotate every i-th
  // row of the matrix i times
  static void rotateMatrix(int mat[][])
  {
    int i = 0;
 
    // Traverse the matrix row-wise
    for (int rows[] : mat) {
 
      // Reverse the current row
      reverse(rows, 0, rows.length - 1);
 
      // Reverse the first i elements
      reverse(rows, 0, i - 1);
 
      // Reverse the last (N - i) elements
      reverse(rows, i, rows.length - 1);
 
      // Increment count
      i++;
    }
 
    // Print final matrix
    for (int rows[] : mat) {
      for (int cols : rows) {
        System.out.print(cols + " ");
      }
      System.out.println();
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    int mat[][] = { { 1, 2, 3 },
                   { 4, 5, 6 },
                   { 7, 8, 9 } };
 
    rotateMatrix(mat);
  }
}
 
// This code is contributed by Kingash.


Python3




# Python3 program for the above approach
 
# Function to rotate every i-th
# row of the matrix i times
def rotateMatrix(mat):
     
    i = 0
    mat1 = []
 
    # Traverse the matrix row-wise
    for it in mat:
 
        # Reverse the current row
        it.reverse()
 
        # Reverse the first i elements
        it1 = it[:i]
        it1.reverse()
 
        # Reverse the last (N - i) elements
        it2 = it[i:]
        it2.reverse()
 
        # Increment count
        i += 1
        mat1.append(it1 + it2)
 
    # Print final matrix
    for rows in mat1:
        for cols in rows:
            print(cols, end = " ")
 
        print()
 
# Driver Code
if __name__ == "__main__":
 
    mat = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
     
    rotateMatrix(mat)
 
# This code is contributed by ukasp


C#




// C# program to Modify a matrix
// by rotating ith row exactly
// i times in clockwise direction
using System;
 
class GFG
{
 
  // Reverse each row of matrix
  static void reverse(int N, int[, ] mat, int start, int end)
  {
 
    // Till start < end, swap the element
    // at start and end index
    while (start < end) {
 
      // Swap the element
      int temp = mat[N,start];
      mat[N, start] = mat[N, end];
      mat[N, end] = temp;
 
      // Increment start and decrement
      // end for next pair of swapping
      start++;
      end--;
    
  }
 
  // An Inplace function to
  // rotate a N x N matrix
  // by 90 degrees in anti-
  // clockwise direction
  static void rotateMatrix(int N, int[, ] mat)
  {
    int i = 0;
 
    // Performing Transpose
    for(int j = 0; j < N; j++)
    {
      // Reverse the current row
      reverse(j, mat, 0, N - 1);
      // Reverse the first i elements
      reverse(j, mat, 0, i - 1);
 
      // Reverse the last (N - i) elements
      reverse(j, mat, i, N - 1);
 
      // Increment count
      i++;
    }
 
    for (i = 0; i < N; i++) {
      for (int j = 0; j < N; j++)
        Console.Write(mat[i, j] + " ");
      Console.Write("\n");
    }
 
  }
 
  // Driver Code
  static public void Main()
  {
    int N = 3;
 
    // Test Case 1
    int[, ] mat = { { 1, 2, 3 },
                   { 4, 5, 6 },
                   { 7, 8, 9 }};
 
    rotateMatrix(N, mat);
  }
}
 
// This code is contributed by Aarti_Rathi


Javascript




<script>
// javascript program for the above approach
 
// Function to reverse arr[] from start to end
function  reverse(arr,start,end)
{
    while (start < end) {
      let temp = arr[start];
      arr[start] = arr[end];
      arr[end] = temp;
      start++;
      end--;
    }
}
 
 // Function to rotate every i-th
  // row of the matrix i times
function rotateMatrix(mat)
{
    let i = 0;
  
    // Traverse the matrix row-wise
    for (let rows=0;rows<mat.length;rows++) {
  
      // Reverse the current row
      reverse(mat[rows], 0, mat[rows].length - 1);
  
      // Reverse the first i elements
      reverse(mat[rows], 0, i - 1);
  
      // Reverse the last (N - i) elements
      reverse(mat[rows], i, mat[rows].length - 1);
  
      // Increment count
      i++;
    }
  
    // Print final matrix
    for (let rows=0;rows< mat.length;rows++) {
      for (let cols=0;cols< mat[rows].length;cols++) {
        document.write(mat[rows][cols] + " ");
      }
      document.write("<br>");
    }
}
 
// Driver Code
let mat=[[ 1, 2, 3 ],
                   [ 4, 5, 6 ],
                   [ 7, 8, 9 ]];
rotateMatrix(mat);
 
 
// This code is contributed by avanitrachhadiya2155
</script>


Output: 

1 2 3 
6 4 5 
8 9 7

 

Time Complexity: O(M * N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments