Minimum Division Operations to Make Array Non Decreasing - Problem

You are given an integer array nums and your task is to transform it into a non-decreasing (sorted) array using a special mathematical operation.

The Operation: You can select any element and divide it by its greatest proper divisor. A proper divisor of a number x is any positive divisor that is strictly less than x.

Examples of proper divisors:

  • For 12: proper divisors are 1, 2, 3, 4, 6 (greatest is 6)
  • For 15: proper divisors are 1, 3, 5 (greatest is 5)
  • For prime numbers like 7: only proper divisor is 1

Your goal is to find the minimum number of operations needed to make the array non-decreasing. If it's impossible, return -1.

Key insight: When you divide a number by its greatest proper divisor, you're essentially reducing it to its smallest prime factor!

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [25, 7]
โ€บ Output: 1
๐Ÿ’ก Note: We need to reduce 25 since 25 > 7. The greatest proper divisor of 25 is 5 (since 25 = 5 ร— 5), so 25 รท 5 = 5. Now we have [5, 7] which is non-decreasing. Total operations: 1.
example_2.py โ€” Impossible Case
$ Input: nums = [7, 7, 6]
โ€บ Output: -1
๐Ÿ’ก Note: We need 7 โ‰ค 6 at index 1, but 7 is prime (only proper divisor is 1), so 7 รท 1 = 7. We cannot reduce 7 further, making it impossible to satisfy 7 โ‰ค 6. Return -1.
example_3.py โ€” Already Sorted
$ Input: nums = [1, 2, 3, 4, 5]
โ€บ Output: 0
๐Ÿ’ก Note: The array is already non-decreasing, so no operations are needed. Return 0.

Visualization

Tap to expand
Number Reduction Pipeline25GPD = 5รท55Prime7No changeKey Mathematical Insights1Greatest Proper Divisor (GPD) is the largest factor less than the number itself2Dividing by GPD gives the smallest prime factor: n รท GPD = smallest_prime_factor3For composite number n = pโ‚ ร— pโ‚‚ ร— ... ร— pโ‚–, GPD = n รท min(pโ‚, pโ‚‚, ..., pโ‚–)4Prime numbers have GPD = 1, so they cannot be reduced furtherโœ“Greedy approach works: always apply minimum reduction needed
Understanding the Visualization
1
Identify Violations
Find elements that are larger than their right neighbors
2
Calculate Greatest Proper Divisor
For each violation, find the largest divisor less than the number itself
3
Apply Division
Divide the number by its GPD to get the smallest possible reduction
4
Repeat Until Sorted
Continue until the array becomes non-decreasing or impossible
Key Takeaway
๐ŸŽฏ Key Insight: The problem reduces to finding the minimum operations to make each element small enough relative to its right neighbor, which is optimally solved by processing right-to-left and applying only necessary reductions.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— โˆšmax(nums))

For each element, we might need to find greatest proper divisor multiple times. Finding GPD takes O(โˆšn) time.

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 2 โ‰ค nums[i] โ‰ค 106
  • All elements are at least 2 (no need to handle 1 or negative numbers)
Asked in
Google 15 Meta 8 Microsoft 6
21.0K Views
Medium Frequency
~25 min Avg. Time
890 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