Given a string str of length N, the task is to decrypt it using a given set of decryption rules and print the decrypted string.
The decryption rules are as follows:
- Start with the middle character of the string str and print it.
- Repetitively traverse the right substring and print its middle character.
- Repeat the same procedure for the left substring too.
Examples:
Input: N = 4, str = "abcd"
Output: bcda
Explanation:
abcd ------ b
/ \
a cd ------ c
/ \
a d ----- d
/
a --------------- a
Hence, the final string is "bcda".
Input: N = 6, str = "gyuitp"
Output: utpigy
Explanation:
gyuitp ------- u
/ \
gy itp ------- t
/ /\
gy i p ------ p
/ /
gy i ----------- i
/
gy --------------- g
\
y -------------- y
Hence, the final string is "utpigy".
Approach:
The main idea is to use recursion. Keep on dividing the whole string into left and right substrings and print the middle element of every such substring, until the string is left with a single character and can not be further divided.
Detailed steps for this approach are as follows:
- Initialize start = 0, end = N -1, denoting the first and last character of the string.
- Print the character at the middle of the string, that is mid = (start + end) / 2.
- Recursively traverse its right substring (start = mid +1, end) followed by its left substring (start, mid – 1).
- Repeat the above steps for each substring traversed. Continue until the entire string is traversed and print the given string.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void decrypt(string Str,
int Start, int End)
{
if (Start > End) {
return ;
}
int mid = (Start + End) >> 1;
cout << Str[mid];
decrypt(Str, mid + 1, End);
decrypt(Str, Start, mid - 1);
}
int main()
{
int N = 4;
string Str = "abcd" ;
decrypt(Str, 0, N - 1);
cout << "\n" ;
N = 6;
Str = "gyuitp" ;
decrypt(Str, 0, N - 1);
return 0;
}
|
Java
class GFG{
static void decrypt(String Str,
int Start, int End)
{
if (Start > End)
{
return ;
}
int mid = (Start + End) >> 1 ;
System.out.print(Str.charAt(mid));
decrypt(Str, mid + 1 , End);
decrypt(Str, Start, mid - 1 );
}
public static void main(String[] args)
{
int N = 4 ;
String Str = "abcd" ;
decrypt(Str, 0 , N - 1 );
System.out.print( "\n" );
N = 6 ;
Str = "gyuitp" ;
decrypt(Str, 0 , N - 1 );
}
}
|
Python3
def decrypt( Str , Start, End):
if (Start > End):
return ;
mid = (Start + End) >> 1 ;
print ( Str [mid], end = "");
decrypt( Str , mid + 1 , End);
decrypt( Str , Start, mid - 1 );
N = 4 ;
Str = "abcd" ;
decrypt( Str , 0 , N - 1 );
print ();
N = 6 ;
Str = "gyuitp" ;
decrypt( Str , 0 , N - 1 );
|
C#
using System;
class GFG{
static void decrypt(String Str,
int Start, int End)
{
if (Start > End)
{
return ;
}
int mid = (Start + End) >> 1;
Console.Write(Str[mid]);
decrypt(Str, mid + 1, End);
decrypt(Str, Start, mid - 1);
}
public static void Main()
{
int N = 4;
String Str = "abcd" ;
decrypt(Str, 0, N - 1);
Console.Write( "\n" );
N = 6;
Str = "gyuitp" ;
decrypt(Str, 0, N - 1);
}
}
|
Javascript
<script>
function decrypt(Str, Start, End)
{
if (Start > End)
{
return ;
}
let mid = (Start + End) >> 1;
document.write(Str[mid]);
decrypt(Str, mid + 1, End);
decrypt(Str, Start, mid - 1);
}
let N = 4;
let Str = "abcd" ;
decrypt(Str, 0, N - 1);
document.write( "<br/>" );
N = 6;
Str = "gyuitp" ;
decrypt(Str, 0, N - 1);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N)