Created
August 5, 2018 12:10
-
-
Save ovarunendra/d48d1fa7b6983a36ebaebe52ada22ad8 to your computer and use it in GitHub Desktop.
JS Bin findLongestConseqSubseq // source https://jsbin.com/facezoq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="findLongestConseqSubseq"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
const findLongestConseqSubseq = (arr) => { | |
let s = new Set(); | |
let ans = 0; | |
for (let elm of arr) { | |
s.add(elm); | |
} | |
for (let elm of arr) { | |
if(!s.has(elm-1)) { | |
let j = elm; | |
while(s.has(j)) { | |
j += 1; | |
} | |
ans = Math.max(ans, j-elm); | |
} | |
} | |
return ans; | |
} | |
const arr = [36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42]; | |
console.log(findLongestConseqSubseq(arr)) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const findLongestConseqSubseq = (arr) => { | |
let s = new Set(); | |
let ans = 0; | |
for (let elm of arr) { | |
s.add(elm); | |
} | |
for (let elm of arr) { | |
if(!s.has(elm-1)) { | |
let j = elm; | |
while(s.has(j)) { | |
j += 1; | |
} | |
ans = Math.max(ans, j-elm); | |
} | |
} | |
return ans; | |
} | |
const arr = [36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42]; | |
console.log(findLongestConseqSubseq(arr))</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const findLongestConseqSubseq = (arr) => { | |
let s = new Set(); | |
let ans = 0; | |
for (let elm of arr) { | |
s.add(elm); | |
} | |
for (let elm of arr) { | |
if(!s.has(elm-1)) { | |
let j = elm; | |
while(s.has(j)) { | |
j += 1; | |
} | |
ans = Math.max(ans, j-elm); | |
} | |
} | |
return ans; | |
} | |
const arr = [36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42]; | |
console.log(findLongestConseqSubseq(arr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment