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[1] = 5 with arr2[1] = 3, then arr1 = [1,3,3,6,7]. Replace arr1[2] = 3 with arr2[3] = 4, then arr1 = [1,3,4,6,7] which is strictly increasing. Total operations: 1.
Example 2 — No Operations Needed
$ Input: arr1 = [1,2,3,4], arr2 = [1,3,5,7]
Output: 0
💡 Note: arr1 is already strictly increasing [1,2,3,4], so no operations are needed.
Example 3 — Impossible Case
$ Input: arr1 = [1,5,3,6,7], arr2 = [4,3,1]
Output: -1
💡 Note: There is no way to make arr1 strictly increasing. We cannot find suitable replacements to fix the decreasing subsequence.

Constraints

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

Visualization

Tap to expand
INPUTALGORITHMRESULTarr1 = [1,5,3,6,7]15367Red = Problem elementsarr2 = [1,3,2,4]1324Replacement optionsGoal: Make arr1 strictlyincreasing with minimumreplacement operations1Initialize DP states2For each position, try:• Keep original element• Replace with arr2 element3Use binary search forefficient transitions4Track minimum operationsfor each valid stateTime: O(n×m×log m)Space: O(n×m)Final Array:13467Green=Kept, Yellow=ReplacedMin Operations: 1Replace 5→3 or 3→4Both give valid solutionStrictly increasing:1 < 3 < 4 < 6 < 7 ✓Key Insight:Dynamic programming with memoization tracks the minimum operations neededto reach each position with different ending values. Binary search optimizationmakes finding valid transitions efficient for large value ranges.TutorialsPoint - Make Array Strictly Increasing | Dynamic Programming
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
28.4K 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