As reported: Google L3 onsite, round 4 (Bangalore, 2025): a list of
Nelements, each a pair(value, can_decrement). Ifcan_decrementis true, the value may only be decremented, any number of times; otherwise it may only be incremented. Determine whether all values can be turned into a permutation of1 … N; if yes return the mapping from original to transformed values, otherwise an empty result. This round was the toughest of the loop.
You are given values and canDecrement, both of length N. Element i can be changed to any integer x with
x ≤ values[i]whencanDecrement[i]is true,x ≥ values[i]otherwise
(including leaving it unchanged). Make the N results a permutation of 1 … N.
Return an array result where result[i] is the new value of element i, or an empty array if it is impossible.
Example
values = [3, 1, 2], canDecrement = [true, false, true]
-> [2, 3, 1] 3 goes down to 2, 1 goes up to 3, 2 goes down to 1
values = [1, 1], canDecrement = [true, true]
-> [] both can only become 1 or less
values = [5, 0, 4, 4], canDecrement = [true, false, false, true]
-> [2, 3, 4, 1] (any valid answer is accepted, e.g. [3, 2, 4, 1])
Clarifications
- Values may repeat, and may lie outside
1 … N. - If several answers exist, return any of them.
Constraints: 1 ≤ N ≤ 2·10^5; −10^9 ≤ values[i] ≤ 10^9.
Code runner format: the first line is N, then N lines value flag with flag = 1 when the element can only be decremented and 0 when it can only be incremented. Implement makePermutation(values, canDecrement). The harness checks your array and prints OK for a valid permutation, IMPOSSIBLE for an empty array, or INVALID with the reason.
Follow-ups
- How many elements could be moved from the decrement-only group to the increment-only group while a permutation is still possible?