Back to posts

LeetCode Challenge Day 29 β€” 3349. Adjacent Increasing Subarrays Detection I

Nitin Ahirwal / October 14, 2025

LeetCode ChallengeDay 29ArraysDPJavaScriptEasyPortfolio

Hey folks

This is Day 29 of my LeetCode streak πŸš€.
Today’s problem is 3349. Adjacent Increasing Subarrays Detection I β€” an easy array + DP problem where we check if two adjacent subarrays of length k are strictly increasing.


πŸ“Œ Problem Statement

You are given an array nums and an integer k.

  • We need to check if there exist two adjacent subarrays of length k such that:
    • Both are strictly increasing.
    • They are back-to-back (second starts immediately after the first).

Return true if such subarrays exist, otherwise false.


πŸ’‘ Intuition

  • A subarray is strictly increasing if every element is greater than the previous.
  • Instead of checking each subarray separately, we can track the length of increasing runs.
  • If the run ending at position i has length β‰₯ k, then [i-k+1 .. i] is strictly increasing.
  • So we just need to check two consecutive windows [a..a+k-1] and [a+k..a+2k-1].

πŸ”‘ Approach

  1. Build an array inc where inc[i] = length of the increasing sequence ending at index i.

    • If nums[i] > nums[i-1], extend the run.
    • Otherwise reset to 1.
  2. For each starting index a:

    • First subarray ends at a+k-1.
    • Second subarray ends at a+2k-1.
    • If both inc[a+k-1] β‰₯ k and inc[a+2k-1] β‰₯ k, return true.
  3. If no valid pair found, return false.


⏱️ Complexity Analysis

  • Time complexity:
    We compute inc in O(n) and scan for valid subarrays in another O(n).
    Total = O(n).

  • Space complexity:
    The inc array uses O(n) extra space.


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

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {boolean}
 */
var hasIncreasingSubarrays = function(nums, k) {
  const n = nums.length;
  // inc[i] = length of the strictly increasing run ending at i
  const inc = new Array(n).fill(1);
  for (let i = 1; i < n; i++) {
    if (nums[i] > nums[i - 1]) inc[i] = inc[i - 1] + 1;
  }

  // Check adjacent windows [a .. a+k-1] and [a+k .. a+2k-1]
  for (let a = 0; a + 2 * k - 1 < n; a++) {
    const end1 = a + k - 1;
    const end2 = a + 2 * k - 1;
    if (inc[end1] >= k && inc[end2] >= k) return true;
  }
  return false;
};

πŸ§ͺ Edge Cases

  • Array too short β†’ impossible, return false.

  • Strictly increasing whole array β†’ always has valid adjacent subarrays if 2k ≀ n.

  • Flat or decreasing regions β†’ breaks runs, result false.


πŸŽ₯ Reflections

This problem was a neat reminder that precomputing run lengths is a powerful technique for sequence checks.
Instead of re-checking each subarray from scratch, storing incremental info makes the solution both clean and efficient.

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

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