Skip to content

Instantly share code, notes, and snippets.

@theptrk
Created July 18, 2017 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theptrk/a6432fe099cf4bb3cde2baf5f2382256 to your computer and use it in GitHub Desktop.
Save theptrk/a6432fe099cf4bb3cde2baf5f2382256 to your computer and use it in GitHub Desktop.
Algo. K shifted array. Find K
// 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