Javascript Program for Shortest Un-ordered Subarray Last Updated : 17 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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 Explanation : Array is in increasing order.The idea is based on the fact that the size of the 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. JavaScript // 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; console.log(shortestUnsorted(ar, n)); Output3 Complexity Analysis:Time 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 Javascript Program for Shortest Un-ordered Subarray kartik Follow Improve Article Tags : Searching Sorting JavaScript Web Technologies DSA Arrays Oracle +3 More Practice Tags : OracleArraysSearchingSorting Similar Reads Shortest Un-ordered Subarray 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 : 6 min read Javascript 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 Javascript Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2}Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0 5 min read Longest Subarray consisting of unique elements from an Array Given an array arr[] consisting of N integers, the task is to find the largest subarray consisting of unique elements only. Examples: Input: arr[] = {1, 2, 3, 4, 5, 1, 2, 3} Output: 5 Explanation: One possible subarray is {1, 2, 3, 4, 5}. Input: arr[]={1, 2, 4, 4, 5, 6, 7, 8, 3, 4, 5, 3, 3, 4, 5, 6, 5 min read Smallest Subarray with Sum K from an Array Given an array arr[] consisting of N integers, the task is to find the length of the Smallest subarray with a sum equal to K. Examples: Input: arr[] = {2, 4, 6, 10, 2, 1}, K = 12 Output: 2 Explanation: All possible subarrays with sum 12 are {2, 4, 6} and {10, 2}. Input: arr[] = {-8, -8, -3, 8}, K = 10 min read Javascript 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, 5 min read JavaScript Program to find the Nth smallest/largest element from an unsorted Array In this article, we will see how to find Nth largest and smallest element from an unsorted array. The Nth smallest/largest element from an unsorted array refers to the element that ranks at the Nth position when the array is sorted either in ascending (smallest) or descending (largest) order. It rep 4 min read Smallest subarray such that all elements are greater than K Given an array of N integers and a number K, the task is to find the length of the smallest subarray in which all the elements are greater than K. If there is no such subarray possible, then print -1. Examples: Input: a[] = {3, 4, 5, 6, 7, 2, 10, 11}, K = 5 Output: 1 The subarray is {10} Input: a[] 4 min read Find smallest subarray that contains all elements in same order Given two arrays of integers of size m and n. The task is to find the minimum length subarray in the first array that contains all the elements of second array. Note: element of second array may be present in the large array in non-contiguous but order must same. ( m < n ) Examples : Input : A[] 14 min read Smallest subarray which upon repetition gives the original array Given an array arr[] of N integers, the task is to find the smallest subarray brr[] of size at least 2 such that by performing repeating operation on the array brr[] gives the original array arr[]. Print "-1" if it is not possible to find such a subarray. A repeating operation on an array is to appe 8 min read Like