Talentd
FleetCode

3326. Minimum Division Operations to Make Array Non Decreasing

Medium29.1% acceptance2 companies

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.

Example 1:

Input: nums = [25,7]

Output: 1

Explanation:

Using a single operation, 25 gets divided by 5 and nums becomes [5, 7].

Example 2:

Input: nums = [7,7,6]

Output: -1

Example 3:

Input: nums = [1,1,1,1]

Output: 0

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 106

Asked at 2 companies

Hints open one at a time

Hint 1
Iterate backward from the last index.
Hint 2
Each number can be divided by its largest proper divisor to yield its smallest prime divisor.
15:35
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minOperations(self, nums: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[25,7]
Input
[7,7,6]
Input
[1,1,1,1]
Your code is saved on this device.Solution