In this HackerEarth Decode problem solution Given an encrypted message, Erwin encodes it the following way:

Removes the median letter of the word from the original word and appends it to the end of the encrypted word and repeats the process until there are no letters left.

A median letter in a word is the letter present in the middle of the word and if the word length is even, the median letter is the left one out of the two middle letters.

Given an encoded string, write a program to decode it.


HackerEarth Decode problem solution


HackerEarth Decode problem solution.

#include <bits/stdc++.h>

using namespace std;

int main()
{
freopen("inp5.txt", "r", stdin);
freopen("out5.txt", "w", stdout);

int t;
cin >> t;

string s, ans;
while (t--) {
cin >> s;
ans.clear();
int len = s.size();
for(int i = 0; i < s.size(); i++ ){
if( len%2 == 0 ){
ans.insert(ans.begin(), s[i]);
}
else{
ans.insert(ans.begin()+ans.size(), s[i]);
}
len--;
}
cout << ans << endl;
}
return 0;
}

Second solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string a;
cin>>a;
string b=a;
int len=a.length(),j=len-1;
for(int i=len-1;i>=0;i-=2)
b[j--]=a[i];
for(int k=(len&1)?1:0;k<len;k+=2)
b[j--]=a[k];j--;
cout<<b<<"\n";
}
return 0;
}