📌  相关文章
📜  使用给定的操作最大化 Array 的除法结果

📅  最后修改于: 2021-09-06 06:23:18             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是通过重复以下两个步骤来找到数组中可能剩余的最大值:

  • 删除任意两个数组元素。
  • 将它们除法的商插入数组中。

注意:我们可以改变元素的顺序。

例子:

方法:
为了解决上述问题,我们可以观察到,我们将获得最大结果,当元件以递减顺序和分割操作进行排序发生在序列ARR [0] /(ARR [1] / ARR [2] / ARR [3]…..arr[n-1])的反向排序数组。因此,相应地对数组进行排序并计算适当的结果。

下面的代码是上述方法的实现:

C++
// C++ implementation to maximize the
// result of division of the given
// array elements
#include 
using namespace std;
 
// Function to find the max result
float maxDivision(int arr[], int n)
{
 
    // Sort the array in descending order
    sort(arr, arr + n, greater());
 
    float mxdiv = arr[1];
 
    // loop to divide in this order
    // arr[0] / ( arr[1] / arr[2] / ....
    // arr[n-2] / arr[n-1])
    for (int i = 2; i < n; ++i)
        mxdiv = mxdiv / arr[i];
 
    // return the final result
    return arr[0] / mxdiv;
}
 
// Driver code
int main()
{
 
    int arr[] = { 100, 1000, 10, 2 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxDivision(arr, n);
 
    return 0;
}


Java
// Java implementation to maximize the
// result of division of the given
// array elements
import java.util.*;
 
class GFG{
 
// Function to find the max result
static float maxDivision(Integer arr[], int n)
{
 
    // Sort the array in descending order
    Arrays.sort(arr, Collections.reverseOrder());
 
    float mxdiv = arr[1];
 
    // Loop to divide in this order
    // arr[0] / ( arr[1] / arr[2] / ....
    // arr[n-2] / arr[n-1])
    for(int i = 2; i < n; ++i)
       mxdiv = mxdiv / arr[i];
 
    // Return the final result
    return arr[0] / mxdiv;
}
 
// Driver code
public static void main(String[] args)
{
 
    Integer arr[] = { 100, 1000, 10, 2 };
    int n = arr.length;
 
    System.out.print((int)maxDivision(arr, n));
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 implementation to maximize
# the result of division of the
# given array elements
 
# Function to find the max result
def maxDivision(arr, n):
     
    # Sort the array in descending order
    arr.sort(reverse = True)
    mxdiv = arr[1]
     
    # Loop to divide in this order
    # arr[0] / ( arr[1] / arr[2] / ....
    # arr[n-2] / arr[n-1])
    for i in range(2, n):
        mxdiv = mxdiv / arr[i]
         
    # Return the final result
    return arr[0] / mxdiv
 
# Driver code
arr = [ 100, 1000, 10, 2 ]
n = len(arr)
 
print(maxDivision(arr, n))
 
# This code is contributed by ishayadav181


C#
// C# implementation to maximize the
// result of division of the given
// array elements
using System;
 
class GFG{
 
// Function to find the max result
static float maxDivision(int []arr, int n)
{
 
    // Sort the array in descending order
    Array.Sort(arr);
    Array.Reverse(arr);
 
    float mxdiv = arr[1];
 
    // Loop to divide in this order
    // arr[0] / ( arr[1] / arr[2] / ....
    // arr[n-2] / arr[n-1])
    for(int i = 2; i < n; ++i)
       mxdiv = mxdiv / arr[i];
 
    // Return the readonly result
    return arr[0] / mxdiv;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 100, 1000, 10, 2 };
    int n = arr.Length;
 
    Console.Write((int)maxDivision(arr, n));
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
200

时间复杂度: O(N logN)

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