Two Crystal Balls O(Math.sqrt(n))

TheTechnoCult
Oct 24, 2023

--

Given 2 crystal balls that will break if dropped from a high enough distance, determine the exact spot in which they’ll break in the most optimized way.

Solution:

function two_crystal_balls(breaks) {
const jmpAmount = Math.floor(Math.sqrt(breaks.length));

let i = jmpAmount;
for (; i < breaks.length; i += jmpAmount) {
if (breaks[i]) {
break;
}
}
console.log(i, "i");

i -= jmpAmount;
console.log(i, "i");

for (let j = i; j < breaks.length; j++) {
if (breaks[j]) {
console.log(j, "i");
return j;
}
}
return -1;
}

console.log(two_crystal_balls([false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, true, true, true]))

--

--

No responses yet