Skip to content

Commit

Permalink
Shortest Unsorted Continuous Subarray Length
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh07bharvada committed Dec 27, 2020
1 parent 1f350d5 commit ab8d6e0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Structures-Wiz is a JavaScript based npm package for using awesome data structur
- [Usage](#usage)
- [Algorithms](#algo)
- [Longest Increasing Subsequence Length](#llisl)
- [Shortest Unsorted Continuous Subarray Length](#sucs)
- [Is Cycle Present in a Graph](#isCyclePresent)
- [Patience Sort](#pat-sort)
- [Topological Sort in a Graph](#top-sort)
Expand Down Expand Up @@ -109,6 +110,21 @@ console.log("Length of longest increasing subsequence is :", longestLen);// 4

```

#### Shortest Unsorted Continuous Subarray Length<a name="sucs"></a>
Method to find the length of the shortest unsorted continuous subarray from an input array.

```javascript

import { getShortestUnsortedSubarray } from 'structures-wiz';

const list = [2,6,4,8,10,9,15];

const shortedSubArrayLen = getShortestUnsortedSubarray(list);

console.log("Length of hortest Unsorted Continuous Subarray is :", shortedSubArrayLen);// 5

```


#### Is Cycle Present in Graph<a name="isCyclePresent"></a>
Method to find the cycle is to use Topological sort. First we find the inDegree of each node and then add those
Expand Down
22 changes: 22 additions & 0 deletions algorithms/util/getShortestUnsortedSubarray.util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { getPatienceSortedList } = require("../sorting/getPatienceSortedList.sort");

const getShortestUnsortedSubarray = nums => {

if(!nums || !Array.isArray(nums)){
return;
}
let copy = Array.from(nums);
copy.sort((a,b)=>a-b);
let min = Infinity, max = -Infinity;

for(let i=0;i<nums.length;i++){
if(copy[i] !== nums[i]){
min = Math.min(min, i);
max = Math.max(max, i);
}
}
return max-min + 1 === -Infinity ? 0 : max-min + 1;
};


module.exports.getShortestUnsortedSubarray = getShortestUnsortedSubarray;
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { LinkedList } = require("./data-structures/LinkedList");
const { PriorityQueue } = require("./data-structures/PriorityQueue");
const { isCyclePresentInGraph } = require("./algorithms/util/isCyclePresent.util");
const { getPatienceSortedList } = require("./algorithms/sorting/getPatienceSortedList.sort");
const { getShortestUnsortedSubarray } = require("./algorithms/util/getShortestUnsortedSubarray.util");
const { getTopologicallySortedGraph } = require("./algorithms/sorting/getTopologicallySortedGraph.sort");
const { getLongestIncreasingSubsequenceLen } = require("./algorithms/util/getLongestIncreasingSubsequenceLen");

Expand All @@ -11,5 +12,6 @@ module.exports.LinkedList = LinkedList;
module.exports.PriorityQueue = PriorityQueue;
module.exports.isCyclePresentInGraph = isCyclePresentInGraph;
module.exports.getPatienceSortedList = getPatienceSortedList;
module.exports.getShortestUnsortedSubarray = getShortestUnsortedSubarray;
module.exports.getTopologicallySortedGraph = getTopologicallySortedGraph;
module.exports.getLongestIncreasingSubsequenceLen = getLongestIncreasingSubsequenceLen;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "structures-wiz",
"version": "1.0.23",
"version": "1.0.24",
"description": "Structures-Wiz is a JavaScript based npm package for using awesome data structures like Stacks, Queue, LinkedList, PriorityQueues.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit ab8d6e0

Please sign in to comment.