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

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 adjacentElementsProduct(inputArray) {
 var arr = inputArray;
  var x=0;
  var y=0;
  var p=0;
  for(var i=0;i<arr.length;i++){
    x=arr[i];
    y=arr[i+1];
    if(x*y>p){
     p=x*y;
    };
  };
 return p;
};

the problem is all the tests works fine but except the array with the negative product as it shown in the attached photo can anyone help .. and thanks in advance

enter image description here



Solution 1:[1]

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

var p = -Infinity;

Solution 2:[2]

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]));

Solution 3:[3]

I had the same problem at first, defining the first max as 0. Then i came up with this:

function solution(inputArray) {
let products = inputArray.map(function(x, index){
    return inputArray[index+1] != undefined? x *inputArray[index+1] : -Infinity;
})
return Math.max(...products);

}

Solution 4:[4]

Problem: Given an array of integers, find the pair of adjacent elements that has the largest product and return that product. #javascript #arraymethods

function solution(inputArray) {
let productsArr = []; // to hold the products of adjacent elements
let n = 0;
for (let i = 0; i < inputArray.length; i++) {
    if (i < inputArray.length - 1) 
    {
        productsArr[n] = inputArray[i] * inputArray[i + 1];
        n++;
    }
}
return productsArr.reduce((aggr, val) => Math.max(aggr, val)); // to find out the biggest product
}

Solution 5:[5]

This is quite simple actually

const solution = (inputArray) => Math.max(...inputArray.slice(0, -1).map((n, index) => n * inputArray[index + 1]))

console.log(solution([3, 6, -2, -5, 7, 3]))

Solution 6:[6]

function solution(inputArray: number[]): number {
    var max = -Infinity;
    
    for(var i=0; i+1<inputArray.length; i++)
    {
        if(max<(inputArray[i]*inputArray[i+1])){
            max=inputArray[i]*inputArray[i+1];
        }
    }
    return max;
}

console.log(solution([2,3,6]))

Solution 7:[7]

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. :)

Solution 8:[8]

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;
}

Solution 9:[9]

you can try to initialize a integer as negative infinity value -math.inf and then use the python ternary operator var=true if condition else false to find the maximum value

code in python

def adjacentarray(a):
    maximum=-math.inf
    for i,in range(0,len(a)-1):
        maximum=a[i]*a[i+1] if a[i]*a[i+1]>maximum else maximum
    return maximum

code in javascript

function adjacentElementsProduct(a) {
    var maximum=-Infinity;
    for (var i=0;i<a.length-1;i++){
        maximum= a[i]*a[i+1]>maximum?a[i]*a[i+1]:maximum;
    }
    return maximum;
}

Solution 10:[10]

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]));

Solution 11:[11]

The var p which saves the max product should be initialized as small as possible instead of a 0. So that when the product is negative, it will still meet the if condition and save the value. Here is a C# solution:

 static void Main(string[] args)
        {
            int[] arr = { 1, -4, 3, -6, -7, 0 };
            Console.WriteLine(FindMaxProduct(arr));
            Console.ReadKey();
        }
 static int FindMaxProduct(int[] arr) {
            int currentProduct = 0;
            int maxProduct = int.MinValue;
            int a=0, b = 0;
            for (int i = 0, j = i + 1; i < arr.Length - 1 && j < arr.Length; i++, j++)
            {
                currentProduct = arr[i] * arr[j];
                if (currentProduct>maxProduct) {
                    a = arr[i];
                    b = arr[j];
                    maxProduct = currentProduct;
                }
            }
            Console.WriteLine("The max product is {0}, the two nums are {1} and {2}.",maxProduct,a,b);
            return maxProduct;
        }

Solution 12:[12]

function solution(inputArray) {
    let f, s, arr = []
    for(let i=0; i<inputArray.length; i++){
        f = inputArray[i]
        s = inputArray[i+1]
        arr.push(f*s)
    }
    let max = arr.sort((a, b) => b - a)
    return max[0]
}

console.log(solution([3, 6, -2, -5, 7, 3]))

Solution 13:[13]

This should help, wrote it in python. Concept: Pass an empty list, for every consecutive product keep storing it in the list. Then just return the max value.

def consecutive_product_max(a):
    lst2 = []
    for i in range(0, len(a)-1):
        x = a[i] * a[i+1]
        lst2.append(x)
    return max(lst2)