3326. Minimum Division Operations to Make Array Non Decreasing LeetCode Solution

In this guide, you will get 3326. Minimum Division Operations to Make Array Non Decreasing LeetCode Solution with the best time and space complexity. The solution to Minimum Division Operations to Make Array Non Decreasing problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Division Operations to Make Array Non Decreasing solution in C++
  4. Minimum Division Operations to Make Array Non Decreasing solution in Java
  5. Minimum Division Operations to Make Array Non Decreasing solution in Python
  6. Additional Resources
3326. Minimum Division Operations to Make Array Non Decreasing LeetCode Solution image

Problem Statement of Minimum Division Operations to Make Array Non Decreasing

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:

See also  1754. Largest Merge Of Two Strings LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(n \cdot \sqrt{(\max(\texttt{nums}))})
  • Space Complexity: O(1)

3326. Minimum Division Operations to Make Array Non Decreasing LeetCode Solution in C++

class Solution {
 public:
  int minOperations(vector<int>& nums) {
    int ans = 0;

    for (int i = nums.size() - 2; i >= 0; --i)
      if (nums[i] > nums[i + 1]) {
        const int minDivisor = getMinDivisor(nums[i]);
        if (minDivisor > nums[i + 1])
          return -1;
        nums[i] = minDivisor;
        ++ans;
      }

    return ans;
  }

 private:
  int getMinDivisor(int num) {
    for (int divisor = 2; divisor <= sqrt(num); ++divisor)
      if (num % divisor == 0)
        return divisor;
    return num;
  }
};
/* code provided by PROGIEZ */

3326. Minimum Division Operations to Make Array Non Decreasing LeetCode Solution in Java

class Solution {
  public int minOperations(int[] nums) {
    int ans = 0;

    for (int i = nums.length - 2; i >= 0; --i)
      if (nums[i] > nums[i + 1]) {
        final int minDivisor = getMinDivisor(nums[i]);
        if (minDivisor > nums[i + 1])
          return -1;
        nums[i] = minDivisor;
        ++ans;
      }

    return ans;
  }

  private int getMinDivisor(int num) {
    for (int divisor = 2; divisor <= Math.sqrt(num); ++divisor)
      if (num % divisor == 0)
        return divisor;
    return num;
  }
}
// code provided by PROGIEZ

3326. Minimum Division Operations to Make Array Non Decreasing LeetCode Solution in Python

class Solution:
  def minOperations(self, nums: list[int]) -> int:
    ans = 0

    for i in range(len(nums) - 2, -1, -1):
      if nums[i] > nums[i + 1]:
        minDivisor = self._getMinDivisor(nums[i])
        if minDivisor > nums[i + 1]:
          return -1
        nums[i] = minDivisor
        ans += 1

    return ans

  def _getMinDivisor(self, num: int) -> int:
    for divisor in range(2, math.isqrt(num) + 1):
      if num % divisor == 0:
        return divisor
    return num
# code by PROGIEZ

Additional Resources

See also  3426. Manhattan Distances of All Arrangements of Pieces LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.