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: 2
💡 Note: 21 > 3 violates constraint. 21 ÷ 7 = 3 (1 op), then 7 > 3 so 7 ÷ 7 = 1 (1 op). Total: 2 operations for [1, 3, 3].
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
Minimum Division Operations to Make Array Non-DecreasingStep 1: Input25725 > 7 ✗Step 2: Operation25 ÷ 5 = 5(5 is greatest proper divisor)Step 3: Result575 ≤ 7 ✓Key ConceptGreatest Proper Divisor:Largest divisor < nFor 25: divisors are 1,5,25 → GPD = 5Answer: 1 operation needed
Understanding the Visualization
1
Input Array
Given array [25, 7] with violation 25 > 7
2
Apply Operation
Divide 25 by its greatest proper divisor 5
3
Result
Array becomes [5, 7] which is non-decreasing
Key Takeaway
🎯 Key Insight: Each number can only be reduced to its smallest prime factor, so we greedily process violations from right to left
Asked in
Google 15 Microsoft 12 Amazon 8
8.5K Views
Medium Frequency
~25 min Avg. Time
234 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen