


N = 12, M = 3 and STR = ‘CODINGNINJAS’

There are three rows (‘M = 3’) in the zig-zag pattern. Row one contains ‘CNN’, row two contains ‘OIGIJS’, and row three contains ‘DNA’. After concatenating the three rows, we get the string ‘CNNOIGIJSDNA’. So, the answer is ‘CNNOIGIJSDNA’.
1. The string ‘STR’ consists of capital letters only (i.e., characters from ‘A-Z’).
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, denoting the size of string ‘STR’ and the number of rows in the zig-zag pattern, respectively.
The second line of each test case contains a string ‘STR’.
For each test case, return the string formed by concatenating all ‘M’ rows when string ‘STR’ is written in a zig-zag pattern.
You do not need to print anything; it has already been taken care of. Just implement the function
1 <= T <= 10^2
1 <= N <= 10^3
1 <= M <= N
‘STR’ contains only ‘A-Z’ characters.
Time Limit: 1 second
Create an array ‘arr’ of size ‘m’, use the array index ‘arr[i]’ to store the string at row ‘i’. Traverse the string ‘str’, identify the character’s row in the current iteration, and append that character to its corresponding row/index in ‘arr’.
We observe that for the first and last row, their index is always incremented by a value of ‘2*(m - 1)’. Let ‘curRow’ be the index of the current row. For the middle rows, if we have to move in the upward direction (i.e., through the last row to the next character in that row), then the index is incremented by a value of ‘2*(m - curRow - 1)’. For the downward direction (i.e., through the first row), the index is incremented by a value of ‘2*(curRow)’. Iterate over each row and append its characters to the answer/output string.