Advertisement

Shortest Subarray to be Removed to Make Array Sorted - LeetCode 1574 Solution

Shortest Subarray to be Removed to Make Array Sorted - Complete Solution Guide

Shortest Subarray to be Removed to Make Array Sorted is LeetCode problem 1574, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array arr , remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing . Return the length of the shortest subarray to remove . A subarray is a contiguous subsequence of the array. Example 1: Input: arr = [1,2,3,10,4,2,3,5] Output: 3 Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted. Another correct solution is to remove the subarray [3,10,4].

Detailed Explanation

The problem asks us to find the length of the shortest subarray that can be removed from a given integer array `arr` such that the remaining elements are in non-decreasing order (sorted in ascending order or with equal values). The removed subarray can be empty, meaning no elements need to be removed if the array is already sorted. The goal is to minimize the length of the removed subarray.

Solution Approach

The solution uses a two-pointer approach to find the longest non-decreasing prefix and suffix of the array. Then, it iterates through the prefix and suffix to find the shortest subarray to remove to make the remaining array non-decreasing. It first calculates the length of the shortest subarray to remove by removing either the entire prefix or the entire suffix. Then, it iterates through the prefix and suffix, comparing elements to find the shortest subarray to remove such that the remaining elements form a non-decreasing sequence.

Step-by-Step Algorithm

  1. Step 1: Find the longest non-decreasing prefix of the array using a `left` pointer. Increment `left` as long as `arr[left] <= arr[left + 1]`.
  2. Step 2: Find the longest non-decreasing suffix of the array using a `right` pointer. Decrement `right` as long as `arr[right - 1] <= arr[right]`.
  3. Step 3: Calculate the initial minimum length of the subarray to remove as the minimum of `n - 1 - left` (removing the prefix) and `right` (removing the suffix).
  4. Step 4: Use two pointers `i` and `j`, initialized to 0 and `right` respectively, to iterate through the prefix and suffix. Compare `arr[i]` and `arr[j]`. If `arr[i] <= arr[j]`, it means we can combine the prefix up to index `i` and the suffix starting from index `j` to form a non-decreasing sequence. Update the minimum length of the subarray to remove as `min(ans, j - i - 1)`. Increment `i`. Otherwise, increment `j`.
  5. Step 5: Return the minimum length of the subarray to remove.

Key Insights

  • Insight 1: The remaining array after removing the subarray can be thought of as two non-decreasing sequences joined together: a prefix and a suffix of the original array.
  • Insight 2: We can use two pointers, one at the beginning and one at the end of the array, to find the longest non-decreasing prefix and suffix respectively.
  • Insight 3: Once we have the longest prefix and suffix, we can iterate through them to find the optimal removal length by considering the cases where elements from the prefix and suffix combine to form a larger non-decreasing sequence.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Two Pointers, Binary Search, Stack, Monotonic Stack.

Companies

Asked at: DE Shaw, razorpay.