📌  相关文章
📜  上一个和下一个元素的最大乘积

📅  最后修改于: 2021-04-22 07:30:13             🧑  作者: Mango

给定一个整数数组,任务将在该数组中打印最大乘积,以使其上一个和下一个元素乘积最大。
注意:可以按循环顺序考虑数组。第一个元素的前一个元素将等于最后一个元素,而最后一个元素的下一个元素将是第一个元素。

例子:

方法:

  • 想法是首先找到上一个元素和下一个元素。
  • 找到两个元素之后,取积并在其中找到最大乘积。

下面是上述方法的实现:

C++
// C++ program to print maximum product
// such that its previous and next
// element product is maximum.
#include
using namespace std;
    // function to return largest element
    // such that its previous and next
    // element product is maximum.
     int maxProduct(int a[], int n)
    {
   
        int product[n];
   
        int maxA[n]; 
        int maxProd = 0;
        int maxArr = 0;
        for (int i = 0; i < n; i++) {
   
            // product of previous and next
            // element and stored into an 
            // array product[i]
            product[i] = a[(i + 1) % n] *
                          a[(i + (n - 1)) % n];
   
            // find maximum product  
            // in product[i] array
            if (maxProd < product[i]) {
                maxProd = product[i];
            }
        }
        return maxProd;
    }
   
    // Driver program
   int main()
    {
        int a[] = { 5, 6, 4, 3, 2 };
   
        int n = sizeof(a)/sizeof(a[0]);
   
        cout<<(maxProduct(a, n));
    }
      
// This code contributed by Rajput-Ji


Java
// Java program to print maximum product
// such that its previous and next
// element product is maximum.
import java.io.*;
  
class GFG
{
    // function to return largest element
    // such that its previous and next
    // element product is maximum.
    static int maxProduct(int a[], int n)
    {
  
        int[] product = new int[n];
  
        int maxA[] = new int[n];
  
        int maxProd = 0;
        int maxArr = 0;
        for (int i = 0; i < n; i++) 
        {
  
            // product of previous and next
            // element and stored into an 
            // array product[i]
            product[i] = a[(i + 1) % n] *
                        a[(i + (n - 1)) % n];
  
            // find maximum product 
            // in product[i] array
            if (maxProd < product[i]) 
            {
                maxProd = product[i];
            }
        }
        return maxProd;
    }
  
    // Driver code
    public static void main(String[] args)
  
    {
        int[] a = { 5, 6, 4, 3, 2 };
  
        int n = a.length;
  
        System.out.println(maxProduct(a, n));
    }
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program to print maximum product 
# such that its previous and next 
# element product is maximum. 
  
# function to return largest element 
# such that its previous and next 
# element product is maximum. 
def maxProduct(a, n) : 
  
    product = [0]*n; 
    maxA = [0]*n; 
    maxProd = 0; 
    maxArr = 0; 
  
    for i in range(n) :
          
        # product of previous and next 
        # element and stored into an 
        # array product[i] 
        product[i] = a[(i + 1) % n] * a[(i + (n - 1)) % n]; 
      
        # find maximum product 
        # in product[i] array 
        if (maxProd < product[i]) :
            maxProd = product[i]; 
              
    return maxProd; 
  
  
# Driver code
if __name__ == "__main__" : 
      
    a = [ 5, 6, 4, 3, 2 ]; 
    n = len(a);
      
    print(maxProduct(a, n)); 
      
# This code is contributed by AnkitRai01


C#
// C# program to print maximum product
// such that its previous and next
// element product is maximum.
using System;
  
class GFG
{
    // function to return largest element
    // such that its previous and next
    // element product is maximum.
    static int maxProduct(int []a, int n)
    {
  
        int[] product = new int[n];
  
        //int []maxA = new int[n];
  
         int maxProd = 0;
        //int maxArr = 0;
        for (int i = 0; i < n; i++) 
        {
  
            // product of previous and next
            // element and stored into an 
            // array product[i]
            product[i] = a[(i + 1) % n] *
                        a[(i + (n - 1)) % n];
  
            // find maximum product 
            // in product[i] array
            if (maxProd < product[i]) 
            {
                maxProd = product[i];
            }
        }
        return maxProd;
    }
  
    // Driver code
    public static void Main()
  
    {
        int[] a = { 5, 6, 4, 3, 2 };
  
        int n = a.Length;
  
        Console.WriteLine(maxProduct(a, n));
    }
}
  
// This code is contributed by anuj_67..


输出:
20

时间复杂度: O(N)