Minimum Operations to Make the Array Beautiful

You are given an integer array nums.

An array is considered beautiful if for every index i such that i > 0, the value nums[i] is divisible by nums[i - 1].

In one operation, you can increment any element nums[i] (where i > 0) by 1.

Return the minimum number of operations required to make the array beautiful.

Example 1:

Input: nums = [2, 5, 7]

Output: 4

Explanation:

  • Increment nums[1] thrice→ [2, 8, 7]
  • Increment nums[2] once → [2, 8, 8]

Now:

  • 8 is divisible by 2
  • 8 is divisible by 8
  • So, the array becomes beautiful using 4 operations.

Example 2:

Input: nums = [5, 3, 10]

Output: 2

Explanation:

  • Increment nums[1] twice → [5, 5, 10]

Now:

  • 5 is divisible by 5
  • 10 is divisible by 5
  • Minimum operations required = 2.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [3, 4, 8]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 50
  • You may increment only elements with index i > 0
  • Each operation increases a single element by exactly 1

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int minOperations(vector<int>& nums) {
// Your code goes here
}
};
Test Case

Input:

Nums