Arrange Packages With Compatible Adjacent Weights
Problem statement
You are given an array weights, where every package weight is an integer from 0 through 9.
Determine whether all packages can be rearranged so that the sum of every pair of adjacent weights is strictly less than 10.
Return true if such a rearrangement exists, or false otherwise.
Function
canArrangePackages(weights: int[]) → booleanExamples
Example 1
weights = [9,0,8,1,7]return = trueOne valid order is [9,0,8,1,7]. Its adjacent sums are 9, 8, 9, and 8.
Example 2
weights = [9,9,1]return = falseA package of weight 9 can be adjacent only to weight 0. No such separator exists.
Example 3
weights = [6,6,3,3,4]return = trueThe order [6,3,6,3,4] is valid.
Constraints
1 <= weights.length <= 200000.0 <= weights[i] <= 9.- Every package must appear exactly once in the rearrangement.