✅☑[C++/Java/Python/JavaScript] || Easy Approach || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n1. The function areSimilar takes a matrix (mat) and an integer (k) as input.\
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n1. The function `areSimilar` takes a matrix (`mat`) and an integer (`k`) as input.\n1. It creates a new matrix `fin` to store the resultant rotated matrix.\n1. It iterates through each row of the input matrix.\n1. For each row, it performs either a left or right rotation based on the row\'s index.\n1. After rotations, it checks if the original matrix and the rotated matrix are similar.\n1. The function returns a boolean value indicating whether the matrices are similar after rotation.\n\n\n\n# Complexity\n- *Time complexity:*\n   $$O(n*m*k)$$\n    \n\n- *Space complexity:*\n   $$O(n*m)$$\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    // Function to check if matrices after rotation are similar\n    bool areSimilar(vector<vector<int>>& mat, int k) {\n        int len = mat.size(); // Number of rows in the matrix\n        int lent = mat[0].size(); // Number of columns in the matrix\n        \n        // Create a matrix \'fin\' to store the resultant rotated matrix\n        vector<vector<int>> fin(len, vector<int>(lent, 0));\n        \n        // Iterate through each row of the matrix\n        for (int i = 0; i < len; i++) {\n            vector<int> arr(lent);\n            arr = mat[i]; // Get the i-th row of the input matrix\n            \n            // Rotate the row left or right based on its index\n            if (i % 2 == 0) {\n                // Left rotation\n                int kt = k;\n                while (kt > 0) {\n                    int last = arr[0];\n                    for (int p = 1; p < lent; p++) {\n                        arr[p - 1] = arr[p];\n                    }\n                    arr[lent - 1] = last;\n                    kt--;\n                }\n            } else {\n                // Right rotation\n                int kt = k;\n                while (kt > 0) {\n                    int last = arr[lent - 1];\n                    for (int p = lent - 2; p >= 0; p--) {\n                        arr[p + 1] = arr[p];\n                    }\n                    arr[0] = last;\n                    kt--;\n                }\n            }\n            fin[i] = arr; // Store the rotated row in the \'fin\' matrix\n        }\n        \n        bool anst = true;\n        \n        // Check if the original and rotated matrices are similar\n        for (int i = 0; i < len; i++) {\n            for (int j = 0; j < lent; j++) {\n                if (mat[i][j] != fin[i][j]) {\n                    anst = false; // If any element is different, set the flag to false\n                }\n            }\n        }\n        \n        return anst; // Return whether the matrices are similar after rotation\n    }\n};\n\n\n```\n```C []\n\n\nbool areSimilar(int** mat, int matSize, int* matColSize, int k) {\n    int len = matSize;\n    int lent = matColSize[0];\n    \n    int** fin = (int**)malloc(len * sizeof(int*));\n    for (int i = 0; i < len; i++) {\n        fin[i] = (int*)malloc(lent * sizeof(int));\n    }\n    \n    for (int i = 0; i < len; i++) {\n        int* arr = (int*)malloc(lent * sizeof(int));\n        arr = mat[i];\n        \n        if (i % 2 == 0) {\n            // Left rotation\n            int kt = k;\n            while (kt > 0) {\n                int last = arr[0];\n                for (int p = 1; p < lent; p++) {\n                    arr[p - 1] = arr[p];\n                }\n                arr[lent - 1] = last;\n                kt--;\n            }\n        } else {\n            // Right rotation\n            int kt = k;\n            while (kt > 0) {\n                int last = arr[lent - 1];\n                for (int p = lent - 2; p >= 0; p--) {\n                    arr[p + 1] = arr[p];\n                }\n                arr[0] = last;\n                kt--;\n            }\n        }\n        fin[i] = arr;\n    }\n    \n    bool anst = true;\n    for (int i = 0; i < len; i++) {\n        for (int j = 0; j < lent; j++) {\n            if (mat[i][j] != fin[i][j]) {\n                anst = false;\n            }\n        }\n    }\n    \n    return anst;\n}\n\n\n\n\n```\n```Java []\nclass Solution {\n    public boolean areSimilar(int[][] mat, int k) {\n        int len = mat.length;\n        int lent = mat[0].length;\n\n        int[][] fin = new int[len][lent];\n\n        for (int i = 0; i < len; i++) {\n            int[] arr = new int[lent];\n            arr = mat[i].clone();\n\n            if (i % 2 == 0) {\n                // Left rotation\n                int kt = k;\n                while (kt > 0) {\n                    int last = arr[0];\n                    for (int p = 1; p < lent; p++) {\n                        arr[p - 1] = arr[p];\n                    }\n                    arr[lent - 1] = last;\n                    kt--;\n                }\n            } else {\n                // Right rotation\n                int kt = k;\n                while (kt > 0) {\n                    int last = arr[lent - 1];\n                    for (int p = lent - 2; p >= 0; p--) {\n                        arr[p + 1] = arr[p];\n                    }\n                    arr[0] = last;\n                    kt--;\n                }\n            }\n            fin[i] = arr;\n        }\n\n        boolean anst = true;\n        for (int i = 0; i < len; i++) {\n            for (int j = 0; j < lent; j++) {\n                if (mat[i][j] != fin[i][j]) {\n                    anst = false;\n                }\n            }\n        }\n        return anst;\n    }\n}\n\n\n\n```\n```python3 []\nclass Solution:\n    def areSimilar(self, mat, k):\n        len_mat = len(mat)\n        lent_mat = len(mat[0])\n\n        fin = [[0] * lent_mat for _ in range(len_mat)]\n\n        for i in range(len_mat):\n            arr = mat[i][:]\n\n            if i % 2 == 0:\n                # Left rotation\n                kt = k\n                while kt > 0:\n                    last = arr[0]\n                    for p in range(1, lent_mat):\n                        arr[p - 1] = arr[p]\n                    arr[lent_mat - 1] = last\n                    kt -= 1\n            else:\n                # Right rotation\n                kt = k\n                while kt > 0:\n                    last = arr[lent_mat - 1]\n                    for p in range(lent_mat - 2, -1, -1):\n                        arr[p + 1] = arr[p]\n                    arr[0] = last\n                    kt -= 1\n            fin[i] = arr\n\n        anst = True\n        for i in range(len_mat):\n            for j in range(lent_mat):\n                if mat[i][j] != fin[i][j]:\n                    anst = False\n        return anst\n\n\n\n```\n```javascript []\nfunction areSimilar(mat, k) {\n    let len = mat.length;\n    let lent = mat[0].length;\n    \n    let fin = new Array(len);\n    for (let i = 0; i < len; i++) {\n        fin[i] = new Array(lent).fill(0);\n    }\n    \n    for (let i = 0; i < len; i++) {\n        let arr = [...mat[i]];\n        \n        if (i % 2 === 0) {\n            // Left rotation\n            let kt = k;\n            while (kt > 0) {\n                let last = arr[0];\n                for (let p = 1; p < lent; p++) {\n                    arr[p - 1] = arr[p];\n                }\n                arr[lent - 1] = last;\n                kt--;\n            }\n        } else {\n            // Right rotation\n            let kt = k;\n            while (kt > 0) {\n                let last = arr[lent - 1];\n                for (let p = lent - 2; p >= 0; p--) {\n                    arr[p + 1] = arr[p];\n                }\n                arr[0] = last;\n                kt--;\n            }\n        }\n        fin[i] = arr;\n    }\n    \n    let anst = true;\n    for (let i = 0; i < len; i++) {\n        for (let j = 0; j < lent; j++) {\n            if (mat[i][j] !== fin[i][j]) {\n                anst = false;\n            }\n        }\n    }\n    \n    return anst;\n}\n\n\n```\n\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['C++']