Splitting into Digits
Practice this coding question on Coding Gurukul.
Problem statement
A craft kit must contain exactly n beads. Each packet can hold a nonzero single-digit quantity from 1 to 9. The packer wants to use as few different packet sizes as possible, then use as few packets as possible among those choices, so the kit is easy to label and handle. Your task Write n as a sum of digits between 1 and 9. First minimize the number of distinct digits used; among such representations minimize the total number of digits. Print the resulting digits in nondecreasing order. Important details This is an additive split of the value n, not a split of its decimal text. Repeated digits are allowed. The secondary minimum-packet rule gives a deterministic answer for the native judge. Every positive n has a valid split. Worked example For n = 18, two packets of size 9 give 9 + 9 = 18. Only one distinct size is used, and no one-packet split is possible, so print 2 followed by 9 9. For n = 11, the one-size solution requires eleven packets of size 1; mixing 5 and 6 would use more distinct sizes and is therefore worse.
Input format
Read one integer n.
Output format
Print k on the first line and the k digits separated by spaces on the second.
Constraints
1 <= n <= 1000.
Sample input
18
Sample output
2 9 9
Sign in to run or submit a solution in C++, Java or Python.