Minimum Division Operations to Make Array Non Decreasing - Problem
You are given an integer array nums. Any positive divisor of a natural number x that is strictly less than x is called a proper divisor of x.
For example, 2 is a proper divisor of 4, while 6 is not a proper divisor of 6.
You are allowed to perform an operation any number of times on nums, where in each operation you select any one element from nums and divide it by its greatest proper divisor.
Return the minimum number of operations required to make the array non-decreasing. If it is not possible to make the array non-decreasing using any number of operations, return -1.
Input & Output
Example 1 — Basic Violation
$
Input:
nums = [25, 7]
›
Output:
1
💡 Note:
25 > 7 violates non-decreasing. 25's greatest proper divisor is 5, so 25 ÷ 5 = 5. Now [5, 7] is non-decreasing with 1 operation.
Example 2 — Multiple Operations
$
Input:
nums = [7, 21, 3]
›
Output:
-1
💡 Note:
21 > 3 violates constraint. 21 ÷ 7 = 3 (1 op), giving [7, 3, 3]. But 7 > 3 and 7 is prime (GPD = 1), so it cannot be reduced further. Return -1.
Example 3 — Already Non-decreasing
$
Input:
nums = [1, 2, 3, 4]
›
Output:
0
💡 Note:
Array is already non-decreasing, no operations needed.
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code