Ternary XOR
Practice this coding question on Coding Gurukul.
Problem statement
A telemetry system combines two base-three signal strings digit by digit. At each position it adds the two digits and keeps the remainder modulo 3, without carrying to another position. The engineer knows the combined signal x and wants two component signals with the smallest possible larger value. Your task Construct equal-length ternary strings a and b without leading zeros such that (a[i] + b[i]) mod 3 = x[i] at every position. Minimize max(a,b), treating both strings as base-three integers. Important details The combined signal starts with 2. Print the smaller component first, so a <= b; this orders the optimal pair deterministically. Digits may be 0, 1 or 2. This operation is digitwise addition modulo 3, not binary XOR and not ordinary addition with carry. Worked example For x = 212, the optimal ordered components are 102 and 110: their digitwise sums modulo 3 are 2, 1 and 2. Their larger value is 110; no valid pair can make that larger value smaller. For x = 222, both components are 111.
Input format
Read t. For each case read n and then a ternary string x of length n.
Output format
For each case print a and b on separate lines, with a <= b and max(a,b) minimized.
Constraints
1 <= t <= 100; 1 <= n <= 3000; total n <= 3000; x starts with 2 and uses only 0, 1, 2.
Sample input
1 3 212
Sample output
102 110
Sign in to run or submit a solution in C++, Java or Python.