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:bcdaExplanation:abcd ------ b / \ acd ------ c / \ ad----- d /a--------------- a Hence, the final string is"bcda".Input:N = 6, str = "gyuitp"Output:utpigyExplanation:gyuitp ------- u / \ gy itp ------- t / /\ gy ip------ p / / gyi----------- 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++

`// C++ implementation of` `// the above appraoch` `#include <iostream>` `using` `namespace` `std;` `// Function to decrypt and` `// print the new string` `void` `decrypt(string Str,` ` ` `int` `Start, ` `int` `End)` `{` ` ` `// If the whole string` ` ` `// has been traversed` ` ` `if` `(Start > End) {` ` ` `return` `;` ` ` `}` ` ` `// To calculate middle` ` ` `// index of the string` ` ` `int` `mid = (Start + End) >> 1;` ` ` `// Print the character` ` ` `// at middle index` ` ` `cout << Str[mid];` ` ` `// Recursively call` ` ` `// for right-substring` ` ` `decrypt(Str, mid + 1, End);` ` ` `// Recursive call` ` ` `// for left-substring` ` ` `decrypt(Str, Start, mid - 1);` `}` `// Driver Code` `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

`// Java implementation of` `// the above appraoch` `class` `GFG{` `// Function to decrypt and` `// print the new String` `static` `void` `decrypt(String Str,` ` ` `int` `Start, ` `int` `End)` `{` ` ` `// If the whole String` ` ` `// has been traversed` ` ` `if` `(Start > End)` ` ` `{` ` ` `return` `;` ` ` `}` ` ` `// To calculate middle` ` ` `// index of the String` ` ` `int` `mid = (Start + End) >> ` `1` `;` ` ` `// Print the character` ` ` `// at middle index` ` ` `System.out.print(Str.charAt(mid));` ` ` `// Recursively call` ` ` `// for right-subString` ` ` `decrypt(Str, mid + ` `1` `, End);` ` ` `// Recursive call` ` ` `// for left-subString` ` ` `decrypt(Str, Start, mid - ` `1` `);` `}` `// Driver Code` `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` `);` `}` `}` `// This code is contributed by sapnasingh4991` |

## Python3

`# Python3 implementation of` `# the above appraoch` `# Function to decrypt and` `# print the new string` `def` `decrypt(` `Str` `, Start, End):` ` ` `# If the whole string` ` ` `# has been traversed` ` ` `if` `(Start > End):` ` ` `return` `;` ` ` ` ` `# To calculate middle` ` ` `# index of the string` ` ` `mid ` `=` `(Start ` `+` `End) >> ` `1` `;` ` ` `# Print the character` ` ` `# at middle index` ` ` `print` `(` `Str` `[mid], end ` `=` `"");` ` ` `# Recursively call` ` ` `# for right-substring` ` ` `decrypt(` `Str` `, mid ` `+` `1` `, End);` ` ` `# Recursive call` ` ` `# for left-substring` ` ` `decrypt(` `Str` `, Start, mid ` `-` `1` `);` `# Driver Code` `N ` `=` `4` `;` `Str` `=` `"abcd"` `;` `decrypt(` `Str` `, ` `0` `, N ` `-` `1` `);` `print` `();` `N ` `=` `6` `;` `Str` `=` `"gyuitp"` `;` `decrypt(` `Str` `, ` `0` `, N ` `-` `1` `);` `# This code is contributed by Code_Mech` |

## C#

`// C# implementation of` `// the above appraoch` `using` `System;` `class` `GFG{` `// Function to decrypt and` `// print the new String` `static` `void` `decrypt(String Str,` ` ` `int` `Start, ` `int` `End)` `{` ` ` `// If the whole String` ` ` `// has been traversed` ` ` `if` `(Start > End)` ` ` `{` ` ` `return` `;` ` ` `}` ` ` `// To calculate middle` ` ` `// index of the String` ` ` `int` `mid = (Start + End) >> 1;` ` ` `// Print the character` ` ` `// at middle index` ` ` `Console.Write(Str[mid]);` ` ` `// Recursively call` ` ` `// for right-subString` ` ` `decrypt(Str, mid + 1, End);` ` ` `// Recursive call` ` ` `// for left-subString` ` ` `decrypt(Str, Start, mid - 1);` `}` `// Driver Code` `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);` `}` `}` `// This code is contributed by Code_Mech` |

## Javascript

`<script>` `// Javascript program for the above approach` `// Function to decrypt and` `// prlet the new String` `function` `decrypt(Str, Start, End)` `{` ` ` `// If the whole String` ` ` `// has been traversed` ` ` `if` `(Start > End)` ` ` `{` ` ` `return` `;` ` ` `}` ` ` `// To calculate middle` ` ` `// index of the String` ` ` `let mid = (Start + End) >> 1;` ` ` `// Prlet the character` ` ` `// at middle index` ` ` `document.write(Str[mid]);` ` ` `// Recursively call` ` ` `// for right-subString` ` ` `decrypt(Str, mid + 1, End);` ` ` `// Recursive call` ` ` `// for left-subString` ` ` `decrypt(Str, Start, mid - 1);` `}` `// Driver Code` ` ` `let N = 4;` ` ` `let Str = ` `"abcd"` `;` ` ` `decrypt(Str, 0, N - 1);` ` ` `document.write(` `"<br/>"` `);` ` ` `N = 6;` ` ` `Str = ` `"gyuitp"` `;` ` ` `decrypt(Str, 0, N - 1);` `// This code is contributed by sanjoy_62.` `</script>` |

**Output:**

bcda utpigy

**Time complexity:** O(N) **Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live**