hardMixed

TargetWeightNodeRearrangement Solution

Problem Statement

You are given an array of node weights and two target values: the total weight of all nodes and the target sum of adjacent node weights. Determine if it's possible to rearrange the nodes to meet these conditions.

Examples

Example 1:
Input:[-2, -3, 4]
Output:true
Explanation: The total weight is 4, and possible pairs are -2 and 6 or -3 and 7.
Example 2:
Input:[1, 2, 3, 4]
Output:true
Explanation: Possible pairs are 1 and 4, 2 and 3.

Constraints

  • The array of node weights must not be null.
  • The total weight of all nodes and target sum of adjacent node weights must be non-negative.
  • The length of the array must be greater than one.
Time: O(n log n) Space: O(n)
Use dynamic programming or binary search to solve the problem in O(n log n) time or better.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.