Make Array Strictly Increasing - Problem

Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.

In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j].

If there is no way to make arr1 strictly increasing, return -1.

Input & Output

Example 1 — Basic Replacement
$ Input: arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
Output: 1
💡 Note: Replace arr1[2] = 3 with arr2[2] = 2. Result: [1,5,2,6,7] is not strictly increasing. Actually replace arr1[1] = 5 with arr2[2] = 2. Result: [1,2,3,6,7] which is strictly increasing with 1 operation.
Example 2 — Multiple Replacements
$ Input: arr1 = [1,5,3,6,7], arr2 = [4,3,1]
Output: 2
💡 Note: Need to replace arr1[1] = 5 and arr1[2] = 3 to make array strictly increasing. Minimum 2 operations required.
Example 3 — Impossible Case
$ Input: arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
Output: -1
💡 Note: No way to make the array strictly increasing with available replacement values.

Constraints

  • 1 ≤ arr1.length, arr2.length ≤ 2000
  • 0 ≤ arr1[i], arr2[i] ≤ 109

Visualization

Tap to expand
Make Array Strictly Increasing1536701234arr2: [1,3,2,4]Problem: 5 > 312367Result: 1 operation (replace 5 with 2)
Understanding the Visualization
1
Input
Two arrays: arr1 to modify, arr2 for replacement values
2
Process
For each position, choose to keep or replace with optimal value
3
Output
Minimum operations needed or -1 if impossible
Key Takeaway
🎯 Key Insight: Use dynamic programming to explore all valid choices while binary search optimizes finding the best replacement value
Asked in
Google 45 Amazon 32 Microsoft 28 Facebook 25
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen