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

📅  最后修改于: 2021-09-03 02:58:44             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是找到任意对(arr[i], arr[j] arr[i] ∗ arr[j] + arr[i] − arr[j]的最大值])来自给定的数组,其中i != j0 < i, j < N – 1

例子:

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

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

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

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

  • 按升序对数组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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live