Choose values for the array so that it satisfies the uniqueness rule for every prefix, or determine that it is impossible.
You are given an array with some positions fixed and some positions unspecified. Your task is to fill the unspecified positions so that the resulting array satisfies a uniqueness condition related to the values appearing in it.
In the hard version, the constraints are large enough that a direct brute-force search is not feasible. You need to reason about value frequencies and how to assign numbers while keeping the array valid.
If a valid assignment exists, output any one of them. Otherwise, report that it is impossible.
Input Format
- The first line contains an integer — the number of test cases.
- For each test case, the input describes an array with partially specified values.
- The exact encoding of fixed and unspecified entries follows the problem statement for the platform version.
Output Format
For each test case, print one valid completed array if it exists. If no valid completion exists, print -1.
Constraints
- The hard version uses large input sizes.
- You should assume the solution must be near-linear or per test case.
- All values should fit in standard 32-bit signed integers unless otherwise implied by the original platform statement.
Example 1
Input
1 5 1 0 0 2 0
Output
1 3 4 2 5
Explanation
One possible completion is shown. The completed array uses distinct values where needed and satisfies the uniqueness requirement.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.