We have a square matrix whose size is continuously expanding by a factor of 2. Given a sequence present in the matrix at position (i, j) at any point of time, we need to return sequence present at position (i, (j + N -1)%N) where N is the size of the matrix.
When we say the matrix is expanding, the expanded matrix is formed by multiplying each element of the original 2 x 2 matrix with the current N x N matrix itself. The expanded matrix will have dimensions 2N x 2N.
For Instance, consider below 2x2 matrix, [a b] [c d] Expanding it will result in a 4x4 matrix as follows: ax[a b] bx[a b] [aa ab ba bb] [c d] [c d] [ac ad bc bd] --> [ca cb da db] cx[a b] dx[a b] [cc cd dc dd] [c d] [c d] Expanding it again results in an 8x8 matrix as follows, and so on. ax[aa ab ba bb] bx[aa ab ba bb] [aaa aab aba abb baa bab bba bbb] [ac ad bc bd] [ac ad bc bd] [aac aad abc abd bac bad bbc bbd] [ca cb da db] [ca cb da db] [aca acb ada adb bca bcb bda bdb] [cc cd dc dd] [cc cd dc dd] [acc acd adc add bcc bcd bdc bdd] --> [caa cab cba cbb daa dab dba dbb] cx[aa ab ba bb] dx[aa ab ba bb] [cac cad cbc cbd dac dad dbc dbd] [ac ad bc bd] [ac ad bc bd] [cca ccb cda cdb dca dcb dda ddb] [ca cb da db] [ca cb da db] [ccc ccd cdc cdd dcc dcd ddc ddd] [cc cd dc dd] [cc cd dc dd]
Basically, for a given sequence, we need to find out the sequence just left to it. The matrix may be assumed circular i.e. sequence present at position (i, 0) should return sequence present at position (i, N-1)
Input: str = dda Output: dcb Input: str = cca Output: ddb Input: str = aacbddc Output: aacbdcd
We strongly recommend you to minimize your browser and try this yourself first.
If we carefully analyze, we can see a pattern here.
We start scanning the string from the rightmost position and for each character do the following –
1. If the current character is ‘b’ or ‘d’, change to ‘a’ or ‘c’ respectively and return the string.
2. If the current character is ‘a’ or ‘c’, change it to ‘b’ or ‘d’ respectively and move to the next character to the left. Repeat Step 1 for the next left character.
Left of aacbddc is aacbdcd
This article is contributed by Aditya Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Return an array of anti-diagonals of given N*N square matrix
- Maximum points from top left of matrix to bottom right and return back
- Count of ways to traverse a Matrix and return to origin in K steps
- Element in a matrix starting from which anti-clockwise traversal ends at the last element
- Sum of columns of a 2-D Matrix where first element is odd
- Minimum element of each row and each column in a matrix
- Count majority element in a matrix
- Problem of 8 Neighbours of an element in a 2-D Matrix
- Find maximum element of each row in a matrix
- Search element in a sorted matrix
- Making all elements of matrix equal to a given element K
- Find the element in the matrix generated by given rules
- Construct a matrix such that union of ith row and ith column contains every element from 1 to 2N-1
- Program to find the maximum element in a Matrix
- Search element in a Spirally sorted Matrix
- Print K'th element in spiral form of matrix
- Find maximum element of each column in a matrix
- Maximum neighbor element in a matrix within distance K
- Find sum of all Boundary and Diagonal element of a Matrix
- Replace every matrix element with maximum of GCD of row or column