Construct an array of the required length that satisfies parity-related constraints, or determine that it is impossible.
Problem
You are given the length of an array and a set of parity-related requirements for its elements. Your task is to construct any integer array that satisfies all requirements and has uniform parity behavior according to the rules of the problem.
In this type of task, each position may need to match a specified parity condition, and the final array must obey every constraint simultaneously. If no valid array exists, return an empty result or the problem’s failure indicator.
Because multiple valid answers may exist, any correct construction is accepted.
Input Format
- An integer describing the array size.
- Additional values or constraints describing the parity requirements for each position.
Output Format
- A valid integer array satisfying the constraints, if one exists.
- Otherwise, return the appropriate empty / impossible result.
Constraints
- The array size is positive.
- Element values should fit within standard integer ranges.
- The solution should be efficient enough for interview-style input sizes.
Hints
- Track how many odd and even values are needed.
- Build the answer greedily while checking whether a choice keeps the remaining constraints feasible.
- If a constraint cannot be satisfied, fail early rather than continuing to fill the array.
Input Format
- Read the array length and the parity constraints.
- Constraints may specify required parity for certain positions or counts of parity groups.
- Exact input details depend on the platform wrapper.
Output Format
- Return one valid constructed array.
- If no valid construction exists, return the designated impossible result.
Constraints
- Use only integers in the output array.
- Satisfy all parity constraints exactly.
- If multiple answers are valid, any one is acceptable.
Example 1
Input
n = 5 requirements = [odd, even, odd, even, odd]
Output
[1, 2, 3, 4, 5]
Explanation
Each position matches the required parity, so this is a valid construction.
Example 2
Input
n = 4 requirements = [even, even, odd, odd]
Output
[2, 4, 1, 3]
Explanation
This array satisfies the parity pattern exactly. Many other answers are also valid.
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.