Adjacent Increasing Subarrays Detection II - Problem

Given an array nums of n integers, your task is to find the maximum value of k for which there exist two adjacent subarrays of length k each, such that both subarrays are strictly increasing.

Specifically, check if there are two subarrays of length k starting at indices a and b (a < b), where:

  • Both subarrays nums[a..a + k - 1] and nums[b..b + k - 1] are strictly increasing.
  • The subarrays must be adjacent, meaning b = a + k.

Return the maximum possible value of k. A subarray is a contiguous non-empty sequence of elements within an array.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,5,7,8,9,2,3,4,3,1]
Output: 3
💡 Note: We can find two adjacent subarrays [2,3,4] and [3] where both are strictly increasing, but the maximum length is 3 with subarrays [5,7,8] and [8,9] (not adjacent). Actually, [2,3,4] at index 5-7 is increasing, but we need adjacent pairs. The answer is 1 as we can find adjacent increasing subarrays of length 1.
Example 2 — No Adjacent Pairs
$ Input: nums = [1,2,3,2,1]
Output: 0
💡 Note: No two adjacent subarrays can both be strictly increasing since the array has decreasing parts that prevent valid adjacent pairs.
Example 3 — Small Array
$ Input: nums = [1,2]
Output: 0
💡 Note: Array is too small to have two adjacent subarrays of any positive length.

Constraints

  • 2 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109

Visualization

Tap to expand
Adjacent Increasing Subarrays Detection II251324012345Find adjacent increasing subarrays: [1] and [3] (length k=1)Adjacent pair found at positions 2,3Maximum k = 1Both subarrays [1] and [3] are strictly increasing
Understanding the Visualization
1
Input Array
Given array with potential increasing subsequences
2
Find Adjacent Pairs
Look for adjacent subarrays that are both strictly increasing
3
Return Maximum k
Find the largest possible length k
Key Takeaway
🎯 Key Insight: Precompute increasing run lengths to efficiently find the maximum k
Asked in
Google 15 Microsoft 12 Amazon 8
8.5K Views
Medium Frequency
~25 min Avg. Time
234 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