C++ Return Previous Element in an Expanding Matrix


Discuss a problem based on expanding the matrix. Expanding matrix is a matrix whose size continuously increases by some factor.

Here we have a matrix of characters whose size is expanding by a factor of 2, i.e., if the original size of the matrix is N * N, then the size of the expanded matrix becomes 2N * 2N. We are given a sequence of characters present at ( i, j ), and we need to return the sequence of characters present at (i, (j - N - 1)%N).

Let’s understand by visualizing some initial expanded matrix,

Given Matrix -> [ a, b ] [ c, d ], 2 X 2 matrix
Multiplying with { a, b, c, d }
A X [ a, b ]
B X [ a, b ]
C X [ a, b ]
D X [ a, b ]
[ c, d ] [ c, d ] [ c, d ] [ c, d ]

Expanded Matrix -> [ aa, ab, ba, bb ]
[ ac, ad, bc, bd ]
[ ca, cb, da, db ]
[ cc, cd, dc, dd ], 4X4 matrix
To expand again, multiply it by { a, b, c, d } and a matrix of size 8X8 will be formed.

Expanded Matrix - > [ aaa, aab, aba, abb, baa, bab, bba, bbb ]
[ aac, aad, abc, abd, bac, bad, bbc, bbd ]
[ aca, acb, ada, adb, bca, bcb, bda, bdb ]
[ acc, acd, adc, add, bcc, bcd, bdc, bdd ]
[ caa, cab, cba, cbb, daa, dab, dba, dbb ]
[ cac, cad, cbc, cbd, dac, dad, dbc, dbd ]
[ cca, ccb, cda, cdb, dca, dcb, dda, ddb ]
[ ccc, ccd, cdc, cdd, dcc, dcd, ddc, ddd ]

Here are two initial expanded matrices; so let’s say we are given a sequence of characters “bcc,” then we need to return the sequence just left to it, i.e., “add.” Also, the matrix is assumed circular, i.e., if the given sequence is at (i, 0), then return the sequence at (i, N-1) for example

Input: abb
Output: aba
Explanation: The sequence just left to abb is aba in the 8X8 matrix.

Input: aadc
Output: aacd

Input: abbcd
Output: abbcc

Approach to Find the Solution

Looking at the problem first and the only solution that comes to mind is to find the expanded matrix that contains the given sequence but doesn't look very complex. We need first to form the matrix and then search for the sequence.

Efficient Approach

After looking at some initially expanded matrices, we found a pattern through which we can see the previous element. I.e

  • Traverse through the sequence of characters from the last index.

  • If the indexed element is 'b' or 'd', then change it to 'a' or 'c' and stop traversing the array.

  • If the indexed element is 'a' or 'c,' change it to 'b' or 'd' and move to the next index and check it.

Example

C++ Code for the Above Approach 

#include <bits/stdc++.h>
using namespace std;
int main (){
   string seq = "abbcd";
   int n = seq.length ();
   // traverse through the string from last.
   for (int i = n; i >= 0; i--){
      // if the element is b or d, change them and stop traversing.
      if (seq[i] == 'b'){
      seq[i] = 'a';
      break;
   }
   if (seq[i] == 'd'){
      seq[i] = 'c';
      break;
   }
   // if an element is b or d, change them and move to the next element.
   if (seq[i] == 'a')
      seq[i] = 'b';
   else if (seq[i] == 'c')
      seq[i] = 'd';
   }
   cout << "The Previous sequence is: " << seq;
   return 0;
}

Output

The previous sequence is: abbcc

Conclusion

In this article, we discussed the expanding character matrix and how it formed. We also discussed the problem of finding the previous element in an expanding matrix. We solved this problem by understanding the pattern created by expanding the matrix of characters.

We also discussed C++ code for this problem which we can write in any programming language like C, Java, Python, etc. We hope you find this tutorial helpful.

Updated on: 26-Nov-2021

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements