Skip to main content

Minimum Operations to Make the Array Beautiful - Solution & Explanation

MediumPremiumFree on FleetCodeArrayDynamic Programming5 min read
Practice this problem

Problem Statement

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 <= 100
  • 1 <= nums[i] <= 50​​​

Approach Overview

Problem 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.

Solution

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force SimulationO(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 / GreedyO(n)O(1)Preferred solution in interviews and production for linear performance

Frequently Asked Questions

Is Minimum Operations to Make the Array Beautiful easy or hard?
Minimum Operations to Make the Array Beautiful is usually categorized as a Medium difficulty problem. The main challenge is recognizing the correct state transition or greedy rule that allows you to convert a naive simulation into a linear dynamic programming solution.
Minimum Operations to Make the Array Beautiful Python/Java solution
The solution is typically implemented with a single loop that tracks violations and updates an operation counter. FleetCode provides implementations in Python, Java, C++, Go, TypeScript, and Rust using the same O(n) dynamic programming logic.
How to solve Minimum Operations to Make the Array Beautiful in O(n)?
Iterate through the array while maintaining a state that represents whether the current prefix is already valid. When the current element breaks the beauty condition with the previous element, increment the operation counter and logically adjust the state to reflect the performed modification. This avoids revisiting earlier elements and guarantees O(n) processing time.
What is the best approach for Minimum Operations to Make the Array Beautiful?
The most efficient approach uses a linear dynamic programming or greedy state transition. You process the array once and track whether the current element violates the beauty constraint relative to the previous element. When a violation occurs, perform one operation and update the state so the rest of the array remains valid. This produces an O(n) time and O(1) space solution.
Is Minimum Operations to Make the Array Beautiful asked at Google/Amazon/Meta?
Problems involving array transformations with minimal operations and dynamic programming patterns frequently appear in interviews at companies like Google, Amazon, and Meta. Variants that require enforcing adjacency rules or pair constraints are especially common in coding rounds.
What data structure is used in Minimum Operations to Make the Array Beautiful?
The core structure is a simple array traversal combined with dynamic programming states. Instead of complex data structures, the algorithm typically maintains a few variables that track the previous element’s condition and the current operation count.
What is the time complexity of Minimum Operations to Make the Array Beautiful?
The optimal solution runs in O(n) time because the array is scanned once while maintaining a small state describing the previous element’s validity. Space complexity can be reduced to O(1) by storing only the last state instead of a full DP array.

Ready to solve this problem?

Practice Minimum Operations to Make the Array Beautiful with our built-in code editor and test cases.

Practice on FleetCode