Back to posts

LeetCode Challenge Day 30 β€” 3350. Adjacent Increasing Subarrays Detection II

Nitin Ahirwal / October 15, 2025

LeetCode ChallengeDay 30ArraysDPJavaScriptMediumPortfolio

Hey folks

This is Day 30 of my LeetCode streak πŸš€.
Today’s problem is 3350. Adjacent Increasing Subarrays Detection II β€” a medium array + DP problem where we find the maximum length k such that two adjacent strictly increasing subarrays of length k exist.


πŸ“Œ Problem Statement

You are given an integer array nums.
We need to return the maximum possible value of k such that there exist two adjacent subarrays of length k where:

  • Both subarrays are strictly increasing.
  • They are adjacent, meaning if the first is nums[a..a+k-1], then the second is nums[a+k..a+2k-1].

πŸ’‘ Intuition

  • If we look at a boundary index m between two adjacent subarrays:

    • The left subarray must end at m.
    • The right subarray must start at m+1.
  • So the possible k at this boundary is constrained by:

    • L[m]: length of the increasing run ending at m.
    • R[m+1]: length of the increasing run starting at m+1.
  • The largest k at this boundary is min(L[m], R[m+1]).

  • The answer is the maximum of these values across all boundaries.


πŸ”‘ Approach

  1. Precompute L[i]: length of the strictly increasing run ending at index i.

    • If nums[i-1] < nums[i], then extend run β†’ L[i] = L[i-1] + 1.
    • Else reset to 1.
  2. Precompute R[i]: length of the strictly increasing run starting at index i.

    • If nums[i] < nums[i+1], then extend run β†’ R[i] = R[i+1] + 1.
    • Else reset to 1.
  3. For each boundary m between m and m+1:

    k = min(L[m], R[m+1])

    Track the maximum k.

  4. Return the maximum found.


⏱️ Complexity Analysis

  • Time complexity:
    Two linear scans to build L and R, and one more scan for boundaries.
    Total = O(n).

  • Space complexity:
    Extra arrays L and R of size n.
    Total = O(n) (can be optimized to O(1) with a streaming approach).


πŸ§‘β€πŸ’» Code (JavaScript)

/**
* @param {number[]} nums
* @return {number}
*/
var maxIncreasingSubarrays = function(nums) {
const n = nums.length;
if (n < 2) return 0;

// L[i] = length of strictly increasing run ending at i
const L = new Array(n).fill(1);
for (let i = 1; i < n; i++) {
 if (nums[i - 1] < nums[i]) L[i] = L[i - 1] + 1;
}

// R[i] = length of strictly increasing run starting at i
const R = new Array(n).fill(1);
for (let i = n - 2; i >= 0; i--) {
 if (nums[i] < nums[i + 1]) R[i] = R[i + 1] + 1;
}

// For each boundary m, max k is min(L[m], R[m+1])
let ans = 0;
for (let m = 0; m < n - 1; m++) {
 ans = Math.max(ans, Math.min(L[m], R[m + 1]));
}
return ans;
};

πŸ§ͺ Example Walkthrough

Input:
nums = [2,5,7,8,9,2,3,4,3,1]

  • Increasing runs:

    • L = [1,2,3,4,5,1,2,3,1,1]

    • R = [5,4,3,2,1,3,2,1,1,1]

  • Boundaries:

    • At m=4: min(L[4], R[5]) = min(5,3) = 3 βœ…

    • This is the maximum.

Output: 3.


πŸŽ₯ Reflections

This problem is a nice step up from Part I.
While Part I just asked if such subarrays exist, Part II pushes us to calculate the maximum possible length.
The trick was to precompute increasing run lengths from both sides and combine them at every boundary.

That’s it for Day 30 of my LeetCode journey!
Onwards to the next challenge πŸ”₯

Happy Coding πŸ‘¨β€πŸ’»