Trains the technique from
LeetCode 3752Lexicographically Smallest Negated Permutation that Sums to TargetThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
The numbers 1 through n are each written down once. Any of them may be turned over, which replaces it with its negative. The numbers are then laid out in a row in whatever order you like.
Return the row whose entries total target and which comes out smallest when rows are compared entry by entry from the front. Return an empty list when no row can total target.
Example 1
The numbers 1 to 4 total 10, so 4 has to be turned over to bring the total down to 2. Turning the largest one over puts the smallest possible entry at the front.
Example 2
The numbers 1 and 2 total 3, and every turn changes the total by an even amount, so an even target can never be reached.
Example 3
Every number is turned over, which is the only way to reach -6.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def lex_smallest_negated_perm(n: int, target: int) -> list[int]:public int[] lexSmallestNegatedPerm(int n, long target)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.