Python3 Program for Shortest Un-ordered Subarray Last Updated : 05 Sep, 2024 Comments Improve Suggest changes Like Article Like Report An array is given of n length, and problem is that we have to find the length of shortest unordered {neither increasing nor decreasing} sub array in given array.Examples: Input : n = 5 7 9 10 8 11Output : 3Explanation : 9 10 8 unordered sub array.Input : n = 5 1 2 3 4 5Output : 0 Explanation : Array is in increasing order. The idea is based on the fact that size of shortest subarray would be either 0 or 3. We have to check array element is either increasing or decreasing, if all array elements are in increasing or decreasing, then length of shortest sub array is 0, And if either the array element is not follow the increasing or decreasing then it shortest length is 3. Python3 # Python3 program to find shortest # subarray which is unsorted # Bool function for checking an array # elements are in increasing def increasing(a, n): for i in range(0, n - 1): if (a[i] >= a[i + 1]): return False return True # Bool function for checking an array # elements are in decreasing def decreasing(a, n): for i in range(0, n - 1): if (a[i] < a[i + 1]): return False return True def shortestUnsorted(a, n): # increasing and decreasing are two functions. # if function return True value then print # 0 otherwise 3. if (increasing(a, n) == True or decreasing(a, n) == True): return 0 else: return 3 # Driver code ar = [7, 9, 10, 8, 11] n = len(ar) print(shortestUnsorted(ar, n)) # This code is contributed by Smitha Dinesh Semwal. Output3Time complexity: O(n) where n is the length of the array.Auxiliary Space: O(1)Please refer complete article on Shortest Un-ordered Subarray for more details! Comment More infoAdvertise with us Next Article Python3 Program for Shortest Un-ordered Subarray kartik Follow Improve Article Tags : Searching Sorting Python Python Programs DSA Arrays Oracle +3 More Practice Tags : OracleArrayspythonSearchingSorting +1 More Similar Reads Python3 Program for Size of The Subarray With Maximum Sum An array is given, find length of the subarray having maximum sum.Examples : Input : a[] = {1, -2, 1, 1, -2, 1}Output : Length of the subarray is 2Explanation: Subarray with consecutive elements and maximum sum will be {1, 1}. So length is 2Input : ar[] = { -2, -3, 4, -1, -2, 1, 5, -3 }Output : Leng 4 min read Python Program For Finding Subarray With Given Sum - Set 1 (Nonnegative Numbers) Given an unsorted array of nonnegative integers, find a continuous subarray which adds to a given number. Examples : Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33 Output: Sum found between indexes 2 and 4 Sum of elements between indices 2 and 4 is 20 + 3 + 10 = 33 Input: arr[] = {1, 4, 0, 0, 3, 10, 5 min read Python3 Program to Find the subarray with least average Given an array arr[] of size n and integer k such that k <= n.Examples : Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, k = 3Output: Subarray between indexes 3 and 5The subarray {20, 10, 50} has the least average among all subarrays of size 3.Input: arr[] = {3, 7, 5, 20, -10, 0, 12}, k = 2Output: Sub 3 min read Python3 Program to Find the smallest missing number Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array. Examples Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0Input: {0, 1, 2, 3}, n = 4, m = 5 4 min read Python3 Program to Find if there is a subarray with 0 sum Given an array of positive and negative numbers, find if there is a subarray (of size at-least one) with 0 sum.Examples : Input: {4, 2, -3, 1, 6}Output: true Explanation:There is a subarray with zero sum from index 1 to 3.Input: {4, 2, 0, 1, 6}Output: true Explanation:There is a subarray with zero s 3 min read Python3 Program to Move all zeroes to end of array Given an array of random numbers, Push all the zero's of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity i 4 min read Python3 Program for Find k pairs with smallest sums in two arrays Given two integer arrays arr1[] and arr2[] sorted in ascending order and an integer k. Find k pairs with smallest sums such that one element of a pair belongs to arr1[] and other element belongs to arr2[]Examples: Input : arr1[] = {1, 7, 11} arr2[] = {2, 4, 6} k = 3Output : [1, 2], [1, 4], [1, 6]Exp 3 min read Python3 Program to Find Lexicographically smallest rotated sequence | Set 2 Write code to find lexicographic minimum in a circular array, e.g. for the array BCABDADAB, the lexicographic minimum is ABBCABDADInput Constraint: 1 < n < 1000 Examples: Input: GEEKSQUIZOutput: EEKSQUIZGInput: GFGOutput: FGGInput : CAPABCQOutput : ABCQCAP We have discussed a O(n2Logn) solutio 2 min read Python3 Program for Queries to find maximum sum contiguous subarrays of given length in a rotating array Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types:If X = 1, rotate the given array to the left by Y positions.If X = 2, print the maximum sum subarray of length Y in the current state of the array.Examples: Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2, Qu 4 min read Python3 Program for Check if an array is sorted and rotated Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 }Output : YESThe above arra 3 min read Like