Created
July 18, 2017 03:49
-
-
Save theptrk/a6432fe099cf4bb3cde2baf5f2382256 to your computer and use it in GitHub Desktop.
Algo. K shifted array. Find K
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
// TODO TEST | |
// given [3,4,5,6,7,8,9,1,2] | |
// return 7 | |
const test1 = [3,4,5,6,7,8,9,1,2] | |
const test2 = [8,9,1,2,3,4,5,6,7] | |
const findk = list => { | |
if (list[0] < list[list.length-1]) { | |
return 0; | |
} | |
let leftIndex = 0; | |
let rightIndex = list.length - 1; | |
while (left < right) { | |
let left = list[leftIndex] | |
let right = list[rightIndex] | |
let midIndex = Math.floor(list.length/2); | |
let mid = list[midIndex] | |
if left > mid | |
rightIndex = midIndex | |
else | |
leftIndex = midIndex | |
} | |
return list[rightIndex]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment