Ctrl K

Split Numbers Into Order

Hard
You are given an array of positive integers values. In one move you may take any element and replace it with two positive integers that add up to it; the two new numbers take its place, in the order you choose. For example, [5, 9] can become [5, 4, 5] or [2, 3, 9]. Return the smallest number of moves after which the array is non-decreasing, meaning every element is at most the one after it.
Example 1
values=[4, 10, 3] → 4

The trailing 3 stays. 10 has to become at least 4 parts of size at most 3; choosing 2, 2, 3, 3 (three splits) keeps the leftmost part as large as possible. The 4 in front is still bigger than 2, so it becomes 2, 2 (one split). That makes 3 + 1 = 4 splits.

Example 2
values=[1, 2, 2, 7] → 0

The array is already non-decreasing, so nothing needs splitting.

Example 3
values=[6, 2] → 2
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCloning Builders HardOne Reversal for Maximum Roughness HardBiggest Number Divisible by Three HardGrowing Groups Without Repeats HardWord Topping Duel Hard
loading Python…
Input
values=[4, 10, 3]
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 110 tests on the server, hidden ones included.