Return previous element in an expanding matrix
Last Updated :
30 Jun, 2022
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]
Expanding it will result in a 4x4 matrix as follows:
ax[a b] bx[a b] [aa ab ba bb]
[ac ad bc bd]
--> [ca cb da db]
cx[a b] dx[a b] [cc cd dc dd]
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)
Examples:
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.
Algorithm:
We start scanning the string from the rightmost position and for each character do the following –
- If the current character is ‘b’ or ‘d’, change to ‘a’ or ‘c’ respectively and return the string.
- 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
string findLeft(string str)
{
int n = str.length();
while (n--)
{
if (str[n] == 'd' )
{
str[n] = 'c' ;
break ;
}
if (str[n] == 'b' )
{
str[n] = 'a' ;
break ;
}
if (str[n] == 'a' )
str[n] = 'b' ;
else if (str[n] == 'c' )
str[n] = 'd' ;
}
return str;
}
int main()
{
string str = "aacbddc" ;
cout << "Left of " << str << " is "
<< findLeft(str);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static StringBuilder findLeft(StringBuilder str)
{
int n = str.length();
while (n > 0 )
{
n--;
if (str.charAt(n) == 'd' )
{
str.setCharAt(n, 'c' );
break ;
}
if (str.charAt(n) == 'b' )
{
str.setCharAt(n, 'a' );
break ;
}
if (str.charAt(n) == 'a' )
str.setCharAt(n, 'b' );
else if (str.charAt(n) == 'c' )
str.setCharAt(n, 'd' );
}
return str;
}
public static void main (String[] args)
{
StringBuilder str = new StringBuilder( "aacbddc" );
System.out.print( "Left of " + str + " is " +
findLeft(str));
}
}
|
Python3
def findLeft( str ):
n = len ( str ) - 1 ;
while (n > 0 ):
if ( str [n] = = 'd' ):
str = str [ 0 :n] + 'c' + str [n + 1 :];
break ;
if ( str [n] = = 'b' ):
str = str [ 0 :n] + 'a' + str [n + 1 :];
break ;
if ( str [n] = = 'a' ):
str = str [ 0 :n] + 'b' + str [n + 1 :];
else if ( str [n] = = 'c' ):
str = str [ 0 :n] + 'd' + str [n + 1 :];
n - = 1 ;
return str ;
if __name__ = = '__main__' :
str = "aacbddc" ;
print ( "Left of" , str , "is" , findLeft( str ));
|
C#
using System;
using System.Text;
public class GFG
{
public static StringBuilder findLeft(StringBuilder str)
{
int n = str.Length;
while (n > 0)
{
n--;
if (str[n] == 'd' )
{
str[n] = 'c' ;
break ;
}
if (str[n] == 'b' )
{
str[n] = 'a' ;
break ;
}
if (str[n] == 'a' )
{
str[n] = 'b' ;
}
else if (str[n] == 'c' )
{
str[n] = 'd' ;
}
}
return str;
}
public static void Main( string [] args)
{
StringBuilder str = new StringBuilder( "aacbddc" );
Console.Write( "Left of " + str + " is " + findLeft(str));
}
}
|
PHP
<?php
function findLeft( $str )
{
$n = strlen ( $str );
while ( $n --)
{
if ( $str [ $n ] == 'd' )
{
$str [ $n ] = 'c' ;
break ;
}
if ( $str [ $n ] == 'b' )
{
$str [ $n ] = 'a' ;
break ;
}
if ( $str [ $n ] == 'a' )
$str [ $n ] = 'b' ;
else if ( $str [ $n ] == 'c' )
$str [ $n ] = 'd' ;
}
return $str ;
}
$str = "aacbddc" ;
echo "Left of " . $str . " is "
. findLeft( $str );
return 0;
?>
|
Javascript
<script>
String.prototype.replaceAt = function (index, replacement)
{
return this .substr(0, index) + replacement + this .substr(index + replacement.length);
}
function findLeft(str)
{
let n = str.length;
while (n > 0)
{
n--;
if (str[n] == 'd' )
{
str = str.replaceAt(n, 'c' );
break ;
}
if (str[n] == 'b' )
{
str = str.replaceAt(n, 'a' );
break ;
}
if (str[n] == 'a' )
str = str.replaceAt(n, 'b' );
else if (str[n] == 'c' )
str = str.replaceAt(n, 'd' );
}
return str;
}
let str = "aacbddc" ;
document.write( "Left of " + str + " is " +
findLeft(str));
</script>
|
Output
Left of aacbddc is aacbdcd
Time complexity: O(n).
Auxiliary Space: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...