Today I have solved the problem in javascript

 Given an integer n, find the next biggest integer with the same number of 1-bits on. For example, given the number 6 (0110 in binary), return 9 (1001).

const inputNumber = 6;

const result = nextIntegerWithSameBits(inputNumber);

function nextIntegerWithSameBits(n) {

   const countBits = (num) => num.toString(2).split('1').length - 1;

 const originalBits = countBits(n);

 let nextInteger = n + 1;

    while (countBits(nextInteger) !== originalBits) {

        nextInteger++;

    }

 return nextInteger;

}

console.log(result);  


Output:

9

Comments

Popular posts from this blog

Building a Full-Stack Student Management System with React.js and NestJS

Today I have solved the problem in javascript

Today I have solved problem in javascript