📌  相关文章
📜  最大化乘积之和以及任何可能的一对数组元素之间的差异

📅  最后修改于: 2021-04-17 14:06:01             🧑  作者: Mango

给定的[]大小为N的阵列ARR,任务是找到ARR的最大值[I] * ARR [J] + ARR [Ⅰ] – ARR [j]的任何一对(ARR [I],编曲[J ])从给定的数组,其中i!= j0

例子:

天真的方法:解决问题的最简单方法是遍历数组并从数组中生成所有可能的对(arr [i],arr [j]) ( i!= j ),并评估所有对的表达式。最后,打印所有对中的最大值。
时间复杂度: O(N 2 )
辅助空间: O(1)

高效的方法:最佳的想法是基于以下两种情况下表达式的值可以达到最大值的观察:

  • 该对是否包含最大和第二大的数组元素。
  • 如果该对包含最小和第二小的数组元素,则选择。当最小和第二最小元素均为负数且它们的乘积将得出正数时,可能会出现这种情况。

请按照以下步骤解决问题:

  • 以升序对数组arr []进行排序。
  • 计算对arr [N – 1]arr [N – 2]的表达式,并将其存储在变量max1中
  • 同样,对arr [1]arr [0]对的表达式求值,并将其存储在变量中,例如max2
  • max1max2的最大值存储在变量ans中
  • 打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to evaluate given expression
int compute(int a, int b)
{
    // Store the result
    int ans = a * b + a - b;
    return ans;
}
 
// Function to find the maximum value of
// the given expression possible for any
// unique pair from the given array
void findMaxValue(int arr[], int N)
{
    // Sort the array in ascending order
    sort(arr, arr + N);
 
    // Evaluate the expression for
    // the two largest elements
    int maxm = compute(arr[N - 1], arr[N - 2]);
 
    // Evaluate the expression for
    // the two smallest elements
    maxm = max(maxm, compute(arr[1], arr[0]));
 
    // Print the maximum
    cout << maxm;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { -4, -5, 0, 1, 3 };
 
    // Store the size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    findMaxValue(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to evaluate given expression
  static int compute(int a, int b)
  {
    // Store the result
    int ans = a * b + a - b;
    return ans;
  }
 
  // Function to find the maximum value of
  // the given expression possible for any
  // unique pair from the given array
  static void findMaxValue(int arr[], int N)
  {
    // Sort the array in ascending order
    Arrays.sort(arr);
 
    // Evaluate the expression for
    // the two largest elements
    int maxm = compute(arr[N - 1], arr[N - 2]);
 
    // Evaluate the expression for
    // the two smallest elements
    maxm = Math.max(maxm, compute(arr[1], arr[0]));
 
    // Print the maximum
    System.out.print(maxm);
  }
 
 
  // Driver Code
  public static void main(String[] args)
  {
    // Given array
    int arr[] = { -4, -5, 0, 1, 3 };
 
    // Store the size of the array
    int N = arr.length;
 
    findMaxValue(arr, N);
  }
}
 
// This code is contributed by saanjoy_62


Python3
# Python Program for the above approach
# Function to evaluate given expression
def compute(a, b):
   
    # Store the result
    res = (a * b) + (a - b)
    return res
   
# Function to find the maximum value of
# the given expression possible for any
# unique pair from the given array
def findMaxValue(arr, N):
   
    # Sort the list in ascending order
    arr.sort()
     
    # Evaluate the expression for
    # the two largest elements
    maxm = compute(arr[N - 1], arr[N - 2])
     
    # Evaluate the expression for
    # the two smallest elements
    maxm = max(maxm, compute(arr[1], arr[0]));
    print(maxm)
     
# Driver code
# given list
arr = [-4, -5, 0, 1, 3]
 
# store the size of the list
N = len(arr)
findMaxValue(arr, N)
 
# This code is contributed by santhoshcharan.


C#
// C# program for above approach
using System;
public class GFG
{
 
  // Function to evaluate given expression
  static int compute(int a, int b)
  {
    // Store the result
    int ans = a * b + a - b;
    return ans;
  }
  
  // Function to find the maximum value of
  // the given expression possible for any
  // unique pair from the given array
  static void findMaxValue(int[] arr, int N)
  {
    // Sort the array in ascending order
    Array.Sort(arr);
  
    // Evaluate the expression for
    // the two largest elements
    int maxm = compute(arr[N - 1], arr[N - 2]);
  
    // Evaluate the expression for
    // the two smallest elements
    maxm = Math.Max(maxm, compute(arr[1], arr[0]));
  
    // Print the maximum
    Console.WriteLine(maxm);
  }
 
// Driver code
public static void Main(String[] args)
{
 
    // Given array
    int[] arr = { -4, -5, 0, 1, 3 };
  
    // Store the size of the array
    int N = arr.Length;
  
    findMaxValue(arr, N);
}
}
 
// This code is contributed by susmitakundugoaldanga.


输出
21

时间复杂度: O(N * log(N))
辅助空间: O(1)