Shortest Un-ordered Subarray Last Updated : 04 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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 11 Output : 3 Explanation : 9 10 8 unordered sub array. Input : n = 5 1 2 3 4 5 Output : 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. Implementation: C++ // CPP program to find shortest subarray which is // unsorted. #include <bits/stdc++.h> using namespace std; // bool function for checking an array elements // are in increasing. bool increasing(int a[], int n) { for (int i = 0; i < n - 1; i++) if (a[i] >= a[i + 1]) return false; return true; } // bool function for checking an array // elements are in decreasing. bool decreasing(int a[], int n) { for (int i = 0; i < n - 1; i++) if (a[i] < a[i + 1]) return false; return true; } int shortestUnsorted(int a[], int n) { // increasing and decreasing are two functions. // if function return true value then print // 0 otherwise 3. if (increasing(a, n) == true || decreasing(a, n) == true) return 0; else return 3; } // Driver code int main() { int ar[] = { 7, 9, 10, 8, 11 }; int n = sizeof(ar) / sizeof(ar[0]); cout << shortestUnsorted(ar, n); return 0; } Java // JAVA program to find shortest subarray which is // unsorted. import java.util.*; import java.io.*; class GFG { // boolean function to check array elements // are in increasing order or not public static boolean increasing(int a[],int n) { for (int i = 0; i < n - 1; i++) if (a[i] >= a[i + 1]) return false; return true; } // boolean function to check array elements // are in decreasing order or not public static boolean decreasing(int arr[],int n) { for (int i = 0; i < n - 1; i++) if (arr[i] < arr[i + 1]) return false; return true; } public static int shortestUnsorted(int a[],int n) { // increasing and decreasing are two functions. // if function return true value then print // 0 otherwise 3. if (increasing(a, n) == true || decreasing(a, n) == true) return 0; else return 3; } // driver program public static void main (String[] args) { int ar[] = new int[]{7, 9, 10, 8, 11}; int n = ar.length; System.out.println(shortestUnsorted(ar,n)); } } // This code is contributed by Akash Singh. 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. C# // Program to find the shortest // subarray which is unsorted. using System; class GFG { // boolean function to check // array elements are in the // increasing order or not public static bool increasing(int[] a, int n) { for (int i = 0; i < n - 1; i++) if (a[i] >= a[i + 1]) return false; return true; } // boolean function to check // array elements are in the // decreasing order or not public static bool decreasing(int[] arr, int n) { for (int i = 0; i < n - 1; i++) if (arr[i] < arr[i + 1]) return false; return true; } public static int shortestUnsorted(int[] a, int n) { // increasing and decreasing are // two functions. function return // true value then print 0 else 3 if (increasing(a, n) == true || decreasing(a, n) == true) return 0; else return 3; } // Driver program public static void Main() { int[] ar = new int[] { 7, 9, 10, 8, 11 }; int n = ar.Length; Console.WriteLine(shortestUnsorted(ar, n)); } } // This code is contributed by vt_m. PHP <?php // php program to find shortest // subarray which is unsorted. // bool function for checking an // array elements are in increasing. function increasing($a, $n) { for ( $i = 0; $i < $n - 1; $i++) if ($a[$i] >= $a[$i + 1]) return false; return true; } // bool function for checking an // array elements are in decreasing. function decreasing($a, $n) { for ($i = 0; $i < $n - 1; $i++) if ($a[$i] < $a[$i + 1]) return false; return true; } function shortestUnsorted($a, $n) { // increasing and decreasing are // two functions. if function // return true value then print // 0 otherwise 3. if (increasing($a, $n) == true || decreasing($a, $n) == true) return 0; else return 3; } // Driver code $ar = array( 7, 9, 10, 8, 11 ); $n = sizeof($ar); echo shortestUnsorted($ar, $n); // This code is contributed by // nitin mittal. ?> JavaScript <script> // JavaScript program to find shortest subarray which is // unsorted. // boolean function to check array elements // are in increasing order or not function increasing(a, n) { for (let i = 0; i < n - 1; i++) if (a[i] >= a[i + 1]) return false; return true; } // boolean function to check array elements // are in decreasing order or not function decreasing(arr, n) { for (let i = 0; i < n - 1; i++) if (arr[i] < arr[i + 1]) return false; return true; } function shortestUnsorted(a, n) { // increasing and decreasing are two functions. // if function return true value then print // 0 otherwise 3. if (increasing(a, n) == true || decreasing(a, n) == true) return 0; else return 3; } // Driver Code let ar = [7, 9, 10, 8, 11]; let n = ar.length; document.write(shortestUnsorted(ar,n)); // This code is contributed by chinmoy1997pal. </script> Output : 3 Time complexity: O(n) where n is the length of the array.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Shortest Un-ordered Subarray A aditya1011 Follow Improve Article Tags : Misc Searching Sorting DSA Arrays Oracle +2 More Practice Tags : OracleArraysMiscSearchingSorting +1 More Similar Reads Javascript Program for Shortest Un-ordered Subarray An array is given of n length, and the problem is that we have to find the length of the shortest unordered {neither increasing nor decreasing} subarray in the 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 Explanat 2 min read Smallest sum contiguous subarray Given an array containing n integers. The problem is to find the sum of the elements of the contiguous subarray having the smallest(minimum) sum.Examples: Input: arr[] = {3, -4, 2, -3, -1, 7, -5}Output: -6Explanation: Subarray with minimum sum is {-4, 2, -3, -1} = -6Input: arr = {2, 6, 8, 1, 4}Outpu 7 min read Sum of all Subarrays Given an integer array arr[], find the sum of all sub-arrays of the given array. Examples: Input: arr[] = [1, 2, 3]Output: 20Explanation: {1} + {2} + {3} + {2 + 3} + {1 + 2} + {1 + 2 + 3} = 20Input: arr[] = [1, 2, 3, 4]Output: 50Naive Approach - O(n^2) Time and O(1) SpaceA simple solution is to gene 6 min read Smallest subarray with positive sum for all indices Given an array arr[] of size N. The task is to determine the minimum length of a subarray starting from index i, such that the sum of the subarray is strictly greater than 0. Calculate the length for all i's in the range 1 to N. If no such subarray exists, then return 0. Examples: Input: N = 3, arr[ 8 min read Longest Decreasing Subsequence Given an array of N integers, find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in strictly decreasing order. Examples: Input: arr[] = [15, 27, 14, 38, 63, 55, 46, 65, 85] Output: 3 Explanation: The longest decreasing subsequence is { 14 min read MEX Subarray Count Given an array of size N, each element of the array lies between [0, N-1] i.e., the given array is a permutation. The task is to determine the number of subarrays whose MEX is greater than the Median. Note: MEX is the smallest whole number that is not present in the array. Examples: Input: N=4, arr= 13 min read Smallest subarray with k distinct numbers We are given an array consisting of n integers and an integer k. We need to find the smallest subarray [l, r] (both l and r are inclusive) such that there are exactly k different numbers. If no such subarray exists, print -1 and If multiple subarrays meet the criteria, return the one with the smalle 14 min read Longest equilibrium Subarray of given Array Given an integer array arr of size N[], the task is to find the longest equilibrium subarray i.e. a subarray such that the prefix sum of the remaining array is the same as the suffix sum. Examples: Input: N = 3, arr[] = {10, 20, 10}Output: 1Explanation: The longest subarray is {20}. The remaining pr 8 min read Generating All Subarrays Given an array arr[], the task is to generate all the possible subarrays of the given array.Examples: Input: arr[] = [1, 2, 3]Output: [ [1], [1, 2], [2], [1, 2, 3], [2, 3], [3] ]Input: arr[] = [1, 2]Output: [ [1], [1, 2], [2] ]Iterative ApproachTo generate a subarray, we need a starting index from t 8 min read Shortest Supersequence by Sequence Reconstruction Given an array arr[] and a 2D array sequences[], determine if arr is the shortest possible and the only supersequence for the given sequences. A supersequence is a sequence that has all sequences[i] as subsequences. The task is to return true if arr is the only shortest supersequence for sequences, 8 min read Like