Make Array Strictly Increasing - Problem
Transform Array into Strictly Increasing Sequence

You're given two integer arrays: arr1 (the array to modify) and arr2 (your toolkit of replacement values). Your goal is to make arr1 strictly increasing using the minimum number of operations.

In each operation, you can:
• Choose any index i in arr1
• Choose any index j in arr2
• Replace arr1[i] with arr2[j]

Challenge: Find the minimum operations needed, or return -1 if it's impossible to create a strictly increasing sequence.

Example: If arr1 = [1,5,3,6,7] and arr2 = [1,3,2,4], you need 1 operation (replace 5 with 2) to get [1,2,3,6,7].

Input & Output

example_1.py — Basic Replacement
$ Input: arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
Output: 1
💡 Note: Replace arr1[1] = 5 with arr2[2] = 2. The array becomes [1,2,3,6,7] which is strictly increasing. Only 1 operation needed.
example_2.py — Multiple Replacements
$ Input: arr1 = [1,5,3,6,7], arr2 = [4,3,1]
Output: 2
💡 Note: Replace arr1[1] = 5 with arr2[1] = 3 and arr1[2] = 3 with arr2[0] = 4. The array becomes [1,3,4,6,7]. Total 2 operations.
example_3.py — Impossible Case
$ Input: arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
Output: -1
💡 Note: Cannot make arr1 strictly increasing. After position with value 6, we need values > 6, but arr2 only has values ≤ 6.

Constraints

  • 1 ≤ arr1.length, arr2.length ≤ 2000
  • 0 ≤ arr1[i], arr2[i] ≤ 109
  • Array elements can be equal, but result must be strictly increasing

Visualization

Tap to expand
🏗️ Building the Perfect StaircaseOriginal Staircase (arr1)15367❌ Not strictly increasing!Replacement Toolkit (arr2 sorted)1234After Optimal Replacement12367✅ Strictly increasing! 1 operationReplace 5→2DP + Binary Search Process1. DP State: track (ending_value → min_ops)2. For each position: keep current OR replace3. Binary search finds optimal replacement4. Update states with minimum operations⚡ Time: O(n × m × log m), Space: O(n × m)🎯 Key Insight:Dynamic Programming tracks optimal states while binary searchefficiently finds the best replacement values, avoiding redundantcalculations and achieving optimal time complexity.
Understanding the Visualization
1
Assess Current Staircase
Look at arr1 = [1,5,3,6,7]. Steps 5→3 violate strictly increasing rule.
2
Check Toolkit
Available replacement steps: arr2 = [1,3,2,4]. Sort for efficient selection.
3
DP State Management
Track minimum operations to reach each position with different ending heights.
4
Binary Search Optimization
For each position, quickly find the smallest valid replacement step.
5
Optimal Path
Replace step 5 with step 2, creating [1,2,3,6,7] with just 1 operation.
Key Takeaway
🎯 Key Insight: The combination of DP state management and binary search optimization transforms an exponential brute force solution into an efficient O(n × m × log m) algorithm by tracking minimal operation counts for each possible ending value.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
29.4K Views
Medium-High Frequency
~25 min Avg. Time
1.2K 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