Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

364
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda