Created
January 2, 2020 11:08
-
-
Save kolyuchii/68969cbe983d587a7ab0b1495e3fc9d1 to your computer and use it in GitHub Desktop.
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
| function find(arr, k) { | |
| const obj = {}; | |
| let start = 0; | |
| let finish = 0; | |
| let beg = 0; | |
| let end = 0; | |
| for (let i = 0; i < arr.length; i += 1) { | |
| const item = beg = end = arr[i]; | |
| if (item in obj) { | |
| continue; | |
| } | |
| obj[item] = 'here'; | |
| if (item - 1 in obj) { | |
| beg = obj[item - 1]; | |
| } | |
| if (item + 1 in obj) { | |
| end = obj[item + 1]; | |
| } | |
| if (end - beg > finish - start) { | |
| [finish, start] = [end, beg]; | |
| } | |
| obj[end] = beg; | |
| obj[beg] = end; | |
| } | |
| return [start, finish]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment