Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
nums[a..a + k - 1] and nums[b..b + k - 1] are strictly increasing.b = a + k.Return the maximum possible value of k.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [2,5,7,8,9,2,3,4,3,1]
Output: 3
Explanation:
[7, 8, 9], which is strictly increasing.[2, 3, 4], which is also strictly increasing.k for which two such adjacent strictly increasing subarrays exist.Example 2:
Input: nums = [1,2,3,4,4,4,4,5,6,7]
Output: 2
Explanation:
[1, 2], which is strictly increasing.[3, 4], which is also strictly increasing.k for which two such adjacent strictly increasing subarrays exist.Constraints:
2 <= nums.length <= 2 * 105-109 <= nums[i] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
[2,5,7,8,9,2,3,4,3,1]
[1,2,3,4,4,4,4,5,6,7]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3350: Adjacent Increasing Subarrays Detection II
class Solution {
public int maxIncreasingSubarrays(List<Integer> nums) {
int ans = 0, pre = 0, cur = 0;
int n = nums.size();
for (int i = 0; i < n; ++i) {
++cur;
if (i == n - 1 || nums.get(i) >= nums.get(i + 1)) {
ans = Math.max(ans, Math.max(cur / 2, Math.min(pre, cur)));
pre = cur;
cur = 0;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3350: Adjacent Increasing Subarrays Detection II
func maxIncreasingSubarrays(nums []int) (ans int) {
pre, cur := 0, 0
for i, x := range nums {
cur++
if i == len(nums)-1 || x >= nums[i+1] {
ans = max(ans, max(cur/2, min(pre, cur)))
pre, cur = cur, 0
}
}
return
}
# Accepted solution for LeetCode #3350: Adjacent Increasing Subarrays Detection II
class Solution:
def maxIncreasingSubarrays(self, nums: List[int]) -> int:
ans = pre = cur = 0
for i, x in enumerate(nums):
cur += 1
if i == len(nums) - 1 or x >= nums[i + 1]:
ans = max(ans, cur // 2, min(pre, cur))
pre, cur = cur, 0
return ans
// Accepted solution for LeetCode #3350: Adjacent Increasing Subarrays Detection II
impl Solution {
pub fn max_increasing_subarrays(nums: Vec<i32>) -> i32 {
let n = nums.len();
let (mut ans, mut pre, mut cur) = (0, 0, 0);
for i in 0..n {
cur += 1;
if i == n - 1 || nums[i] >= nums[i + 1] {
ans = ans.max(cur / 2).max(pre.min(cur));
pre = cur;
cur = 0;
}
}
ans
}
}
// Accepted solution for LeetCode #3350: Adjacent Increasing Subarrays Detection II
function maxIncreasingSubarrays(nums: number[]): number {
let [ans, pre, cur] = [0, 0, 0];
const n = nums.length;
for (let i = 0; i < n; ++i) {
++cur;
if (i === n - 1 || nums[i] >= nums[i + 1]) {
ans = Math.max(ans, (cur / 2) | 0, Math.min(pre, cur));
[pre, cur] = [cur, 0];
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.