Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!