Adjacent Increasing Subarrays Detection II - Problem

Imagine you're analyzing data patterns in a sequence of measurements. You need to find the maximum length of two consecutive patterns where both show a strictly increasing trend.

Given an array nums of n integers, find the maximum value of k such that there exist two adjacent subarrays of length k each, where both subarrays are strictly increasing.

Requirements:

  • Find two subarrays of length k starting at indices a and b where a < b
  • Both nums[a..a + k - 1] and nums[b..b + k - 1] must be strictly increasing
  • The subarrays must be adjacent: b = a + k
  • Return the maximum possible value of k

Example: For [2, 5, 7, 8, 9, 2, 3, 4, 3, 1], the answer is 3 because we can have subarrays [2, 5, 7] and [8, 9, 2]... wait, that's not right! We need [5, 7, 8] and [8, 9, 2] but [8, 9, 2] isn't increasing. Let's find valid adjacent increasing pairs!

Input & Output

example_1.py โ€” Basic Case
$ Input: [2, 5, 7, 8, 9, 2, 3, 4, 3, 1]
โ€บ Output: 2
๐Ÿ’ก Note: We can find subarrays [2, 5] and [7, 8] (both strictly increasing, length 2) or [7, 8] and [9, 2] but [9, 2] is decreasing. The maximum valid k is 2 with subarrays [2, 5] and [7, 8] at positions 0-1 and 2-3.
example_2.py โ€” Perfect Case
$ Input: [1, 2, 3, 4, 5, 6]
โ€บ Output: 3
๐Ÿ’ก Note: We can have [1, 2, 3] and [4, 5, 6], both strictly increasing subarrays of length 3.
example_3.py โ€” No Valid Pairs
$ Input: [5, 4, 3, 2, 1]
โ€บ Output: 0
๐Ÿ’ก Note: The array is strictly decreasing, so no increasing subarrays of length > 0 exist.

Visualization

Tap to expand
Route 1 (k=3)Route 2 (k=2)๐Ÿ”๏ธ Maximum k = 2 (both routes uphill)
Understanding the Visualization
1
Survey the Terrain
First, we map out all uphill sections and their lengths from each starting point
2
Binary Search Strategy
Instead of trying every route length, we use binary search - if routes of length k work, try longer; if not, try shorter
3
Quick Validation
Using our terrain map, we can quickly check if any position allows two consecutive uphill routes of length k
4
Find Maximum
Continue binary search until we find the maximum possible route length
Key Takeaway
๐ŸŽฏ Key Insight: Binary search on the answer combined with preprocessing allows us to find the maximum length efficiently in O(n log n) time rather than O(nยณ) brute force.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยณ)

O(n) for trying different k values ร— O(n) for trying positions ร— O(n) to check if subarray is increasing

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • 2 โ‰ค n โ‰ค 105
  • -109 โ‰ค nums[i] โ‰ค 109
  • Array contains at least 2 elements
  • Subarrays must be strictly increasing (no equal elements allowed)
Asked in
Google 23 Meta 18 Amazon 15 Microsoft 12
28.9K Views
Medium Frequency
~25 min Avg. Time
847 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