304 North Cardinal St.
Dorchester Center, MA 02124

# Romantic Reversals CodeChef Solution

## Romantic Reversals CodeChef Solution in C++17

``````#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n,k;
cin >> n >> k;
string s;
cin >> s;
string ans;
int first=0, last=k-1;
int c=0;
while(first<=last)
{
if(c%2==0){
ans += s[first];
first++;
}
else{
ans+=s[last];
last--;
}
c++;
}
reverse(ans.begin(), ans.end());
for(int i=k; i<n; ++i){
ans += s[i];
}
cout << ans << endl;
}
}``````

## Romantic Reversals CodeChef Solution in C++14

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

void solve()
{
int n,k;
cin>>n>>k;
string s;
cin>>s;
string ans="";
int  it1=(k/2),it2=(k/2)-1;
if(k%2==1)
{
cout<<s[it1];
it1++;
}
for(int i=0;i<k/2;i++)
{
cout<<s[it1];
cout<<s[it2];
it2--;
it1++;
}
cout<<ans;
for(int i=k;i<n;i++)
{
cout<<s[i];
}
cout<<"\n";
}

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;

cin >> t;
while (t--)
{
solve();
}
return 0;
}``````

## Romantic Reversals CodeChef Solution in PYTH 3

``````# cook your dish here
t=int(input())
for _ in range(t):
n,k=map(int,input().split())
s=input()
x=s[:k]
y=s[k:]
r=""
for i in range(len(x)//2):
r+=x[i]
r+=x[-(i+1)]
if(len(x)%2):
r+=x[len(x)//2]
r=r[::-1]
print(r+y)``````

## Romantic Reversals CodeChef Solution in C

``````#include <stdio.h>

void solve(int n, int k)
{
char str[n];
char ans[n];
char dir = 1;
int i, j = 0, s = k - 1, p = k;
char temp;
for (i = 0; i < n; i++)
scanf("%c", &str[i]);
i = 0;
while (s >= j)
{

if (dir)
{
ans[i++] = str[j++];
dir = 0;
}
else
{
ans[i++] = str[s--];
dir = 1;
}
}
for (i--; i >= 0; i--)
printf("%c", ans[i]);
for (i = p; i < n; i++)
printf("%c", str[i]);
printf("\n");
}
int main()
{
int t, n, k;

scanf("%d", &t);
while (t > 0)
{
t--;
scanf("%d%d\n", &n, &k);
solve(n, k);
}
return 0;
}
``````

## Romantic Reversals CodeChef Solution in JAVA

``````import java.io.*;
import java.lang.*;
import java.util.*;

//Atg
class Codechef {

public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int t = 1;
t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
String a = sc.next();
//   char[] brr = new char[n + 1];
String str = a;
StringBuilder brr = new StringBuilder(str);

int ind = 0;
int tmp = 1;
if (k % 2 == 1) {
tmp = 2;
}
int kk = k;

for (int i = k + 1; i < n + 1; i++) {
brr.setCharAt(i-1, a.charAt(i - 1));
}

while (k > 0) {
brr.setCharAt(k-1, a.charAt(ind++));
k -= 2;
}

while (tmp < kk) {
brr.setCharAt(tmp-1, a.charAt(ind++));
tmp += 2;
}

System.out.print(brr);
System.out.println();
}
}
}
``````

## Romantic Reversals CodeChef Solution in PYPY 3

``````T = int(input())

for _ in range(T):
N, K = map(int, input().split())

S = input()
lst = list(S)
s = ''
i = 0
j = K-1
l = K-1
while i < j:
lst[l] = S[i]
l -= 1
lst[l] = S[j]
l-= 1
j -= 1
i += 1

if i == j:
lst[l] = S[i]
print(''.join(lst))``````

## Romantic Reversals CodeChef Solution in PYTH

``````t = int(raw_input())
for i in range(t):
st = raw_input().split()
N = int(st[0])
K = int(st[1])
S = raw_input().strip()
p = K/2
st = S[p]
if K%2 == 0:
m = -1
else:
m = 1
# endif
for k in range(1,K):
p += m*k
m = -1*m
st += S[p]
# endfor k
for k in range(K,N):
st += S[k]
# endfor k
print st
# endfor i
``````

## Romantic Reversals CodeChef Solution in C#

``````// Problem: RMNTREV - Romantic Reversals
// Author: Gusztav Szmolik

using System;
using System.Text;

public class RomanticReversals
{
public static int Main ()
{
ushort t = UInt16.Parse (Console.ReadLine ());
string[] parts = null;
for (ushort i = 0; i < t; i++)
{
uint n = UInt32.Parse (parts[0]);
uint k = UInt32.Parse (parts[1]);
StringBuilder ans = new StringBuilder ();
bool oddLength = (k%2 == 1);
if (oddLength)
ans.Append (s[Convert.ToInt32 ((k-1)/2)]);
for (uint j = (oddLength ? (k+1)/2 : k/2); j < k; j++)
{
ans.Append (s[(int)j]);
ans.Append (s[Convert.ToInt32 (k-1-j)]);
}
ans.Append (s.Substring ((int)k));
Console.WriteLine (ans);
}
return 0;
}
}``````

## Romantic Reversals CodeChef Solution in GO

``````package main

import (
"bufio"
"bytes"
"fmt"
"os"
)

func main() {

var buf bytes.Buffer
for tc > 0 {
tc--
res := solve(n, k, S)
buf.WriteString(fmt.Sprintf("%s\n", res))
}
fmt.Println(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
i := from
sign := 1
if bytes[i] == '-' {
sign = -1
i++
}
tmp := 0
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + int(bytes[i]-'0')
i++
}
*val = tmp * sign
return i
}

return
}

a, b = res[0], res[1]
return
}

a, b, c = res[0], res[1], res[2]
return
}

res := make([]int, n)
x := 0
for i := 0; i < n; i++ {
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
x++
}
}
return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
i := from

var tmp uint64
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + uint64(bytes[i]-'0')
i++
}
*val = tmp

return i
}
func solve(n, k int, X string) string {
buf := make([]byte, n)
copy(buf[k:], X[k:])
l, r := 0, k-1
pos := k - 1
for l <= r {
buf[pos] = X[l]
if pos > 0 {
buf[pos-1] = X[r]
}
pos -= 2
l++
r--
}

return string(buf)
}``````
##### Romantic Reversals CodeChef Solution Review:

In our experience, we suggest you solve this Romantic Reversals CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Romantic Reversals CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Romantic Reversals CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!