Shortest Un-ordered Subarray Last Updated : 04 Aug, 2022 Summarize Comments Improve Suggest changes Share 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 DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 4 min read Like