|
| 1 | +<h2><a href="https://leetcode.com/problems/lexicographically-smallest-negated-permutation-that-sums-to-target">4077. Lexicographically Smallest Negated Permutation that Sums to Target</a></h2><h3>Medium</h3><hr><p>You are given a positive integer <code>n</code> and an integer <code>target</code>.</p> |
| 2 | + |
| 3 | +<p>Return the <strong><span data-keyword="lexicographically-smaller-array">lexicographically smallest</span></strong> array of integers of size <code>n</code> such that:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>The <strong>sum</strong> of its elements equals <code>target</code>.</li> |
| 7 | + <li>The <strong>absolute values</strong> of its elements form a <strong>permutation</strong> of size <code>n</code>.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>If no such array exists, return an empty array.</p> |
| 11 | + |
| 12 | +<p>A <strong>permutation</strong> of size <code>n</code> is a rearrangement of integers <code>1, 2, ..., n</code>.</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<div class="example-block"> |
| 18 | +<p><strong>Input:</strong> <span class="example-io">n = 3, target = 0</span></p> |
| 19 | + |
| 20 | +<p><strong>Output:</strong> <span class="example-io">[-3,1,2]</span></p> |
| 21 | + |
| 22 | +<p><strong>Explanation:</strong></p> |
| 23 | + |
| 24 | +<p>The arrays that sum to 0 and whose absolute values form a permutation of size 3 are:</p> |
| 25 | + |
| 26 | +<ul> |
| 27 | + <li><code>[-3, 1, 2]</code></li> |
| 28 | + <li><code>[-3, 2, 1]</code></li> |
| 29 | + <li><code>[-2, -1, 3]</code></li> |
| 30 | + <li><code>[-2, 3, -1]</code></li> |
| 31 | + <li><code>[-1, -2, 3]</code></li> |
| 32 | + <li><code>[-1, 3, -2]</code></li> |
| 33 | + <li><code>[1, -3, 2]</code></li> |
| 34 | + <li><code>[1, 2, -3]</code></li> |
| 35 | + <li><code>[2, -3, 1]</code></li> |
| 36 | + <li><code>[2, 1, -3]</code></li> |
| 37 | + <li><code>[3, -2, -1]</code></li> |
| 38 | + <li><code>[3, -1, -2]</code></li> |
| 39 | +</ul> |
| 40 | + |
| 41 | +<p>The lexicographically smallest one is <code>[-3, 1, 2]</code>.</p> |
| 42 | +</div> |
| 43 | + |
| 44 | +<p><strong class="example">Example 2:</strong></p> |
| 45 | + |
| 46 | +<div class="example-block"> |
| 47 | +<p><strong>Input:</strong> <span class="example-io">n = 1, target = 10000000000</span></p> |
| 48 | + |
| 49 | +<p><strong>Output:</strong> <span class="example-io">[]</span></p> |
| 50 | + |
| 51 | +<p><strong>Explanation:</strong></p> |
| 52 | + |
| 53 | +<p>There are no arrays that sum to <span class="example-io">10000000000 and whose absolute values form a permutation of size 1. Therefore, the answer is <code>[]</code>.</span></p> |
| 54 | +</div> |
| 55 | + |
| 56 | +<p> </p> |
| 57 | +<p><strong>Constraints:</strong></p> |
| 58 | + |
| 59 | +<ul> |
| 60 | + <li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 61 | + <li><code>-10<sup>10</sup> <= target <= 10<sup>10</sup></code></li> |
| 62 | +</ul> |
0 commit comments