📌  相关文章
📜  最小化要删除的数组元素的数量,以最大化任何对之间的差异,最大为 K

📅  最后修改于: 2021-09-06 06:20:52             🧑  作者: Mango

给定一个数组arr[]和一个整数K ,任务是计算要从数组中删除的元素的数量,使得剩下的最大和最小数量之差不超过 K。

例子:

方法:
任务是找到不应超过K最大最小数组元素之间的差异。

  • 按升序对数组进行排序并将变量初始化为最小值。
  • 迭代数组以生成所有可能的对并检查任何对之间的差异是否小于或等于K
  • 更新每对的最小移除次数。
  • 最后,打印获得的最小去除量。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to count the number of
// elements to be removed from the
// array based on the given condition
int min_remove(int arr[], int n, int k)
{
    // Sort the array
    sort(arr, arr + n);
 
    /// Initialize the variable
    int ans = INT_MAX;
 
    // Iterate for all possible pairs
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
 
            // Check the difference
            // between the numbers
            if (arr[j] - arr[i] <= k) {
 
                // Update the minimum removals
                ans = min(ans, n - j + i - 1);
            }
        }
    }
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    int k = 3;
    int arr[] = { 1, 2, 3, 4, 5 };
 
    int n = sizeof arr / sizeof arr[0];
 
    cout << min_remove(arr, n, k);
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to count the number of
// elements to be removed from the
// array based on the given condition
static int min_remove(int arr[], int n, int k)
{
    // Sort the array
    Arrays.sort(arr);
 
    /// Initialize the variable
    int ans = Integer.MAX_VALUE;
 
    // Iterate for all possible pairs
    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)
        {
 
            // Check the difference
            // between the numbers
            if (arr[j] - arr[i] <= k)
            {
 
                // Update the minimum removals
                ans = Math.min(ans, n - j + i - 1);
            }
        }
    }
    // Return the answer
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int k = 3;
    int arr[] = { 1, 2, 3, 4, 5 };
 
    int n = arr.length;
 
    System.out.print(min_remove(arr, n, k));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program to implement
# the above approach
import sys
 
# Function to count the number of
# elements to be removed from the
# array based on the given condition
def min_remove(arr, n, k):
 
    # Sort the array
    arr.sort()
 
    # Initialize the variable
    ans = sys.maxsize
 
    # Iterate for all possible pairs
    for i in range(n):
        for j in range(i, n):
 
            # Check the difference
            # between the numbers
            if (arr[j] - arr[i] <= k):
 
                # Update the minimum removals
                ans = min(ans, n - j + i - 1)
     
    # Return the answer
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    k = 3
    arr = [ 1, 2, 3, 4, 5 ]
 
    n = len(arr)
 
    print (min_remove(arr, n, k))
 
# This code is contributed by chitranayal


C#
// C# Program to implement
// the above approach
using System;
class GFG{
 
// Function to count the number of
// elements to be removed from the
// array based on the given condition
static int min_remove(int []arr, int n, int k)
{
    // Sort the array
    Array.Sort(arr);
 
    /// Initialize the variable
    int ans = int.MaxValue;
 
    // Iterate for all possible pairs
    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)
        {
 
            // Check the difference
            // between the numbers
            if (arr[j] - arr[i] <= k)
            {
 
                // Update the minimum removals
                ans = Math.Min(ans, n - j + i - 1);
            }
        }
    }
    // Return the answer
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int k = 3;
    int []arr = { 1, 2, 3, 4, 5 };
 
    int n = arr.Length;
 
    Console.Write(min_remove(arr, n, k));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
1

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