Home > Back-end >  Find longest sequence of zeros in binary representation of an integer in JavaScript?
Find longest sequence of zeros in binary representation of an integer in JavaScript?

Time:12-13

  1. Well in this question I have to find the longest sequence of zeros in the binary representation of an integer.
  2. After a lot of hard work, I'm able to find the longest sequence of zeros with my logic which I changed many times.
  3. I have one problem with my logic which is if I input an integer that has no gap in it has to give me an answer 0 which I tried to make by myself but I failed.
  4. Right now if I input an integer that has no gap it gives me output infinity.
  5. I want my answer to print 0 when the binary representation of an integer has no gap in it.

var dn = prompt("Enter a number: ");
var bn = new Array();
var i = 0;
var binary = [];

while (dn != 0) {
  bn[i] = dn % 2;
  dn = Math.floor(dn / 2);
  i  ;
}

for (var j = i - 1; j >= 0; j--) {
  binary.push(bn[j]);
}
console.log(binary.join(""));

var currentGap = 0;
var gaps = [];
var len = binary.length;

for (var k = 0; k < len; k  ) {
  if (binary[k] == 0) {
    currentGap  ;
    if (binary[k   1] == 1) {
      gaps.push(currentGap);
      currentGap = 0;
    }
  }
}

console.log(Math.max(...gaps));

CodePudding user response:

You can simply print 0 if your gaps array has no length.

var dn = prompt("Enter a number: ");
var bn = new Array();
var i = 0;
var binary = [];

while (dn != 0) {
  bn[i] = dn % 2;
  dn = Math.floor(dn / 2);
  i  ;
}

for (var j = i - 1; j >= 0; j--) {
  binary.push(bn[j]);
}
console.log(binary.join(""));

var currentGap = 0;
var gaps = [];
var len = binary.length;

for (var k = 0; k < len; k  ) {
  if (binary[k] == 0) {
    currentGap  ;
    if (binary[k   1] == 1) {
      gaps.push(currentGap);
      currentGap = 0;
    }
  }
}

console.log(gaps.length ? Math.max(...gaps) : 0); // <-- This

CodePudding user response:

You could add initialize gaps with 0, or add condition when gaps remains empty to print 0 else max of gaps.

var dn = prompt("Enter a number: ");
var bn = new Array();
var i = 0;
var binary = [];

while (dn != 0) {
  bn[i] = dn % 2;
  dn = Math.floor(dn / 2);
  i  ;
}

for (var j = i - 1; j >= 0; j--) {
  binary.push(bn[j]);
}
console.log(binary.join(""));

var currentGap = 0;
//var gaps = []
var gaps = [0];
var len = binary.length;

for (var k = 0; k < len; k  ) {
  if (binary[k] == 0) {
    currentGap  ;
    if (binary[k   1] == 1) {
      gaps.push(currentGap);
      currentGap = 0;
    }
  }
}
//if (gaps.length === 0)
//  console.log(0)
//else
//  console.log(Math.max(...gaps));
console.log(Math.max(...gaps));

CodePudding user response:

I apologize if I'm misunderstanding your question, but would this satisfy your problem?

const s = prompt("Enter a number:");
const longest = Math.max(...parseInt(s).toString(2).split('1').map(s=>s.length));

console.log(longest);

CodePudding user response:

just change var gaps = [];
to var gaps = [0];

You can also do that...

let dn = parseInt(prompt("Enter a number: "))

if (isNaN(dn)) console.log('Not a Number!')
else
  {
  let
    dnStr = dn.toString(2)  // binary convertion
  , count = 0
  , zVal  = '0'
    ;
  while (dnStr.includes(zVal) ) 
    {
    count  
    zVal  = '0'
    }

  console.log( `${dnStr}\n--> ${count}` )
  }

If you want to do your own binary convertion, prefert o use Javasscript binary opérators:

let dn = parseInt(prompt("Enter a number: "))

if (isNaN(dn)) console.log('Not a Number!')
else
  {
  let gap   = [0]
  let Bin   = []
  let count = 0
  for (;;)
    {
    Bin.unshift(  dn & 1  )  // --> dn % 2
    if (dn & 1) 
      {
      gap.push(count)
      count = 0  
      }
    else count  

    dn >>>=1          // --> dn = Math.floor(dn / 2)
    if (!dn) break    // --> if (dn === 0 )
    }

  console.log(Bin.join(''))
  console.log(Math.max(...gap))
  }

  • Related