
Let the matrix be and let 'K' will be 1:
1 2 3
4 5 6
7 8 9
The new matrix will be:
3 1 2
6 4 5
9 7 8
The first line of input contains an integer ‘T,’ denoting the number of test cases. The test cases follow.
The first line of each test case contains a single integer ‘N’ denoting the number of rows and columns of the matrix respectively.
Next line will contain the integer ‘K’.
The next ‘N’ lines of each test case contain ‘N’ integers in each line.
For each test case, if Ninja managed to solve the problem, print the rotated matrix.
You don’t need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
2 <= N <= 10 ^ 2
1 <= K <= 10 ^ 2
Time Limit: 1 sec
Consider each row as an array and then rotate the array. This can be done by copying the elements from Kth column to the (N - 1)th column to the starting of the array using a temporary array and then remaining elements from the start to K - 1 to the end of the array.
The steps are as follows:
The idea is to rotate each row K times, K % N *K elements from the back end of the array come to the front and the rest of the elements from the front shift backwards.
In this approach, we firstly reverse all the elements of the array. Then, reversing the first K elements followed by reversing the rest N - K * N - K elements give us the required result.