📌  相关文章
📜  在数组 arr[] 中查找 abs(i – j) * min(arr[i], arr[j]) 的最大值

📅  最后修改于: 2022-05-13 01:57:52.239000             🧑  作者: Mango

在数组 arr[] 中查找 abs(i – j) * min(arr[i], arr[j]) 的最大值

给定一个包含 n 个不同元素的数组。求数组中Minimum of two numbers的乘积的最大值及其位置的绝对差,即求abs(i – j) * min(arr[i], arr[j])的最大值,其中i和j不同从 0 到 n-1。

例子 :

Input : arr[] = {3, 2, 1, 4}
Output: 9
// arr[0] = 3 and arr[3] = 4 minimum of them is 3 and 
// absolute difference between their position is 
// abs(0-3) = 3. So product is 3*3 = 9

Input : arr[] = {8, 1, 9, 4}
Output: 16
// arr[0] = 8 and arr[2] = 9 minimum of them is 8 and 
// absolute difference between their position is 
// abs(0-2) = 2. So product is 8*2 = 16 

这个问题的一个简单解决方案是一个一个地获取每个元素,并将这个元素与它右边的元素进行比较。然后计算它们的最小值和它们的索引之间的绝对差的乘积并最大化结果。这种方法的时间复杂度是 O(n^2)。

解决线性时间复杂度问题的有效解决方案。我们采用两个迭代器Left=0Right=n-1 ,比较元素 arr[Left] 和 arr[right]。

left = 0, right = n-1
maxProduct = -INF
While (left < right)
    If arr[Left] < arr[right] 
        currProduct = arr[Left]*(right-Left) 
        Left++ . 
    If arr[right] < arr[Left] 
        currProduct = arr[Right]*(Right-Left) 
        Right-- . 
  
    maxProduct = max(maxProduct, currProduct)

下面是上述想法的实现。

C++
// C++ implementation of code
#include
using namespace std;
 
// Function to calculate maximum value of
// abs(i - j) * min(arr[i], arr[j]) in arr[]
int Maximum_Product(int arr[], int n)
{
    int maxProduct = INT_MIN; // Initialize result
    int currProduct; // product of current pair
 
    // loop  until they meet with each other
    int Left = 0, right = n-1;
    while (Left < right)
    {
        if (arr[Left] < arr[right])
        {
            currProduct = arr[Left]*(right-Left);
            Left++;
        }
        else // arr[right] is smaller
        {
            currProduct = arr[right]*(right-Left);
            right--;
        }
 
        // maximizing the product
        maxProduct = max(maxProduct, currProduct)
    }
 
    return maxProduct;
}
 
// Driver program to test the case
int main()
{
    int arr[] = {8, 1, 9, 4};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << Maximum_Product(arr,n);
    return 0;
}


Java
// Java implementation of code
import java.util.*;
 
class GFG {
     
    // Function to calculate maximum value of
    // abs(i - j) * min(arr[i], arr[j]) in arr[]
    static int Maximum_Product(int arr[], int n) {
         
    // Initialize result
    int maxProduct = Integer.MIN_VALUE;
     
    // product of current pair
    int currProduct;           
 
    // loop until they meet with each other
    int Left = 0, right = n - 1;
    while (Left < right) {
    if (arr[Left] < arr[right]) {
        currProduct = arr[Left] * (right - Left);
        Left++;
    }
     
    // arr[right] is smaller
    else
    {
        currProduct = arr[right] * (right - Left);
        right--;
    }
 
    // maximizing the product
    maxProduct = Math.max(maxProduct, currProduct);
    }
 
    return maxProduct;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = {8, 1, 9, 4};
    int n = arr.length;
    System.out.print(Maximum_Product(arr, n));
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python implementation of code
# Function to calculate
# maximum value of
# abs(i - j) * min(arr[i],
# arr[j]) in arr[]
def Maximum_Product(arr,n):
     
    # Initialize result
    maxProduct = -2147483648
 
    # product of current pair
    currProduct=0
  
    # loop until they meet with each other
    Left = 0
    right = n-1
    while (Left < right):
     
        if (arr[Left] < arr[right]):
         
            currProduct = arr[Left]*(right-Left)
            Left+=1
         
        else:
 
            # arr[right] is smaller
            currProduct = arr[right]*(right-Left)
            right-=1
         
  
        # maximizing the product
        maxProduct = max(maxProduct, currProduct)
     
  
    return maxProduct
 
# Driver code
 
arr = [8, 1, 9, 4]
n = len(arr)
 
print(Maximum_Product(arr,n))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# implementation of code
using System;
 
class GFG {
     
// Function to calculate maximum
// value of abs(i - j) * min(arr[i],
// arr[j]) in arr[]
static int Maximum_Product(int []arr,
                           int n)
{
         
    // Initialize result
    int maxProduct = int.MinValue;
     
    // product of current pair
    int currProduct;        
 
    // loop until they meet
    // with each other
    int Left = 0, right = n - 1;
    while (Left < right) {
    if (arr[Left] < arr[right])
    {
        currProduct = arr[Left] *
                      (right - Left);
        Left++;
    }
     
    // arr[right] is smaller
    else
    {
        currProduct = arr[right] *
                      (right - Left);
        right--;
    }
 
    // maximizing the product
    maxProduct = Math.Max(maxProduct,
                          currProduct);
    }
 
    return maxProduct;
}
 
// Driver code
public static void Main()
{
    int []arr = {8, 1, 9, 4};
    int n = arr.Length;
    Console.Write(Maximum_Product(arr, n));
}
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


输出 :

16

这是如何运作的?
重要的是要表明我们不会错过上述线性算法中的任何潜在对,即,我们需要证明做 left++ 或 right- 不会导致我们得到 maxProduct 更高值的情况。
请注意,我们总是与(右-左)相乘。

1) 如果 arr[left] < arr[right],那么当前 left 的较小的right值是没有用的,因为它们不能产生更高的 maxProduct 值(因为我们将 arr[left] 与 (right – left) 相乘)。如果 arr[left] 大于其左侧的任何元素怎么办。在这种情况下,必须在当前权限中找到该元素的更好对。因此,我们可以安全地增加左,而不会错过任何与当前左更好的配对。

2) 当 arr[right] < arr[left] 时,类似的论点适用。