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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an array nums of n integers and an integer k, determine whether there exist two adjacent subarrays of length k such that both subarrays are strictly increasing. Specifically, check if there are two subarrays 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 true if it is possible to find two such subarrays, and false otherwise.
Example 1:
Input: nums = [2,5,7,8,9,2,3,4,3,1], k = 3
Output: true
Explanation:
2 is [7, 8, 9], which is strictly increasing.5 is [2, 3, 4], which is also strictly increasing.true.Example 2:
Input: nums = [1,2,3,4,4,4,4,5,6,7], k = 5
Output: false
Constraints:
2 <= nums.length <= 1001 < 2 * k <= nums.length-1000 <= nums[i] <= 1000Problem summary: Given an array nums of n integers and an integer k, determine whether there exist two adjacent subarrays of length k such that both subarrays are strictly increasing. Specifically, check if there are two subarrays 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 true if it is possible to find two such subarrays, and false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[2,5,7,8,9,2,3,4,3,1] 3
[1,2,3,4,4,4,4,5,6,7] 5
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3349: Adjacent Increasing Subarrays Detection I
class Solution {
public boolean hasIncreasingSubarrays(List<Integer> nums, int k) {
int mx = 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)) {
mx = Math.max(mx, Math.max(cur / 2, Math.min(pre, cur)));
pre = cur;
cur = 0;
}
}
return mx >= k;
}
}
// Accepted solution for LeetCode #3349: Adjacent Increasing Subarrays Detection I
func hasIncreasingSubarrays(nums []int, k int) bool {
mx, pre, cur := 0, 0, 0
for i, x := range nums {
cur++
if i == len(nums)-1 || x >= nums[i+1] {
mx = max(mx, max(cur/2, min(pre, cur)))
pre, cur = cur, 0
}
}
return mx >= k
}
# Accepted solution for LeetCode #3349: Adjacent Increasing Subarrays Detection I
class Solution:
def hasIncreasingSubarrays(self, nums: List[int], k: int) -> bool:
mx = pre = cur = 0
for i, x in enumerate(nums):
cur += 1
if i == len(nums) - 1 or x >= nums[i + 1]:
mx = max(mx, cur // 2, min(pre, cur))
pre, cur = cur, 0
return mx >= k
// Accepted solution for LeetCode #3349: Adjacent Increasing Subarrays Detection I
impl Solution {
pub fn has_increasing_subarrays(nums: Vec<i32>, k: i32) -> bool {
let n = nums.len();
let (mut mx, mut pre, mut cur) = (0, 0, 0);
for i in 0..n {
cur += 1;
if i == n - 1 || nums[i] >= nums[i + 1] {
mx = mx.max(cur / 2).max(pre.min(cur));
pre = cur;
cur = 0;
}
}
mx >= k
}
}
// Accepted solution for LeetCode #3349: Adjacent Increasing Subarrays Detection I
function hasIncreasingSubarrays(nums: number[], k: number): boolean {
let [mx, 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]) {
mx = Math.max(mx, (cur / 2) | 0, Math.min(pre, cur));
[pre, cur] = [cur, 0];
}
}
return mx >= k;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.