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

691
Views
find the pair of adjacent elements that has the largest product (incorrect message)

i keep getting incorrect message please explain me what is wrong?

here is the challenge question: Given an array of integers, find the pair of adjacent elements that has the largest product and return that product. Example

ForinputArray = [3, 6, -2, -5, 7, 3],the output should be adjacentElementsProduct(inputArray) = 21.

7 and 3 produce the largest product.

here is my code answer:

function adjacentElementsProduct(inputArray) {
for(let i=0;i<inputArray.length;i++){
    let prod =Math.max(inputArray[i]*inputArray[i+1]);
   

}
 return prod;
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Things you should Correct:

  1. When the index reaches to the length of inputArray, the inputArray[i+1] will throw an OutOfIndex error, since the indexes' range are from 0 to inputArray.length - 1, and here the last i will have the value of inputArray.length - 1 then the value of i+1 will be inputArray.length and it's not correct.
  2. You should make a variable (consider max_res) to save the prevoius maximum result and compare it to new results.
  3. Define the prod variable before the for loop, because variables which are first defined in the loop they won't be accessible after the loop ends.
  4. Define a vairable to store the index of i that makes the maximum value, since the question wanted the index ,too.

You should change your code like this, to get the correct output:

function adjacentElementsProduct(inputArray) {
  let max_res = Number.NEGATIVE_INFINITY;
  let res_index = -1;
  let prod = 0;
  for (let i = 0; i < inputArray.length - 1; i++) {
    prod = Math.max(inputArray[i] * inputArray[i + 1]);  
    if (prod > max_res){
        max_res = prod;
        let res_index = i;
    }
  }
  return prod;
}

let inputArray = [3, 6, -2, -5, 7, 3]
let prod = adjacentElementsProduct(inputArray)

console.log(prod);

about 4 years ago · Juan Pablo Isaza Report

0

1) You have to get the max of two numbers as

sum = Math.max(sum, inputArray[i] * (inputArray[i + 1] ?? 1));

2) You also have to handle the last case where i + 1 will be undefined then you have multiply by 1. You can use null coalescing operator

(inputArray[i + 1] ?? 1)

function adjacentElementsProduct(inputArray) {
  let sum = 0;
  for (let i = 0; i < inputArray.length; i++) {
    sum = Math.max(sum, inputArray[i] * (inputArray[i + 1] ?? 1));
  }
  return sum;
}

const inputArray = [3, 6, -2, -5, 7, 3];
console.log(adjacentElementsProduct(inputArray));

about 4 years ago · Juan Pablo Isaza Report

0

Number.MIN_SAFE_INTEGER will allow the smallest integer because this needs to find the largest sum which may include negative integers.

function solution(inputArray) {
    let largestProduct = Number.MIN_SAFE_INTEGER;
    for (let i = 0; i < inputArray.length; i++) {
        if(inputArray[i] * inputArray[i + 1] > largestProduct) {
            largestProduct = inputArray[i] * inputArray[i + 1];
        }
    }
    return largestProduct;
}
about 4 years ago · Juan Pablo Isaza 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!