#3752
Lexicographically Smallest Negated Permutation that Sums to Target
medium · 30.9% accepted · 65 likes · top 8%
array · math · two pointers · greedy · sorting
Description
You are given a positive integer n and an integer target.
Return the lexicographically smallest array of integers of size n such that:
- The sum of its elements equals target.
- The absolute values of its elements form a permutation of size n.
If no such array exists, return an empty array.
A permutation of size n is a rearrangement of integers 1, 2, ..., n.
Solution