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

125
Vistas
Returning index of an Array when it stops increasing and begins decreasing

having a problem when running two tests using this code, test 1 returns the whole array, test2 returns the appropriate index. (the point in the array in which it stops decreasing or increasing and starts the reverse) if there is only one sequence it should return -1

the test cases are Input: [-4, -2, 9, 10] Output: -1 Input: [5, 4, 3, 2, 10, 11] Output: 3

for (i = 0; i < arr.length; i++) {
    while (arr[i] < arr[i + 1]) {
      i++;
      if (arr[i] < arr[i - 1]) {
        return -1
      }
      else if (arr[i] > arr[i + 1]) {
        return i
      }
    } while (arr[i] > arr[i + 1]) {
      i++;
      if (arr[i] > arr[i - 1]) {
        return -1
      } else if (arr[i] < arr[i + 1]) {
        return i
      }
    }
  }
  return arr;

}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

In the solution below, the isHomogeneous() method returns true if the content of the array is ever-increasing or ever-decreasing. The isIncreasing() method returns true if the array content is constantly decreasing. The isDecreasing() method returns true if the array content is ever-increasing. The getAscendingIndex() method returns the index at which the contents of the array begin to decrease. The getDescendingIndex() method returns the index at which the contents of the array begin to decrease. The application() method contains application code that executes other methods. If the content of the array is not homogeneous (continuously increasing or decreasing), the first index value at which the values start to increase in an array that starts with decreasing values is obtained using the getDescendingIndex() method.

/* Returns true if the array is descending to the elements (homogeneous). */
function isIncreasing(array) {
  for(let i = 0 ; i < array.length ; ++i)
    if(array[i] < array[i + 1])
      return false;
  return true;
}

/* Returns true if the array is incremental (homogeneous) to the elements. */
function isDecreasing(array) {
  for(let i = 0 ; i < array.length ; ++i)
    if(array[i] > array[i + 1])
      return false;
  return true;
}

/* Returns true if the array content is ever-increasing or ever-decreasing. */
function isHomogeneous(array) {
  return isIncreasing(array) || isDecreasing(array);
}

/* return the descending index in the array starting with increasing. */
function getAscendingIndex(array) {
  for(let i = 0 ; i < array.length ; ++i)
    if(array[i] > array[i + 1])
      return i;
  return -1;
}

/* return increasing index in array starting with decreasing. */
function getDescendingIndex(array) {
  for(let i = 0 ; i < array.length ; ++i)
    if(array[i] < array[i + 1])
      return i;
  return -1;
}

/* demo */
function application() {
  let firstArray = [-4, -2, 9, 10];
  let secondArray = [5, 4, 3, 2, 10, 11];
  
  console.log(`Increasing: ${(isIncreasing(firstArray) == true) ? "true" : "false"}`);
  console.log(`Decreasing: ${(isDecreasing(firstArray) == true) ? "true" : "false"}`);
  console.log(`First Array Index: ${getAscendingIndex(firstArray)}`);
  
 if(!isHomogeneous(secondArray) && getAscendingIndex(secondArray) != -1) {
    console.log(`Second Array Index: ${getDescendingIndex(secondArray)}`);
  }
}

application();

over 4 years ago · Santiago Trujillo Denunciar

0

Maybe this approach would be useful:

Find the index of the maximum and minimum element. Check if this index is the beginning or the end of the array and retern -1 if it is. And last return the maximal index out of the found ones.

  const first = [-4, -2, 9, 10];
  const second = [5, 4, 3, 2, 10, 11];
  
  const getMyIndex = (arr) => {
    const maxIndex = arr.indexOf(Math.max(...arr));
    const minIndex = arr.indexOf(Math.min(...arr));
    const getResult = (index) => 
      (index === 0) || (index === arr.length - 1) ? -1 : index;
    
    return Math.max(getResult(maxIndex), getResult(minIndex));   
  };
  
  console.log('arr:', first, 'result:', getMyIndex(first));
  console.log('arr:', second, 'result:', getMyIndex(second));
.as-console-wrapper{min-height: 100%!important; top: 0}

over 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