LeetCode 2863: Maximum Length of Semi-Decreasing Subarrays Solution

Master LeetCode problem 2863 (Maximum Length of Semi-Decreasing Subarrays), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2863. Maximum Length of Semi-Decreasing Subarrays

Problem Explanation

Explanation:

Given an array of integers, we need to find the maximum length of a contiguous subarray that is semi-decreasing. A subarray is considered semi-decreasing if at most one element needs to be removed in order to make the entire subarray in non-increasing order.

To solve this problem, we can use a sliding window approach. We will iterate over the array and keep track of the current subarray that is semi-decreasing. We will maintain a variable to store the length of the longest semi-decreasing subarray found so far.

Here is the algorithm:

  1. Initialize variables start and maxLength to 0.
  2. Iterate over the array using a sliding window:
    • If the current element is greater than or equal to the previous element, we can extend the current semi-decreasing subarray.
    • If the current element is less than the previous element, we need to consider removing the previous element to maintain semi-decreasing order. In this case, update start to the index of the previous element.
    • Update maxLength to be the maximum of maxLength and the length of the current subarray.
  3. Return maxLength.

Time Complexity: O(n) where n is the number of elements in the array. Space Complexity: O(1)

:

Solution Code

class Solution {
    public int maxLengthSemiDecreasing(int[] nums) {
        int start = 0, maxLength = 0;
        
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] >= nums[i - 1]) {
                continue;
            }
            
            maxLength = Math.max(maxLength, i - start);
            start = i;
        }
        
        maxLength = Math.max(maxLength, nums.length - start);
        
        return maxLength;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2863 (Maximum Length of Semi-Decreasing Subarrays)?

This page provides optimized solutions for LeetCode problem 2863 (Maximum Length of Semi-Decreasing Subarrays) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2863 (Maximum Length of Semi-Decreasing Subarrays)?

The time complexity for LeetCode 2863 (Maximum Length of Semi-Decreasing Subarrays) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2863 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2863 in Java, C++, or Python.

Back to LeetCode Solutions