Link of the question:https://leetcode.com/problems/adjacent-increasing-subarrays-detection-i/
Explanation:
Edge case check: If the array has fewer than
2 * k
elements, it's impossible to find two adjacent subarrays of lengthk
, so we returnfalse
immediately.Sliding window logic:
- We slide through the array with a starting index
i
such thati
goes from0
ton - 2*k
(this ensures that we have enough elements for two adjacent subarrays). - For each window of length
k
, we check if the elements within the window are strictly increasing by checking if each element is smaller than the next one. - Once we check the first subarray, we check the second subarray starting at
i + k
and ensure it's also strictly increasing.
- We slide through the array with a starting index
Return result: If both subarrays are strictly increasing, return
true
. Otherwise, continue sliding through the array. If no such pair is found, returnfalse
.
Feel free to connect:
splendid!!
ReplyDeleteHope you liked it
Delete