📌  相关文章
📜  (arr[i] * arr[j]) + (arr[j] – arr[i])) 对于数组中的任何对可能的最大值

📅  最后修改于: 2021-09-07 02:30:56             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是为任何对找到表达式(arr[i] * arr[j]) + (arr[j] – arr[i]))的最大可能值(i, j) ,使得i ≠ j0 ≤ (i, j) < N

例子:

朴素方法:解决给定问题的最简单方法是生成数组的所有可能对并找到给定表达式的值(arr[i] * arr[j]) + (arr[j] – arr[i] ))为每一对并打印为任何对获得的最大值。

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

高效的方法:上述方法也可以优化,这是基于观察将通过选择数组的最大值和第二个最大值或最小值和第二个最小值的对来获得表达式的最大值。请按照以下步骤解决问题:

  • 初始化一个变量,比如ans ,以存储结果最大值。
  • 按升序对数组进行排序。
  • 发现该阵列的最大和第二最大元素,说XY分别与更新ANS的最大ANS和表达的使用值XY的值的值。
  • 求出数组的最小值和第二最小元素,说XY分别与更新ANS的最大ANS和表达的使用值XY的值的值。
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the value of
// the expression a * b + (b - a)
int calc(int a, int b)
{
    return a * b + (b - a);
}
 
// Function to find the maximum value
// of the expression a * b + (b - a)
// possible for any pair (a, b)
int findMaximum(vector arr, int N)
{
    // Sort the vector in ascending order
    sort(arr.begin(), arr.end());
 
    // Stores the maximum value
    int ans = -1e9;
 
    // Update ans by choosing the pair
    // of the minimum and 2nd minimum
    ans = max(ans, calc(arr[0], arr[1]));
 
    // Update ans by choosing the pair
    // of maximum and 2nd maximum
    ans = max(ans, calc(arr[N - 2],
                        arr[N - 1]));
 
    // Return the value of ans
    return ans;
}
 
// Driver Code
int main()
{
    vector arr = { 0, -47, 12 };
    int N = (int)arr.size();
    cout << findMaximum(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
     
// Function to find the value of
// the expression a * b + (b - a)
static int calc(int a, int b)
{
    return a * b + (b - a);
}
 
// Function to find the maximum value
// of the expression a * b + (b - a)
// possible for any pair (a, b)
static int findMaximum(int[] arr, int N)
{
     
    // Sort the vector in ascending order
    Arrays.sort(arr);
 
    // Stores the maximum value
    int ans = (int)-1e9;
 
    // Update ans by choosing the pair
    // of the minimum and 2nd minimum
    ans = Math.max(ans, calc(arr[0], arr[1]));
 
    // Update ans by choosing the pair
    // of maximum and 2nd maximum
    ans = Math.max(ans, calc(arr[N - 2],
                             arr[N - 1]));
 
    // Return the value of ans
    return ans;
}   
 
// Driver Code
public static void main(String[] args)
{
     
    // Given inputs
    int[] arr = { 0, -47, 12 };
    int N = arr.length;
     
    System.out.println(findMaximum(arr, N));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to find the value of
# the expression a * b + (b - a)
def calc(a, b):
    return a * b + (b - a)
 
# Function to find the maximum value
# of the expression a * b + (b - a)
# possible for any pair (a, b)
def findMaximum(arr, N):
   
    # Sort the vector in ascending order
    arr = sorted(arr)
 
    # Stores the maximum value
    ans = -10**9
 
    # Update ans by choosing the pair
    # of the minimum and 2nd minimum
    ans = max(ans, calc(arr[0], arr[1]))
 
    # Update ans by choosing the pair
    # of maximum and 2nd maximum
    ans = max(ans, calc(arr[N - 2],arr[N - 1]))
 
    # Return the value of ans
    return ans
 
# Driver Code
if __name__ == '__main__':
    arr = [0, -47, 12]
    N = len(arr)
    print (findMaximum(arr, N))
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
   
    // Function to find the value of
    // the expression a * b + (b - a)
    static int calc(int a, int b)
    {
        return a * b + (b - a);
    }
 
    // Function to find the maximum value
    // of the expression a * b + (b - a)
    // possible for any pair (a, b)
    static int findMaximum(List arr, int N)
    {
       
        // Sort the vector in ascending order
        arr.Sort();
 
        // Stores the maximum value
        int ans = -1000000000;
 
        // Update ans by choosing the pair
        // of the minimum and 2nd minimum
        ans = Math.Max(ans, calc(arr[0], arr[1]));
 
        // Update ans by choosing the pair
        // of maximum and 2nd maximum
        ans = Math.Max(ans, calc(arr[N - 2], arr[N - 1]));
 
        // Return the value of ans
        return ans;
    }
 
    // Driver Code
    public static void Main()
    {
        List arr = new List{ 0, -47, 12 };
        int N = (int)arr.Count;
        Console.Write(findMaximum(arr, N));
    }
}
 
// This code is contributed by ukasp..


Javascript


输出:
47

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

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