Link of the question:https://leetcode.com/problems/adjacent-increasing-subarrays-detection-i/
Explanation:
Edge case check: If the array has fewer than
2 * kelements, it's impossible to find two adjacent subarrays of lengthk, so we returnfalseimmediately.Sliding window logic:
- We slide through the array with a starting index
isuch thatigoes from0ton - 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 + kand 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