572. Maximum Length of Semi Decreasing Subarrays

You are given an array nums of n integers.

Return the length of the longest semi-decreasing subarray of nums, and 0 if there are no such subarrays.

  • A subarray is a contiguous non-empty sequence of elements within an array.
  • A non-empty array is semi-decreasing if its first element is strictly greater than its last element.

Example 1:

Input : nums = [10, 5, 9, 2, 5, 15]

Output : 5

Explanation :

Take the subarray [10, 5, 9, 2, 5].

The first element is 10 and the last one is 5 so the condition is met.

Hence, the answer would be the length of the subarray or 5.

It can be shown that there aren't any subarrays with the given condition with a length greater than 5.

Example 2:

Input : nums = [1, 2, 3, 4, 5]

Output : 0

Explanation :

Since there are no semi-decreasing subarrays in the given array, the answer is 0.

Now Your Turn!

Pick the correct output for the given input

Input : nums = [7, 8, 9 10, 11, 2, 3, 4, 5]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 105
  • -109 <= nums[i] <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int maxSubarrayLength(vector<int>& nums) {
//your code goes here
}
};
Test Case

Input:

Nums