Given an array of integers, find the pair of adjacent elements that has the largest product and return that product

前端 未结 5 1972
情书的邮戳
情书的邮戳 2021-01-19 12:22

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

and here is my code

function ad         


        
相关标签:
5条回答
  • 2021-01-19 12:43

    You can try to create a new array of length (arr.length-1) inside the function and append the products of adjacent numbers to this new array. Then find the largest number in the array and return it. This will solve the problem with negative product.

    function adjacentElementsProduct(inputArray) {
      var arr = inputArray;
      var prodArr[];
      var p;
      for (var i = 0; i < arr.length-1; i++) {
        prodArr[i] = arr[i]*arr[i+1];
      };
      for (j=prodArr.length; j--){
      if (prodArr[j] > p) {
          p = prodArr[j];
        };
      return p;
    };
    
    console.log(adjacentElementsProduct([-23, 4, -3, 8, -12]));
    
    0 讨论(0)
  • 2021-01-19 12:46

    You could start with a really large negative value, instead of zero.

    var p = -Infinity;
    
    0 讨论(0)
  • 2021-01-19 12:49

    This is quite simple actually

    function adjacentElementsProduct(inputArray) {
        let max = -Infinity;
        for (let i = 1; i < inputArray.length; i++) {
            max = Math.max(inputArray[i] * inputArray[i - 1], max);
        }
    
        return max;
    }
    
    0 讨论(0)
  • 2021-01-19 12:58

    You are initializing the variable p to zero. That means any multiplication values smaller than that are not accepted. Rather set it to the smallest possible integer value:

    var p = Number.MIN_SAFE_INTEGER;
    

    function adjacentElementsProduct(inputArray) {
      var arr = inputArray;
      var x = 0;
      var y = 0;
      var p = Number.MIN_SAFE_INTEGER;
      for (var i = 0; i < arr.length; i++) {
        x = arr[i];
        y = arr[i + 1];
        if (x * y > p) {
          p = x * y;
        };
      };
      return p;
    };
    
    console.log(adjacentElementsProduct([-23, 4, -3, 8, -12]));

    0 讨论(0)
  • 2021-01-19 13:02

    Here's a very simple implementation without using any additional variables (actually less), and no special values. Just simple logic.

    function adjacentElementsProduct(inputArray) {
        var c =inputArray[0]*inputArray[1];
        var p = c;
        for(var i=1;i<inputArray.length;i++){
            console.log(c);
            var c=inputArray[i]*inputArray[i+1];
            if(c > p){
                p=c;
            };
        };
        return p;
    };
    console.log("minimum product = " + adjacentElementsProduct([-23,4,-3,8,-12]));

    What I did was, initialize a variable c (current product) with the product of first two elements of the array. And then I declared the variable p and initialize it to c. This way, all other products are compared to this product. Rest is simple.

    Hope it helps. :)

    0 讨论(0)
提交回复
热议问题