Description
Description
Editorial
Editorial
Solutions
Solutions
Submissions
Submissions
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 nums is [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 nums is 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 <= 105
  • 1 <= nums[i] <= 109
  • 1 <= k <= 1014
 
Code
Code
Testcase
Testcase
Test Result
Test Result