📌  相关文章
📜  通过精确地去除K来最大和最小数组元素之间的差异最小化

📅  最后修改于: 2021-04-21 23:45:50             🧑  作者: Mango

给定一个由n个正整数和一个整数K组成的数组arr [] ,任务是在精确删除K个元素后,使给定数组中最大和最小元素之间的差异最小。

例子:

方法:解决给定问题的想法是,通过删除数组中的最小元素或数组中的最大元素,将差异最小化。请按照以下步骤解决问题:

  • 以升序对数组arr []进行排序。
  • 初始化变量left = 0right =(N – 1)
  • 迭代K次,然后根据以下条件将最大值或最小值更改为0:
    • 如果arr [right – 1] – arr [left] < arr [right] – arr [left + 1] ,则将arr [right]更改为0,并将右指针1
    • 否则,变化ARR [左]0,递增1左指针。
  • 在上述步骤之后,在索引的元素之间的差异是所要求的最小差。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to minimize the difference
// of the maximum and minimum array
// elements by removing K elements
void minimumRange(int arr[], int N, int K)
{
    // Base Condition
    if (K >= N)
    {
        cout << 0;
        return;
    }
 
    // Sort the array
    sort(arr, arr + N);
 
    // Initialize left and right pointers
    int left = 0, right = N - 1, i;
 
    // Iterate for K times
    for (i = 0; i < K; i++)
    {
 
        // Removing right element
        // to reduce the difference
        if (arr[right - 1] - arr[left]
            < arr[right] - arr[left + 1])
            right--;
 
        // Removing the left element
        // to reduce the difference
        else
            left++;
    }
 
    // Print the minimum difference
    cout << arr[right] - arr[left];
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 10, 12, 14, 21, 54, 61 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    int K = 4;
 
    // Function Call
    minimumRange(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to minimize the difference
// of the maximum and minimum array
// elements by removing K elements
static void minimumRange(int arr[], int N,
                         int K)
{
     
    // Base Condition
    if (K >= N)
    {
        System.out.print(0);
        return;
    }
     
    // Sort the array
    Arrays.sort(arr);
     
    // Initialize left and right pointers
    int left = 0, right = N - 1, i;
 
    // Iterate for K times
    for(i = 0; i < K; i++)
    {
         
        // Removing right element
        // to reduce the difference
        if (arr[right - 1] - arr[left] <
            arr[right] - arr[left + 1])
            right--;
             
        // Removing the left element
        // to reduce the difference
        else
            left++;
    }
     
    // Print the minimum difference
    System.out.print(arr[right] - arr[left]);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 5, 10, 12, 14, 21, 54, 61 };
    int N = arr.length;
    int K = 4;
 
    // Function Call
    minimumRange(arr, N, K);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to minimize the difference
# of the maximum and minimum array
# elements by removing K elements
def minimumRange(arr, N, K) :
 
    # Base Condition
    if (K >= N) :
        print(0, end = '');
        return;
 
    # Sort the array
    arr.sort();
 
    # Initialize left and right pointers
    left = 0; right = N - 1;
 
    # Iterate for K times
    for i in range(K) :
 
        # Removing right element
        # to reduce the difference
        if (arr[right - 1] - arr[left] < arr[right] - arr[left + 1]) :
            right -= 1;
 
        # Removing the left element
        # to reduce the difference
        else :
            left += 1;
 
    # Print the minimum difference
    print(arr[right] - arr[left], end = '');
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 5, 10, 12, 14, 21, 54, 61 ];
    N = len(arr);
 
    K = 4;
 
    # Function Call
    minimumRange(arr, N, K);
 
    # This code is contributed by AnkitRai01


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to minimize the difference
// of the maximum and minimum array
// elements by removing K elements
static void minimumRange(int []arr, int N,
                         int K)
{
     
    // Base Condition
    if (K >= N)
    {
        Console.Write(0);
        return;
    }
     
    // Sort the array
    Array.Sort(arr);
     
    // Initialize left and right pointers
    int left = 0, right = N - 1, i;
 
    // Iterate for K times
    for(i = 0; i < K; i++)
    {
         
        // Removing right element
        // to reduce the difference
        if (arr[right - 1] - arr[left] <
            arr[right] - arr[left + 1])
            right--;
             
        // Removing the left element
        // to reduce the difference
        else
            left++;
    }
     
    // Print the minimum difference
    Console.Write(arr[right] - arr[left]);
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 5, 10, 12, 14, 21, 54, 61 };
    int N = arr.Length;
    int K = 4;
 
    // Function Call
    minimumRange(arr, N, K);
}
}
 
// This code is contributed by 29AjayKumar


输出:
4

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