FastPrepArrange Packages With Compatible Adjacent Weights

Arrange Packages With Compatible Adjacent Weights

Amazon logoAmazon● MediumFULLTIMEPHONE SCREEN
Learn

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[]) → boolean

Examples

Example 1

weights = [9,0,8,1,7]return = true

One valid order is [9,0,8,1,7]. Its adjacent sums are 9, 8, 9, and 8.

Example 2

weights = [9,9,1]return = false

A package of weight 9 can be adjacent only to weight 0. No such separator exists.

Example 3

weights = [6,6,3,3,4]return = true

The 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.

More Amazon problems

See Amazon hiring insights
public boolean canArrangePackages(int[] weights) {
  // write your code here
}
weights[9,0,8,1,7]
expectedtrue
Checking account…