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 strictlyincreasing. 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 maximumpossible value of k.
A subarray is a contiguous non-empty sequence of elements within an array.
Input: nums =[2,5,7,8,9,2,3,4,3,1]Output: 3Explanation:
* The subarray starting at index 2is`[7, 8, 9]`, which is strictly increasing.* The subarray starting at index 5is`[2, 3, 4]`, which is also strictly increasing.* These two subarrays are adjacent, and 3is the **maximum** possible value of `k`for which two such adjacent strictly increasing subarrays exist.
Example 2:
1
2
3
4
5
6
Input: nums =[1,2,3,4,4,4,4,5,6,7]Output: 2Explanation:
* The subarray starting at index 0is`[1, 2]`, which is strictly increasing.* The subarray starting at index 2is`[3, 4]`, which is also strictly increasing.* These two subarrays are adjacent, and 2is the **maximum** possible value of `k`for which two such adjacent strictly increasing subarrays exist.
We want the largest k such that there exist two adjacent subarrays of length k that are both strictly increasing. Since checking for all possible k is inefficient, we can use binary search on k and efficiently check for each k using a sliding window.
Set lo = 1, hi = n // 2 (since two adjacent subarrays of length k need 2k <= n).
For each k, check if there exist two adjacent strictly increasing subarrays of length k.
Check Function:
For each possible starting index i (from 0 to n - 2*k + 1), check if both nums[i:i+k] and nums[i+k:i+2*k] are strictly increasing.
Use a sliding window to efficiently check if a subarray is strictly increasing by precomputing an array inc where inc[i] is the length of the strictly increasing sequence starting at i.
classSolution {
public:int maxIncreasingSubarrays(std::vector<int>& nums) {
int n = nums.size();
if (n <2) return0;
std::vector<int> inc(n, 1);
for (int i = n -2; i >=0; --i) {
inc[i] = (nums[i] < nums[i +1]) ? inc[i +1] +1:1;
}
int lo =1, hi = n /2, ans =0;
while (lo <= hi) {
int k = (lo + hi) /2;
bool found = false;
for (int i =0; i +2* k <= n; ++i) {
if (inc[i] >= k && inc[i + k] >= k) { found = true; break; }
}
if (found) { ans = k; lo = k +1; } else { hi = k -1; }
}
return ans;
}
};
classSolution {
publicintmaxIncreasingSubarrays(List<Integer> nums) {
int n = nums.size();
if (n < 2) return 0;
int[] inc =newint[n];
Arrays.fill(inc, 1);
for (int i = n - 2; i >= 0; --i) {
inc[i]= nums.get(i) < nums.get(i + 1) ? inc[i + 1]+ 1 : 1;
}
int lo = 1, hi = n / 2, ans = 0;
while (lo <= hi) {
int k = (lo + hi) / 2;
boolean found =false;
for (int i = 0; i + 2 * k <= n; ++i) {
if (inc[i]>= k && inc[i + k]>= k) { found =true; break; }
}
if (found) { ans = k; lo = k + 1; } else { hi = k - 1; }
}
return ans;
}
}
classSolution {
funmaxIncreasingSubarrays(nums: List<Int>): Int {
val n = nums.size
if (n < 2) return0val inc = IntArray(n) { 1 }
for (i in n - 2 downTo 0) {
inc[i] = if (nums[i] < nums[i + 1]) inc[i + 1] + 1else1 }
var lo = 1var hi = n / 2var ans = 0while (lo <= hi) {
val k = (lo + hi) / 2var found = falsefor (i in0..(n - 2 * k)) {
if (inc[i] >= k && inc[i + k] >= k) { found = true; break }
}
if (found) { ans = k; lo = k + 1 } else { hi = k - 1 }
}
return ans
}
}
classSolution:
defmaxIncreasingSubarrays(self, nums: list[int]) -> int:
n = len(nums)
if n <2:
return0 inc = [1] * n
for i in range(n -2, -1, -1):
if nums[i] < nums[i +1]:
inc[i] = inc[i +1] +1 ans =0 lo, hi =1, n //2while lo <= hi:
k = (lo + hi) //2 found =Falsefor i in range(0, n -2* k +1):
if inc[i] >= k and inc[i + k] >= k:
found =Truebreakif found:
ans = k
lo = k +1else:
hi = k -1return ans
pubstructSolution;
impl Solution {
pubfnmaxIncreasingSubarrays(&self, nums: Vec<i64>) -> i32 {
let n = nums.len();
if n <2 { return0; }
letmut inc =vec![1usize; n];
for i in (0..n-1).rev() {
inc[i] =if nums[i] < nums[i+1] { inc[i+1] +1 } else { 1 };
}
letmut lo =1usize;
letmut hi = n /2;
letmut ans =0i32;
while lo <= hi {
let k = (lo + hi) /2;
letmut found =false;
for i in0..=n -2* k {
if inc[i] >= k && inc[i + k] >= k { found =true; break; }
}
if found { ans = k asi32; lo = k +1; } else { if k==0 { break; } hi = k -1; }
}
ans
}
}