You are given an integer array nums.
An array is called beautiful if for every index i > 0, the value at nums[i] is divisible by nums[i - 1].
In one operation, you may increment any element nums[i] (with i > 0) by 1.
Return the minimum number of operations required to make the array beautiful.
Example 1:
Input: nums = [3,7,9]
Output: 2
Explanation:
Applying the operation twice on nums[1] makes the array beautiful: [3,9,9]
Example 2:
Input: nums = [1,1,1]
Output: 0
Explanation:
The given array is already beautiful.
Example 3:
Input: nums = [4]
Output: 0
Explanation:
The array has only one element, so it's already beautiful.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 50Problem Overview: You are given an array and need the minimum number of operations required to transform it into a beautiful array according to the rules defined in the problem. The challenge is deciding where modifications are necessary while minimizing the total number of operations.
Approach 1: Brute Force Simulation (O(n^2) time, O(1) space)
The most direct idea is to simulate possible fixes whenever the beauty rule is violated. Iterate through the array and, whenever the condition breaks (for example conflicting adjacent values or invalid pair structure), try all possible fixes such as modifying the current element or adjusting a neighbor. Each decision may require scanning forward to verify the array remains valid. Because each correction may trigger additional checks, the worst‑case time complexity becomes O(n^2). This approach helps understand the constraint interactions but does not scale for large arrays.
Approach 2: Dynamic Programming on Prefix State (O(n) time, O(n) space)
A better approach models the problem using dynamic programming. Define a DP state representing the minimum operations required to make the prefix nums[0..i] valid while respecting the beauty constraint. At each index you decide whether to keep the current element or perform an operation to modify it so it satisfies the required relation with the previous element. The transition only depends on the previous state, which keeps the solution linear. This method systematically evaluates both choices and stores the minimal cost for each prefix.
Approach 3: Optimized Greedy / Rolling DP (O(n) time, O(1) space)
The DP observation reveals that only the previous state is required. Replace the DP array with a few rolling variables and process the array in a single pass. When a violation of the beauty rule appears, increment the operation count and update the state so future comparisons remain valid. This technique is common in array processing problems where local constraints determine global validity. The result is an optimal O(n) time solution with constant memory.
Recommended for interviews: Start by describing the brute‑force reasoning so the interviewer sees how you interpret the beauty constraint. Then move to the prefix dynamic programming formulation and finally compress it into the rolling state greedy solution. Interviewers usually expect the linear O(n) approach because it demonstrates pattern recognition and state optimization.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Simulation | O(n^2) | O(1) | Useful for understanding how violations occur and testing small inputs |
| Dynamic Programming (Prefix States) | O(n) | O(n) | General solution that explicitly tracks optimal operations for each prefix |
| Optimized Rolling DP / Greedy | O(n) | O(1) | Preferred solution in interviews and production for linear performance |
Practice Minimum Operations to Make the Array Beautiful with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor