Problem · Array
Get Minimum Operations to Sort Array
Learn this problemProblem statement
You are given an unsorted positive-integer array arr of length n. You may perform the following operation any number of times:
- Select an element
arr[i]and replace it with two positive integersaandbsuch thata + b = arr[i].
Return the minimum number of operations required to make the resulting array non-decreasing.
Function
getMinimumOperationsToSortArray(arr: int[], n: int) → longExamples
Example 1
arr = [3, 4, 3]n = 3return = 2Split 4 into 2, 2, producing [3, 2, 2, 3]. Then split 3 into 1, 2, producing the non-decreasing array [1, 2, 2, 2, 3]. This uses two operations.
Constraints
1 <= n <= 10^51 <= arr[i] <= 10^9