Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Last active October 15, 2025 16:45
Show Gist options
  • Select an option

  • Save tatsuyax25/736f552be7d4d7f48ab2923720f203b6 to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/736f552be7d4d7f48ab2923720f203b6 to your computer and use it in GitHub Desktop.
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
/**
* @param {number[]} nums
* @return {number}
*/
var maxIncreasingSubarrays = function (nums) {
let maxK = 0; // Stores the maximum valid k found so far
let curLen = 1; // Length of the current strictly increasing run
let prevLen = 0; // Length of the previous strictly increasing run
for (let i = 1; i < nums.length; i++) {
if (nums[i] > nums[i - 1]) {
// Continue the current increasing run
curLen++;
} else {
// End of an increasing run — evaluate possible k values
// Case 1: Two adjacent increasing runs — take the smaller of the two
// Case 2: A single long run — it can be split into two adjacent k-length runs
maxK = Math.max(
maxK,
Math.floor(curLen / 2), // Split one long run into two adjacent k-length runs
Math.min(curLen, prevLen) // Two adjacent runs of curLen and prevLen
);
// Shift current run to previous, and reset current
prevLen = curLen;
curLen = 1;
}
}
// Final check after the loop ends (in case the array ends with an increasing run)
maxK = Math.max(
maxK,
Math.floor(curLen / 2),
Math.min(curLen, prevLen)
);
return maxK;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment