📌  相关文章
📜  通过一一减少元素来最大化Array的最小元素

📅  最后修改于: 2022-05-13 01:56:09.490000             🧑  作者: Mango

通过一一减少元素来最大化Array的最小元素

给定一个包含N个整数的数组arr[] 。在每个操作中,从数组中选择一个最小整数,并在从剩余元素中减去它后从数组中删除。任务是在任意数量的此类操作之后找到数组的最小值的最大值。

例子:

朴素方法:从数组中删除最小元素并从剩余元素中进行减法,并在每次操作中跟踪数组的最小值的最大值,而数组的大小不等于0

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

高效方法:上述方法可以通过使用贪心方法进行优化。这可以从数学上推导出来,因为每次都需要删除最小元素,因此它与数组中元素的顺序无关。所以需要对数组进行排序。请遵循以下观察:

从上述证明中可以看出,最终结果将是连续元素的差异。因为每个步骤中的最小值是相邻元素的差异。

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

  • 将初始答案初始化为max_value ,其中0次操作为arr[0]
  • 按升序对数组arr[]进行排序。
  • [0, N-1]范围内迭代
  • 在每次迭代中跟踪最小值的最大值(即差arr[i + 1] – arr[i])
  • 返回最大值

下面是上述方法的实现:

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to find maximum of minimum value
// of the array in the array
// in each operation
int min_after_del(int arr[], int n)
{
    // If no operations are done
    int max_value = arr[0];
 
    // Sort the array arr in ascending order
    sort(arr, arr + n);
 
    // Traverse the array to check
    // the required condition
    for (int i = 0; i < n - 1; i++) {
        max_value = max(max_value,
                        arr[i + 1] - arr[i]);
    }
 
    return max_value;
}
 
// Driver code
int main()
{
 
    // Initializing array of arr
    int arr[] = { -1, -2, 4, 3, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Call the function and
    // print the answer
    cout << (min_after_del(arr, N));
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
class GFG{
 
// Function to find maximum of minimum value
// of the array in the array
// in each operation
static int min_after_del(int arr[], int n)
{
    // If no operations are done
    int max_value = arr[0];
 
    // Sort the array arr in ascending order
    Arrays.sort(arr);
 
    // Traverse the array to check
    // the required condition
    for (int i = 0; i < n - 1; i++) {
        max_value = Math.max(max_value,
                        arr[i + 1] - arr[i]);
    }
 
    return max_value;
}
 
// Driver code
public static void main(String[] args)
{
 
    // Initializing array of arr
    int arr[] = { -1, -2, 4, 3, 5 };
    int N = arr.length;
 
    // Call the function and
    // print the answer
    System.out.print(min_after_del(arr, N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python code for the above approach
 
# Function to find maximum of minimum value
# of the array in the array
# in each operation
def min_after_del (arr, n):
 
    # If no operations are done
    max_value = arr[0];
 
    # Sort the array arr in ascending order
    arr.sort();
     
    # Traverse the array to check
    # the required condition
    for i in range(n - 1):
        max_value = max(max_value, arr[i + 1] - arr[i]);
 
    return max_value;
 
# Driver code
# Initializing array of arr
arr = [-1, -2, 4, 3, 5];
N = len(arr)
 
# Call the function and
# print the answer
print(min_after_del(arr, N));
 
# This code is contributed by Saurabh Jaiswal


C#
using System;
 
public class GFG{
 
  // Function to find maximum of minimum value
  // of the array in the array
  // in each operation
  static int min_after_del(int[] arr, int n)
  {
    // If no operations are done
    int max_value = arr[0];
 
    // Sort the array arr in ascending order
    Array.Sort(arr);
 
    // Traverse the array to check
    // the required condition
    for (int i = 0; i < n - 1; i++) {
      max_value = Math.Max(max_value,
                           arr[i + 1] - arr[i]);
    }
 
    return max_value;
  }
 
  // Driver code
  static public void Main (){
 
    int[] arr = { -1, -2, 4, 3, 5 };
    int N = arr.Length;
 
    // Call the function and
    // print the answer
    Console.Write(min_after_del(arr, N));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript



输出
4

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