Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
What is wrong with my solution?(hackerrank exercise - bitwise operation)

the link for the excersize In short, ‘n’ is series of number from 1 to n, and ‘k’ is a number. I need to return the largest result of a&b (a<b) as long as it’s smaller than k, for example 1&2, 1&3 …2&3,2&4…

I get 0 whenever I run this function:

function getMaxLessThanK(n, k) {
  let maxPV = 0;
  for (let a = 1; a < n; a++) {
    for (let b = a + 1; b <= n; b++) {
      if (a & b < k && a & b > maxPV) {
        maxPV = (a & b)
      }
    }
  }
  return maxPV
}
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It's an operator precedence problem.

Comparison operators (<, >, ...) have higher precedence than bitwise AND (&), so you need to use parentheses to group:

if ((a & b) < k && (a & b) > maxPV) {
    // ...
}

Complete snippet:

function getMaxLessThanK(n, k) {
  let maxPV = 0;
  for (let a = 1; a < n; a++) {
    for (let b = a + 1; b <= n; b++) {
      if ((a & b) < k && (a & b) > maxPV) {
        maxPV = (a & b);
      }
    }
  }
  return maxPV;
}

console.log(getMaxLessThanK(8, 5));

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda