Given an array nums of n integers and an integer k, determine whether there exist twoadjacent subarrays of length k such that both subarrays are strictlyincreasing. 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.
Input: nums =[2,5,7,8,9,2,3,4,3,1], k =3Output: trueExplanation:
* The subarray starting at index `2`is`[7, 8, 9]`, which is strictly increasing.* The subarray starting at index `5`is`[2, 3, 4]`, which is also strictly increasing.* These two subarrays are adjacent, so the result is`true`.
Example 2:
1
2
Input: nums =[1,2,3,4,4,4,4,5,6,7], k =5Output: false
We need to find two adjacent subarrays of length k that are both strictly increasing. By sliding a window of size 2k across the array, we can check if both halves of the window (each of length k) are strictly increasing. If so, return true.
classSolution {
public:bool areTwoAdjacentIncreasingSubarrays(vector<int>& nums, int k) {
int n = nums.size();
for (int i =0; i <= n -2* k; ++i) {
bool first = true, second = true;
for (int j = i; j < i + k -1; ++j) {
if (nums[j] >= nums[j +1]) {
first = false;
break;
}
}
if (!first) continue;
for (int j = i + k; j < i +2* k -1; ++j) {
if (nums[j] >= nums[j +1]) {
second = false;
break;
}
}
if (second) return true;
}
return false;
}
};
classSolution {
publicbooleanareTwoAdjacentIncreasingSubarrays(int[] nums, int k) {
int n = nums.length;
for (int i = 0; i <= n - 2 * k; i++) {
boolean first =true, second =true;
for (int j = i; j < i + k - 1; j++) {
if (nums[j]>= nums[j + 1]) {
first =false;
break;
}
}
if (!first) continue;
for (int j = i + k; j < i + 2 * k - 1; j++) {
if (nums[j]>= nums[j + 1]) {
second =false;
break;
}
}
if (second) returntrue;
}
returnfalse;
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defareTwoAdjacentIncreasingSubarrays(self, nums: list[int], k: int) -> bool:
n = len(nums)
for i in range(n -2* k +1):
first = all(nums[j] < nums[j +1] for j in range(i, i + k -1))
ifnot first:
continue second = all(nums[j] < nums[j +1] for j in range(i + k, i +2* k -1))
if second:
returnTruereturnFalse