Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created October 14, 2025 20:25
Show Gist options
  • Select an option

  • Save tatsuyax25/28f97731c03a60077170a30f20346486 to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/28f97731c03a60077170a30f20346486 to your computer and use it in GitHub Desktop.
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),
/**
* @param {number[]} nums
* @param {number} k
* @return {boolean}
*/
var hasIncreasingSubarrays = function(nums, k) {
// Helper function to check if a subarray is strictly increasing
function isStrictlyIncreasing(start, end) {
for (let i = start; i < end; i++) {
if (nums[i] >= nums[i + 1]) {
return false; // Not strictly increasing
}
}
return true;
}
// Total length needed for two adjacent subarrays of length k
const totalLength = 2 * k;
// Loop through all possible starting indices for the first subarray
// We stop at nums.length - totalLength + 1 to avoid overflow
for (let i = 0; i <= nums.length - totalLength; i++) {
const firstStart = i;
const firstEnd = i + k - 1;
const secondStart = i + k;
const secondEnd = i + 2 * k - 1;
// Check both subarrays for strict increase
if (isStrictlyIncreasing(firstStart, firstEnd) &&
isStrictlyIncreasing(secondStart, secondEnd)) {
return true; // Found a valid pair
}
}
// No valid pair found
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment