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

361
Vistas
How to check strictly increasing and decreasing array in javascript

I solved an exercise in javascript but I'm not very happy with my solution and I couldn't find a better one.

Problem: check if an array have a starting subarray strictly increasing and an ending subarray strictly decreasing. Each array has at least 3 elements.

Examples:

checkSledJump([1, 2, 3, 2, 1]) // true: strictly increasing and then strictly decreasing
checkSledJump([0, 1, 0])       // -> true: strictly increasing and then strictly decreasing
checkSledJump([0, 3, 2, 1])    // -> true: strictly increasing and then strictly decreasing
checkSledJump([0, 1000, 1])    // -> true: strictly increasing and then strictly decreasing

checkSledJump([2, 4, 4, 6, 2])       // false: [4,4] isn't strictly increasing or decreasing
checkSledJump([1, 2, 3])             // false: only increasing
checkSledJump([3, 2, 1])             // false: only decreasing
checkSledJump([1, 2, 3, 2, 1, 2, 3]) // false: increasing then decreasing then increasing

My solution:

function checkSledJump(heights) {
  let max = Math.max(...heights);
  let maxIndex = heights.indexOf(max);
  if (maxIndex === 0 || maxIndex === heights.length-1) return false
  
  let strictlyIncreasing = heights.slice(0, maxIndex+1)
  let strictlyDecreasing = heights.slice(maxIndex);

  for(let i = 0; i < strictlyIncreasing.length - 1; i++)
    if(!(strictlyIncreasing[i] < strictlyIncreasing[i+1])) return false

  for(let i = 0; i < strictlyDecreasing.length - 1; i++)
    if(!(strictlyDecreasing[i] > strictlyDecreasing[i+1])) return false
  
  return true
}

Is there a better way to do it? Maybe using reduce?

Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can create difference graph and check for the specific pattern.

Here you can find two patterns first return condition check whether array elements follows increment and then decrement pattern.

Second return condition check whether array elements follows and decrement then increment pattern.

function checkSledJump(heights) {
    let graph = [];
    for (let i = 0; i < heights.length - 1; i++) {
        const diff = heights[i + 1] - heights[i];
        if(!diff) return false;
        graph.push(diff > 0 ? 1 : 0);
    }

    const graphString = graph.join('');

    return graphString.lastIndexOf('1') + 1 && graphString.lastIndexOf('1') < graphString.indexOf('0') ? true : false; // pattern increment => decrement
    //return graphString.lastIndexOf('0') + 1 && graphString.lastIndexOf('0') < graphString.indexOf('1')? true : false; // pattern decrement => increment
}

console.log(checkSledJump([1, 2, 3, 2, 1]));        // true: strictly increasing and then strictly decreasing
console.log(checkSledJump([0, 1, 0]));              // -> true: strictly increasing and then strictly decreasing
console.log(checkSledJump([0, 3, 2, 1]));           // -> true: strictly increasing and then strictly decreasing
console.log(checkSledJump([0, 1000, 1]));           // -> true: strictly increasing and then strictly decreasing
console.log(checkSledJump([2, 4, 4, 6, 2]));        // false: [4,4] isn't strictly increasing or decreasing
console.log(checkSledJump([1, 2, 3]));              // false: only increasing
console.log(checkSledJump([3, 2, 1]));              // false: only decreasing
console.log(checkSledJump([1, 2, 3, 2, 1, 2, 3]));  // false: increasing then decreasing then increasing
console.log(checkSledJump([1, 2, 3, 2, 2, 1]));

about 4 years ago · Juan Pablo Isaza 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