Hello coders, today we are going to solve Arranging the Appetizers Codechef Solution.

Problem
The Chef has prepared the appetizers in the shapes of letters to spell a special message for the guests. There are n appetizers numbered from 0 to n-1 such that if the appetizers are arrayed in this order, they will display the message. The Chef plans to display them in this order on a table that can be viewed by all guests as they enter. The appetizers will only be served once all guests are seated.
The appetizers are not necessarily finished in the same order as they are numbered. So, when an appetizer is finished the Chef will write the number on a piece of paper and place it beside the appetizer on a counter between the kitchen and the restaurant. A server will retrieve this appetizer and place it in the proper location according to the number written beside it.
The Chef has a penchant for binary numbers. The number of appetizers created is a power of 2, say n = 2k. Furthermore, he has written the number of the appetizer in binary with exactly k bits. That is, binary numbers with fewer than k bits are padded on the left with zeros so they are written with exactly k bits.
Unfortunately, this has unforseen complications. A binary number still “looks” binary when it is written upside down. For example, the binary number “0101” looks like “1010” when read upside down and the binary number “110” looks like “011” (the Chef uses simple vertical lines to denote a 1 bit). The Chef didn’t realize that the servers would read the numbers upside down so he doesn’t rotate the paper when he places it on the counter. Thus, when the server picks up an appetizer they place it the location indexed by the binary number when it is read upside down.
You are given the message the chef intended to display and you are to display the message that will be displayed after the servers move all appetizers to their locations based on the binary numbers they read.
Input
The first line consists of a single integer T ≤ 25 indicating the number of test cases to follow. Each test case consists of a single line beginning with an integer 1 ≤ k ≤ 16 followed by a string of precisely 2k characters. The integer and the string are separated by a single space. The string has no spaces and is composed only of lower case letters from `a` to `z`.
Output
For each test case you are to output the scrambled message on a single line.
Example
Sample Input 1
2
2 chef
4 enjoyourapplepie
Sample Output 1
cehf
eayejpuinpopolre
Arranging the Appetizers CodeChef Solution in JAVA
import java.util.Scanner; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int tc = 0; tc < T; tc++) { int k = sc.nextInt(); String message = sc.next(); System.out.println(solve(k, message)); } sc.close(); } static String solve(int k, String message) { return IntStream.range(0, message.length()) .mapToObj(i -> message.charAt(Integer.parseInt( new StringBuilder(String.format("%" + k + "s", Integer.toBinaryString(i)).replace(' ', '0')) .reverse().toString(), 2))) .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString(); } }
Arranging the Appetizers CodeChef Solution in CPP
#include <cstdio> #include <string> using namespace std; char Buffer[65537]; struct Solution { int K; int Reverse(int x) { int answer = 0; for (int i = 0; i < K; ++i) { answer += answer + x % 2; x /= 2; } return answer; } void Solve() { scanf("%d", &K); scanf("%s", Buffer); string s = Buffer; string s1(s.size(), 0); for (int i = 0; i < (1 << K); ++i) { s1[Reverse(i)] = s[i]; } printf("%s\n", s1.c_str()); } }; int main() { int t; scanf("%d", &t); for (int i = 0; i < t; ++i) Solution().Solve(); return 0; }
Arranging the Appetizers CodeChef Solution in Python
T = int(input()) for i in range(T): K, text = input().strip().split() K = int(K) L = len(text) arr = [' ' for _ in range(L)] for i in range(L): binStr = str(bin(i))[2:] binStr = ('0' * (K - len(binStr))) + binStr j = int(binStr[::-1], 2) arr[j] = text[i] print("".join(arr))
Disclaimer: The above Problem (Arranging the Appetizers) is generated by CodeChef but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purpose.