LeetCode Challenge Day 29 β 3349. Adjacent Increasing Subarrays Detection I
Nitin Ahirwal / October 14, 2025
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
ksuch 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
ihas 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
-
Build an array
incwhereinc[i]= length of the increasing sequence ending at indexi.- If
nums[i] > nums[i-1], extend the run. - Otherwise reset to
1.
- If
-
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] β₯ kandinc[a+2k-1] β₯ k, returntrue.
- First subarray ends at
-
If no valid pair found, return
false.
β±οΈ Complexity Analysis
-
Time complexity:
We computeincin O(n) and scan for valid subarrays in another O(n).
Total = O(n). -
Space complexity:
Theincarray 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 π¨βπ»