Watch 3 video solutions for Minimum Operations to Transform Array, a medium level problem involving Array, Greedy. This walkthrough by Sanyam IIT Guwahati has 901 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given two integer arrays nums1 of length n and nums2 of length n + 1.
You want to transform nums1 into nums2 using the minimum number of operations.
You may perform the following operations any number of times, each time choosing an index i:
nums1[i] by 1.nums1[i] by 1.nums1[i] to the end of the array.Return the minimum number of operations required to transform nums1 into nums2.
Example 1:
Input: nums1 = [2,8], nums2 = [1,7,3]
Output: 4
Explanation:
| Step | i |
Operation | nums1[i] |
Updated nums1 |
|---|---|---|---|---|
| 1 | 0 | Append | - | [2, 8, 2] |
| 2 | 0 | Decrement | Decreases to 1 | [1, 8, 2] |
| 3 | 1 | Decrement | Decreases to 7 | [1, 7, 2] |
| 4 | 2 | Increment | Increases to 3 | [1, 7, 3] |
Thus, after 4 operations nums1 is transformed into nums2.
Example 2:
Input: nums1 = [1,3,6], nums2 = [2,4,5,3]
Output: 4
Explanation:
| Step | i |
Operation | nums1[i] |
Updated nums1 |
|---|---|---|---|---|
| 1 | 1 | Append | - | [1, 3, 6, 3] |
| 2 | 0 | Increment | Increases to 2 | [2, 3, 6, 3] |
| 3 | 1 | Increment | Increases to 4 | [2, 4, 6, 3] |
| 4 | 2 | Decrement | Decreases to 5 | [2, 4, 5, 3] |
Thus, after 4 operations nums1 is transformed into nums2.
Example 3:
Input: nums1 = [2], nums2 = [3,4]
Output: 3
Explanation:
| Step | i |
Operation | nums1[i] |
Updated nums1 |
|---|---|---|---|---|
| 1 | 0 | Increment | Increases to 3 | [3] |
| 2 | 0 | Append | - | [3, 3] |
| 3 | 1 | Increment | Increases to 4 | [3, 4] |
Thus, after 3 operations nums1 is transformed into nums2.
Constraints:
1 <= n == nums1.length <= 105nums2.length == n + 11 <= nums1[i], nums2[i] <= 105Problem Overview: You are given an array and need to compute the minimum number of operations required to transform it so that it satisfies the required ordering constraint. Each operation adjusts array elements, and the goal is to achieve the valid configuration with the fewest changes.
Approach 1: Brute Force Adjustment (O(n^2) time, O(1) space)
The naive strategy repeatedly scans the array and fixes violations one step at a time. Whenever the current element breaks the required ordering relative to the previous element, you increment or modify it until the constraint holds. Because each correction may require multiple passes over the array, the algorithm can degrade to O(n^2) time in the worst case. This method is mostly useful for understanding the mechanics of the transformation but is too slow for large inputs.
Approach 2: Greedy Single Pass (O(n) time, O(1) space)
The optimal solution uses a greedy observation: when scanning from left to right, the only element that matters for the current position is the previous value after transformation. If nums[i] already satisfies the required relation with the previous element, no operation is needed. If it violates the constraint, the best choice is to minimally adjust the current element so the relation becomes valid. Each adjustment contributes directly to the total operation count.
This works because any extra adjustment beyond the minimum would only increase the number of operations and potentially make later elements harder to fix. Maintaining the smallest valid value at each step keeps the array as flexible as possible for future elements. The algorithm simply iterates once through the array, tracks the previous valid value, and accumulates the required operations.
This greedy reasoning ensures correctness: every local fix is globally optimal because the constraint only depends on adjacent values. The approach runs in linear time and uses constant extra space, making it ideal for large inputs.
Problems like this frequently appear in interviews to test your understanding of array traversal patterns and when a greedy decision is safe. Recognizing that only the previous transformed value matters allows you to collapse what looks like a complex sequence of operations into a single pass.
Recommended for interviews: Start by briefly describing the brute force idea to demonstrate understanding of the transformation process. Then move to the greedy single-pass solution. Interviewers expect the O(n) greedy approach because it shows you can identify the minimal adjustment needed at each step and avoid unnecessary recomputation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Adjustment | O(n^2) | O(1) | Useful for understanding the transformation process or very small arrays |
| Greedy Single Pass | O(n) | O(1) | Best for interviews and production; handles large inputs efficiently |