Home

Its Confidential

Abhi is a spy. It's quite natural for him to go on a secret mission. Once You and Abhi were in the same secret mission when Abhi felt an urgent need to communicate with Dr. Sabitabatra, who appointed both of you for this mission. The only way to communicate with him was by sending letters. As both of you were on a secret mission, Abhi will write the letter with some encrypted words which Dr. Sabitabatra was well aware of. As per encryption is concerned, the encrypted word will start with the middle character of the string and will be formed henceforth with the middle characters of the left and right substrings (of the middle character of the word) and so on. Take a look at the explanation of the sample test case for better comprehension. As the letter was going to be quite long, Abhi wants your help to encrypt some critical words for him so that he can quickly finish the letter and sent it to Dr. Sabitabatra.

Example:

Input:  n = 3, s = "abc"
Output: bac

Approach

C++

#include <bits/stdc++.h>
using namespace std;

string Confidential(string s)
{
    int mid;
    if (s.size() & 1)
        mid = s.size() / 2;
    else
        mid = s.size() / 2 - 1;
    if (mid == 0)
        return s.substr(mid);
    return s[mid] + Confidential(s.substr(0mid)) 
+ Confidential(s.substr(mid + 1));
}
int main()
{

    int n = 3;

    string s = "abc";

    string res = Confidential(s);
    cout << res << "\n";

    return 0;
}


No comments:

Post a Comment