给定N个整数的数组arr [] ,任务是通过重复以下两个步骤来找到数组中可能剩余的最大值:
- 删除任何两个数组元素。
- 将除法商插入数组。
注意:我们可以更改元素的顺序。
例子:
Input: arr[] = {100, 1000, 10, 2}
Output: 200
Explanation:
- Remove 100 and 10 from the array. Insert 10 (= 100/10) back into the array.
- Remove 10 and 2 from the array. Insert 5 into the array.
- Remove 1000 and 5 from the array. Insert 200 into the array
Hence, the maximum result is 200.
Input: arr[] = {2, 100, 1}
Output: 50
方法:
为了解决上述问题,我们可以观察到,当元素以降序排序并且除法操作以序列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
输出:
200
时间复杂度: O(N logN)