filmov
tv
Find the length of the shortest unsorted subarray in an array of Integers || Shortest Subarray
![preview_player](https://i.ytimg.com/vi/uzMpMmuGqaE/sddefault.jpg)
Показать описание
Find the length of the shortest unsorted subarray in an array of integers, such that sorting the subarray would make the whole array sorted in ascending order.
In the given code, the find Unsorted Subarray method takes an array of integers as input, and returns the length of the shortest unsorted subarray.
The main method creates an instance of the Solution class and calls the findUnsortedSubarray method with an example input array. The result is then printed to the console.
The findUnsortedSubarray method iterates over the input array nums twice.
In the first iteration, it maintains a variable max that keeps track of the maximum element encountered so far, and an index end that is initialized
to -2. It checks if the current element is less than the maximum element encountered so far. If it is, it sets the end index to the current index,
indicating that the current element is out of order. At the end of the iteration, end is the index of the last element in the shortest unsorted subarray.
In the second iteration, it maintains a variable min that keeps track of the minimum element encountered so far, and an index begin that is initialized
to -1. It checks if the current element is greater than the minimum element encountered so far. If it is, it sets the begin index to the current index,
indicating that the current element is out of order. At the end of the iteration, begin is the index of the first element in the shortest unsorted subarray.
Finally, the method returns the length of the shortest unsorted subarray by subtracting begin from end and adding 1. If begin is still -1, it means that
the array is already sorted, so the method returns 0.
Viral Tags:
shortest unsorted continuous subarray,shortest unsorted continuous subarray leetcode,leetcode shortest unsorted continuous subarray,581. shortest unsorted continuous subarray,leetcode shortest unsorted continuous subarray solution,shortest unsorted continuous subarray c++,shortest unsorted continuous subarray leetcode solution,581 shortest unsorted continuous subarray,shortes unsorted continuous subarray,shortest unsorted continuous subarray solution
------------------------------------------------------------------------------------------------------
Join this channel to get access to Premium Interview Problems:
👇👇Join Group (Amazon Interview Help) 👇👇
===================
===================
👇👇Business Query👇👇
👇👇Click on the links to JOIN Group👇👇
👆👆Click on link above to JOIN Group👆👆
--------------------------------------------------------------------
👇👇Click on the links to JOIN Group👇👇
👆👆Click on link above to JOIN Group👆👆
--------------------------------------------
LIKE || SHARE || SUBSCRIBE
#beingactual
@Being Actual
In the given code, the find Unsorted Subarray method takes an array of integers as input, and returns the length of the shortest unsorted subarray.
The main method creates an instance of the Solution class and calls the findUnsortedSubarray method with an example input array. The result is then printed to the console.
The findUnsortedSubarray method iterates over the input array nums twice.
In the first iteration, it maintains a variable max that keeps track of the maximum element encountered so far, and an index end that is initialized
to -2. It checks if the current element is less than the maximum element encountered so far. If it is, it sets the end index to the current index,
indicating that the current element is out of order. At the end of the iteration, end is the index of the last element in the shortest unsorted subarray.
In the second iteration, it maintains a variable min that keeps track of the minimum element encountered so far, and an index begin that is initialized
to -1. It checks if the current element is greater than the minimum element encountered so far. If it is, it sets the begin index to the current index,
indicating that the current element is out of order. At the end of the iteration, begin is the index of the first element in the shortest unsorted subarray.
Finally, the method returns the length of the shortest unsorted subarray by subtracting begin from end and adding 1. If begin is still -1, it means that
the array is already sorted, so the method returns 0.
Viral Tags:
shortest unsorted continuous subarray,shortest unsorted continuous subarray leetcode,leetcode shortest unsorted continuous subarray,581. shortest unsorted continuous subarray,leetcode shortest unsorted continuous subarray solution,shortest unsorted continuous subarray c++,shortest unsorted continuous subarray leetcode solution,581 shortest unsorted continuous subarray,shortes unsorted continuous subarray,shortest unsorted continuous subarray solution
------------------------------------------------------------------------------------------------------
Join this channel to get access to Premium Interview Problems:
👇👇Join Group (Amazon Interview Help) 👇👇
===================
===================
👇👇Business Query👇👇
👇👇Click on the links to JOIN Group👇👇
👆👆Click on link above to JOIN Group👆👆
--------------------------------------------------------------------
👇👇Click on the links to JOIN Group👇👇
👆👆Click on link above to JOIN Group👆👆
--------------------------------------------
LIKE || SHARE || SUBSCRIBE
#beingactual
@Being Actual