Home > Web Front-end > JS Tutorial > body text

rystal Ball Searching Problem Solved Using Javascript

PHPz
Release: 2024-08-18 00:04:02
Original
347 people have browsed it

rystal Ball Searching Problem Solved Using Javascript

2 Crystal ball problem find the 1st hit with minimum time complexity.

const arr = [false, false, false, false, true, true, true, true, true, true];

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");

    const updatedPos = i - jmpAmount;

  for (let j = updatedPos; j<= i; j++) {
    if (arr[j]) {
        console.log('Answer ---> ', j);
        return ;
    }
}
  return -1;
}
two_crystal_balls(arr);

/*
Output
6 i
Answer --->  4 
*/
Copy after login

The above is the detailed content of rystal Ball Searching Problem Solved Using Javascript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!