Medium
You are given an integer array nums and an integer k.
A rearrangement of nums is called valid if no sum is equal to k.
Return any valid rearrangement of nums. If no valid rearrangement exists, return an empty array.
Example 1:
Input: nums = [3,1,4,2], k = 5
Output: [1,2,3,4]
Explanation:
- One of the valid rearrangements of
numsis[1, 2, 3, 4]. - The prefix sums for this arrangement are 1, 3, 6, and 10, none of which are equal to
k = 5.
Example 2:
Input: nums = [1,2,3], k = 6
Output: []
Explanation:
- The sum of all elements of
numsis 6, so the prefix sum of the entire array is always 6 regardless of the rearrangement. - Therefore, no valid rearrangement exists, so the answer is an empty array.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1091 <= k <= 1014