
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Length of shortest unsorted array in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.
Our function needs to find the length of one continuous subarray such that if we only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.
For example, if the input to the function is −
const arr = [3, 7, 5, 9, 11, 10, 16];
Then the output should be −
const output = 5;
Output Explanation
Because if we sort [7, 5, 9, 11, 10], the whole array will be sorted.
Example
Following is the code −
const arr = [3, 7, 5, 9, 11, 10, 16]; const shortestLength = (arr = []) => { const sorted = [...arr].sort((a, b) => a - b) let start = 0 let end = sorted.length - 1 while (sorted[start] === arr[start] && start < arr.length) { start += 1 } while (sorted[end] === arr[end] && end >= 0) { end -= 1 } return end >= start ? end - start + 1 : 0 } console.log(shortestLength(arr));
Output
Following is the console output −
5
- Related Articles
- Shortest Unsorted Continuous Subarray in Python
- Find the shortest string in an array - JavaScript
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- JavaScript - length of array objects
- Get the longest and shortest string in an array JavaScript
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted in Python
- Program to find length of shortest supersequence in Python
- Length of a JavaScript associative array?
- Encode String with Shortest Length in C++
- JavaScript Array length property
- Find Mean and Median of an unsorted Array in Java
- Shortest path algorithms in Javascript
- Maximum length of mountain in an array using JavaScript
- Java Program to sort integers in unsorted array
- K’th Smallest/Largest Element in Unsorted Array in C++

Advertisements