📜  从数组中最小移除以达到 max – min <= K

📅  最后修改于: 2021-09-22 10:38:23             🧑  作者: Mango

给定 N 个整数和 K,找出应该删除的最小元素数,使得 A max -A min <=K。移除元素后,A max和A min被视为剩余元素。

例子:

Input : a[] = {1, 3, 4, 9, 10, 11, 12, 17, 20} 
          k = 4 
Output : 5 
Explanation: Remove 1, 3, 4 from beginning 
and 17, 20 from the end.

Input : a[] = {1, 5, 6, 2, 8}  K=2
Output : 3
Explanation: There are multiple ways to 
remove elements in this case.
One among them is to remove 5, 6, 8.
The other is to remove 1, 2, 5

方法:对给定的元素进行排序。使用贪婪方法,最好的方法是删除最小元素或最大元素,然后检查是否A max -A min <= K 。必须考虑各种移除组合。我们写了一个递归关系来尝试每一种可能的组合。将有两种可能的删除方式,要么删除最小值,要么删除最大值。令(i…j) 为移除元素后剩余元素的索引。最初,我们从 i=0 和 j=n-1 开始,移除的元素数一开始为 0。我们只在 a[j]-a[i]>k 时移除一个元素,两种可能的移除方式是(i+1…j) 或 (i…j-1) 。考虑两者中的最小值。
令 DP i, j为需要移除的元素数,以便移除后 a[j]-a[i]<=k。

排序数组的递归关系:

DPi, j = 1+ (min(countRemovals(i+1, j), countRemovals(i, j-1))

下面是上述想法的实现:

C++
// CPP program to find minimum removals
// to make max-min <= K
#include 
using namespace std;
 
#define MAX 100
int dp[MAX][MAX];
 
// function to check all possible combinations
// of removal and return the minimum one
int countRemovals(int a[], int i, int j, int k)
{
    // base case when all elements are removed
    if (i >= j)
        return 0;
 
    // if condition is satisfied, no more
    // removals are required
    else if ((a[j] - a[i]) <= k)
        return 0;
 
    // if the state has already been visited
    else if (dp[i][j] != -1)
        return dp[i][j];
 
    // when Amax-Amin>d
    else if ((a[j] - a[i]) > k) {
 
        // minimum is taken of the removal
        // of minimum element or removal
        // of the maximum element
        dp[i][j] = 1 + min(countRemovals(a, i + 1, j, k),
                           countRemovals(a, i, j - 1, k));
    }
    return dp[i][j];
}
 
// To sort the array and return the answer
int removals(int a[], int n, int k)
{
    // sort the array
    sort(a, a + n);
 
    // fill all stated with -1
    // when only one element
    memset(dp, -1, sizeof(dp));
    if (n == 1)
        return 0;
    else
        return countRemovals(a, 0, n - 1, k);
}
 
// Driver Code
int main()
{
    int a[] = { 1, 3, 4, 9, 10, 11, 12, 17, 20 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 4;
    cout << removals(a, n, k);
    return 0;
}


Java
// Java program to find minimum removals
// to make max-min <= K
import java.util.Arrays;
 
class GFG
{
    static int MAX=100;
    static int dp[][]=new int[MAX][MAX];
     
    // function to check all possible combinations
    // of removal and return the minimum one
    static int countRemovals(int a[], int i, int j, int k)
    {
        // base case when all elements are removed
        if (i >= j)
            return 0;
     
        // if condition is satisfied, no more
        // removals are required
        else if ((a[j] - a[i]) <= k)
            return 0;
     
        // if the state has already been visited
        else if (dp[i][j] != -1)
            return dp[i][j];
     
        // when Amax-Amin>d
        else if ((a[j] - a[i]) > k) {
     
            // minimum is taken of the removal
            // of minimum element or removal
            // of the maximum element
            dp[i][j] = 1 + Math.min(countRemovals(a, i + 1, j, k),
                                    countRemovals(a, i, j - 1, k));
        }
        return dp[i][j];
    }
     
    // To sort the array and return the answer
    static int removals(int a[], int n, int k)
    {
        // sort the array
        Arrays.sort(a);
     
        // fill all stated with -1
        // when only one element
        for(int[] rows:dp)
        Arrays.fill(rows,-1);
        if (n == 1)
            return 0;
        else
            return countRemovals(a, 0, n - 1, k);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int a[] = { 1, 3, 4, 9, 10, 11, 12, 17, 20 };
        int n = a.length;
        int k = 4;
        System.out.print(removals(a, n, k));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to find
# minimum removals to
# make max-min <= K
MAX = 100
dp = [[0 for i in range(MAX)]
         for i in range(MAX)]
for i in range(0, MAX) :
    for j in range(0, MAX) :
        dp[i][j] = -1
 
# function to check all
# possible combinations
# of removal and return
# the minimum one
def countRemovals(a, i, j, k) :
 
    global dp
     
    # base case when all
    # elements are removed
    if (i >= j) :
        return 0
 
    # if condition is satisfied,
    # no more removals are required
    elif ((a[j] - a[i]) <= k) :
        return 0
 
    # if the state has
    # already been visited
    elif (dp[i][j] != -1) :
        return dp[i][j]
 
    # when Amax-Amin>d
    elif ((a[j] - a[i]) > k) :
 
        # minimum is taken of
        # the removal of minimum
        # element or removal of
        # the maximum element
        dp[i][j] = 1 + min(countRemovals(a, i + 1,
                                         j, k),
                           countRemovals(a, i,
                                         j - 1, k))
    return dp[i][j]
 
# To sort the array
# and return the answer
def removals(a, n, k) :
 
    # sort the array
    a.sort()
 
    # fill all stated
    # with -1 when only
    # one element
    if (n == 1) :
        return 0
    else :
        return countRemovals(a, 0,
                             n - 1, k)
 
# Driver Code
a = [1, 3, 4, 9, 10,
     11, 12, 17, 20]
n = len(a)
k = 4
print (removals(a, n, k))
 
# This code is contributed by
# Manish Shaw(manishshaw1)


C#
// C# program to find minimum
// removals to make max-min <= K
using System;
 
class GFG
{
    static int MAX = 100;
    static int [,]dp = new int[MAX, MAX];
     
    // function to check all
    // possible combinations
    // of removal and return
    // the minimum one
    static int countRemovals(int []a, int i,
                             int j, int k)
    {
        // base case when all
        // elements are removed
        if (i >= j)
            return 0;
     
        // if condition is satisfied,
        // no more removals are required
        else if ((a[j] - a[i]) <= k)
            return 0;
     
        // if the state has
        // already been visited
        else if (dp[i, j] != -1)
            return dp[i, j];
     
        // when Amax-Amin>d
        else if ((a[j] - a[i]) > k)
        {
     
            // minimum is taken of the
            // removal of minimum element
            // or removal of the maximum
            // element
            dp[i, j] = 1 + Math.Min(countRemovals(a, i + 1,
                                                  j, k),
                                    countRemovals(a, i,
                                                  j - 1, k));
        }
        return dp[i, j];
    }
     
    // To sort the array and
    // return the answer
    static int removals(int []a,
                        int n, int k)
    {
        // sort the array
        Array.Sort(a);
     
        // fill all stated with -1
        // when only one element
        for(int i = 0; i < MAX; i++)
        {
            for(int j = 0; j < MAX; j++)
                dp[i, j] = -1;
        }
        if (n == 1)
            return 0;
        else
            return countRemovals(a, 0,
                                 n - 1, k);
    }
     
    // Driver code
    static void Main()
    {
        int []a = new int[]{ 1, 3, 4, 9, 10,
                             11, 12, 17, 20 };
        int n = a.Length;
        int k = 4;
        Console.Write(removals(a, n, k));
    }
}
 
// This code is contributed by
// ManishShaw(manishshaw1)


PHP
= $j)
        return 0;
 
    // if condition is satisfied,
    // no more removals are required
    else if (($a[$j] - $a[$i]) <= $k)
        return 0;
 
    // if the state has
    // already been visited
    else if ($dp[$i][$j] != -1)
        return $dp[$i][$j];
 
    // when Amax-Amin>d
    else if (($a[$j] - $a[$i]) > $k)
    {
 
        // minimum is taken of
        // the removal of minimum
        // element or removal of
        // the maximum element
        $dp[$i][$j] = 1 + min(countRemovals($a, $i + 1,
                                                $j, $k),
                              countRemovals($a, $i,
                                            $j - 1, $k));
    }
    return $dp[$i][$j];
}
 
// To sort the array
// and return the answer
function removals($a, $n, $k)
{
    // sort the array
    sort($a);
 
    // fill all stated with -1
    // when only one element
    if ($n == 1)
        return 0;
    else
        return countRemovals($a, 0,
                             $n - 1, $k);
}
 
// Driver Code
$a = array(1, 3, 4, 9, 10,
           11, 12, 17, 20);
$n = count($a);
$k = 4;
echo (removals($a, $n, $k));
 
// This code is contributed by
// Manish Shaw(manishshaw1)
?>


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find
// rightmost index
// which satisfies the condition
// arr[j] - arr[i] <= k
int findInd(int key, int i,
            int n, int k, int arr[])
{
    int start, end, mid, ind = -1;
     
    // Initialising start to i + 1
    start = i + 1;
     
    // Initialising end to n - 1
    end = n - 1;
     
    // Binary search implementation
    // to find the rightmost element
    // that satisfy the condition
    while (start < end)
    {
        mid = start + (end - start) / 2;
         
        // Check if the condition satisfies
        if (arr[mid] - key <= k)
        {  
             
            // Store the position
            ind = mid;
             
            // Make start = mid + 1
            start = mid + 1;
        }
        else
        {
            // Make end = mid
            end = mid;
        }
    }
     
    // Return the rightmost position
    return ind;
}
 
// Function to calculate
// minimum number of elements
// to be removed
int removals(int arr[], int n, int k)
{
    int i, j, ans = n - 1;
     
    // Sort the given array
    sort(arr, arr + n);
     
    // Iterate from 0 to n-1
    for (i = 0; i < n; i++)
    {
         
        // Find j such that
        // arr[j] - arr[i] <= k
        j = findInd(arr[i], i, n, k, arr);
         
        // If there exist such j
        // that satisfies the condition
        if (j != -1)
        {
            // Number of elements
            // to be removed for this
            // particular case is
            // (j - i + 1)
            ans = min(ans, n - (j - i + 1));
        }
    }
     
    // Return answer
    return ans;
}
 
// Driver Code
int main()
{
    int a[] = {1, 3, 4, 9, 10,
               11, 12, 17, 20};
    int n = sizeof(a) / sizeof(a[0]);
    int k = 4;
    cout << removals(a, n, k);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find rightmost index
// which satisfies the condition
// arr[j] - arr[i] <= k
static int findInd(int key, int i,
                   int n, int k, int arr[])
{
    int start, end, mid, ind = -1;
     
    // Initialising start to i + 1
    start = i + 1;
     
    // Initialising end to n - 1
    end = n - 1;
     
    // Binary search implementation
    // to find the rightmost element
    // that satisfy the condition
    while (start < end)
    {
        mid = start + (end - start) / 2;
         
        // Check if the condition satisfies
        if (arr[mid] - key <= k)
        {
             
            // Store the position
            ind = mid;
             
            // Make start = mid + 1
            start = mid + 1;
        }
        else
        {
             
            // Make end = mid
            end = mid;
        }
    }
     
    // Return the rightmost position
    return ind;
}
 
// Function to calculate
// minimum number of elements
// to be removed
static int removals(int arr[], int n, int k)
{
    int i, j, ans = n - 1;
     
    // Sort the given array
    Arrays.sort(arr);
     
    // Iterate from 0 to n-1
    for(i = 0; i < n; i++)
    {
         
        // Find j such that
        // arr[j] - arr[i] <= k
        j = findInd(arr[i], i, n, k, arr);
         
        // If there exist such j
        // that satisfies the condition
        if (j != -1)
        {
             
            // Number of elements
            // to be removed for this
            // particular case is
            // (j - i + 1)
            ans = Math.min(ans,
                           n - (j - i + 1));
        }
    }
     
    // Return answer
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
    int a[] = { 1, 3, 4, 9, 10,
                11, 12, 17, 20 };
    int n = a.length;
    int k = 4;
     
    System.out.println(removals(a, n, k));
}
}
 
// This code is contributed by adityapande88


Python3
# Python program for the
# above approach
 
# Function to find
# rightmost index
# which satisfies the condition
# arr[j] - arr[i] <= k
def findInd(key, i, n,
            k, arr):
   
     ind = -1
     
     # Initialising start
     # to i + 1
     start = i + 1
       
     # Initialising end
     # to n - 1
     end = n - 1;
     
     # Binary search implementation
     # to find the rightmost element
     # that satisfy the condition
     while (start < end):
          mid = int(start +
                   (end - start) / 2)
         
          # Check if the condition
          # satisfies
          if (arr[mid] - key <= k):
             
               # Store the position
               ind = mid
                 
               # Make start = mid + 1
               start = mid + 1
          else:
               # Make end = mid
               end = mid
                 
     # Return the rightmost position
     return ind
     
# Function to calculate
# minimum number of elements
# to be removed
def removals(arr, n, k):
   
     ans = n - 1
     
     # Sort the given array
     arr.sort()
     
     # Iterate from 0 to n-1
     for i in range(0, n):
       
          # Find j such that
          # arr[j] - arr[i] <= k
          j = findInd(arr[i], i,
                      n, k, arr)
           
          # If there exist such j
          # that satisfies the condition
          if (j != -1):
             
               # Number of elements
               # to be removed for this
               # particular case is
               # (j - i + 1)
               ans = min(ans, n -
                        (j - i + 1))
               
     # Return answer
     return ans
     
# Driver Code
a = [1, 3, 4, 9,
     10,11, 12, 17, 20]
n = len(a)
k = 4
print(removals(a, n, k))
 
# This code is contributed by Stream_Cipher


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find rightmost index
// which satisfies the condition
// arr[j] - arr[i] <= k
static int findInd(int key, int i,
                   int n, int k, int[] arr)
{
    int start, end, mid, ind = -1;
      
    // Initialising start to i + 1
    start = i + 1;
      
    // Initialising end to n - 1
    end = n - 1;
      
    // Binary search implementation
    // to find the rightmost element
    // that satisfy the condition
    while (start < end)
    {
        mid = start + (end - start) / 2;
          
        // Check if the condition satisfies
        if (arr[mid] - key <= k)
        {
             
            // Store the position
            ind = mid;
              
            // Make start = mid + 1
            start = mid + 1;
        }
        else
        {
             
            // Make end = mid
            end = mid;
        }
    }
     
    // Return the rightmost position
    return ind;
}
  
// Function to calculate minimum
// number of elements to be removed
static int removals(int[] arr, int n, int k)
{
    int i, j, ans = n - 1;
      
    // Sort the given array
    Array.Sort(arr);
      
    // Iterate from 0 to n-1
    for(i = 0; i < n; i++)
    {
          
        // Find j such that
        // arr[j] - arr[i] <= k
        j = findInd(arr[i], i, n, k, arr);
          
        // If there exist such j
        // that satisfies the condition
        if (j != -1)
        {
              
            // Number of elements
            // to be removed for this
            // particular case is
            // (j - i + 1)
            ans = Math.Min(ans,
                           n - (j - i + 1));
        }
    }
      
    // Return answer
    return ans;
}
 
// Driver code
static void Main()
{
    int[] a = { 1, 3, 4, 9, 10,
                11, 12, 17, 20 };
    int n = a.Length;
    int k = 4;
      
    Console.Write(removals(a, n, k));
}
}
 
// This code is contributed by sanjoy_62


Javascript


C++
// C++ program for the above approach
#include
using namespace std;
 
// To sort the array and return the answer
int removals(int arr[], int n, int k)
{
   
  // sort the array
  sort(arr, arr + n);
  int dp[n];
 
  // fill all stated with -1
  // when only one element
  for(int i = 0; i < n; i++)
    dp[i] = -1;
 
  // as dp[0] = 0 (base case) so min
  // no of elements to be removed are
  // n-1 elements
  int ans = n - 1;
  dp[0] = 0;
  for (int i = 1; i < n; i++)
  {
    dp[i] = i;
    int j = dp[i - 1];
    while (j != i && arr[i] - arr[j] > k)
    {
      j++;
    }
    dp[i] = min(dp[i], j);
    ans = min(ans, (n - (i - j + 1)));
  }
  return ans;
}
 
// Driver code   
int main()
{
  int a[] = { 1, 3, 4, 9, 10, 11, 12, 17, 20 };
  int n = sizeof(a) / sizeof(a[0]);
  int k = 4;
  cout<


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
   
    // To sort the array and return the answer
    static int removals(int arr[], int n, int k)
    {
        // sort the array
        Arrays.sort(arr);
 
        // fill all stated with -1
        // when only one element
        int dp[] = new int[n];
        Arrays.fill(dp, -1);
       
        // as dp[0] = 0 (base case) so min
        // no of elements to be removed are
        // n-1 elements
        int ans = n - 1;
        dp[0] = 0;
       
        // Iterate from 1 to n - 1
        for (int i = 1; i < n; i++) {
            dp[i] = i;
            int j = dp[i - 1];
            while (j != i && arr[i] - arr[j] > k) {
                j++;
            }
            dp[i] = Integer.min(dp[i], j);
            ans = Integer.min(ans, (n - (i - j + 1)));
        }
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 1, 3, 4, 9, 10, 11, 12, 17, 20 };
        int n = a.length;
        int k = 4;
        System.out.print(removals(a, n, k));
    }
}


Python3
# Python3 program for the above approach
 
# To sort the array and return the answer
def removals(arr, n, k):
     
  # sort the array
  arr.sort()
  dp = [0 for i in range(n)]
 
  # Fill all stated with -1
  # when only one element
  for i in range(n):
    dp[i] = -1
 
  # As dp[0] = 0 (base case) so min
  # no of elements to be removed are
  # n-1 elements
  ans = n - 1
  dp[0] = 0
   
  for i in range(1, n):
    dp[i] = i
    j = dp[i - 1]
     
    while (j != i and arr[i] - arr[j] > k):
      j += 1
       
    dp[i] = min(dp[i], j)
    ans = min(ans, (n - (i - j + 1)))
     
  return ans
 
# Driver code   
a = [ 1, 3, 4, 9, 10, 11, 12, 17, 20 ]
n = len(a)
k = 4
 
print(removals(a, n, k))
 
# This code is contributed by rohan07


C#
// C# program for the above approach
using System;
 
class GFG{
     
// To sort the array and return the answer
static int removals(int[] arr, int n, int k)
{
     
    // Sort the array
    Array.Sort(arr);
    int[] dp = new int[n];
 
    // Fill all stated with -1
    // when only one element
    for(int i = 0; i < n; i++)
        dp[i] = -1;
 
    // As dp[0] = 0 (base case) so min
    // no of elements to be removed are
    // n-1 elements
    int ans = n - 1;
    dp[0] = 0;
     
    // Iterate from 1 to n - 1
    for(int i = 1; i < n; i++)
    {
        dp[i] = i;
        int j = dp[i - 1];
         
        while (j != i && arr[i] - arr[j] > k)
        {
            j++;
        }
        dp[i] = Math.Min(dp[i], j);
        ans = Math.Min(ans, (n - (i - j + 1)));
    }
    return ans;
}
 
// Driver code
static public void Main()
{
    int[] a = { 1, 3, 4, 9, 10, 11, 12, 17, 20 };
    int n = a.Length;
    int k = 4;
 
    Console.Write(removals(a, n, k));
}
}
 
// This code is contributed by lokeshpotta20


Javascript


输出
5

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

该解决方案可以进一步优化。这个想法是按递增顺序对数组进行排序并遍历所有元素(假设索引 i)并找到其右侧的最大元素(索引 j),使得 arr[j] – arr[i] <= k。因此,要删除的元素数量变为 n-(j-i+1)。可以通过取 n-(ji-1) 整体 i 的最小值来找到元素的最小数量。索引 j 的值可以通过 start = i+1 和 end = n-1 之间的二分查找找到;

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find
// rightmost index
// which satisfies the condition
// arr[j] - arr[i] <= k
int findInd(int key, int i,
            int n, int k, int arr[])
{
    int start, end, mid, ind = -1;
     
    // Initialising start to i + 1
    start = i + 1;
     
    // Initialising end to n - 1
    end = n - 1;
     
    // Binary search implementation
    // to find the rightmost element
    // that satisfy the condition
    while (start < end)
    {
        mid = start + (end - start) / 2;
         
        // Check if the condition satisfies
        if (arr[mid] - key <= k)
        {  
             
            // Store the position
            ind = mid;
             
            // Make start = mid + 1
            start = mid + 1;
        }
        else
        {
            // Make end = mid
            end = mid;
        }
    }
     
    // Return the rightmost position
    return ind;
}
 
// Function to calculate
// minimum number of elements
// to be removed
int removals(int arr[], int n, int k)
{
    int i, j, ans = n - 1;
     
    // Sort the given array
    sort(arr, arr + n);
     
    // Iterate from 0 to n-1
    for (i = 0; i < n; i++)
    {
         
        // Find j such that
        // arr[j] - arr[i] <= k
        j = findInd(arr[i], i, n, k, arr);
         
        // If there exist such j
        // that satisfies the condition
        if (j != -1)
        {
            // Number of elements
            // to be removed for this
            // particular case is
            // (j - i + 1)
            ans = min(ans, n - (j - i + 1));
        }
    }
     
    // Return answer
    return ans;
}
 
// Driver Code
int main()
{
    int a[] = {1, 3, 4, 9, 10,
               11, 12, 17, 20};
    int n = sizeof(a) / sizeof(a[0]);
    int k = 4;
    cout << removals(a, n, k);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find rightmost index
// which satisfies the condition
// arr[j] - arr[i] <= k
static int findInd(int key, int i,
                   int n, int k, int arr[])
{
    int start, end, mid, ind = -1;
     
    // Initialising start to i + 1
    start = i + 1;
     
    // Initialising end to n - 1
    end = n - 1;
     
    // Binary search implementation
    // to find the rightmost element
    // that satisfy the condition
    while (start < end)
    {
        mid = start + (end - start) / 2;
         
        // Check if the condition satisfies
        if (arr[mid] - key <= k)
        {
             
            // Store the position
            ind = mid;
             
            // Make start = mid + 1
            start = mid + 1;
        }
        else
        {
             
            // Make end = mid
            end = mid;
        }
    }
     
    // Return the rightmost position
    return ind;
}
 
// Function to calculate
// minimum number of elements
// to be removed
static int removals(int arr[], int n, int k)
{
    int i, j, ans = n - 1;
     
    // Sort the given array
    Arrays.sort(arr);
     
    // Iterate from 0 to n-1
    for(i = 0; i < n; i++)
    {
         
        // Find j such that
        // arr[j] - arr[i] <= k
        j = findInd(arr[i], i, n, k, arr);
         
        // If there exist such j
        // that satisfies the condition
        if (j != -1)
        {
             
            // Number of elements
            // to be removed for this
            // particular case is
            // (j - i + 1)
            ans = Math.min(ans,
                           n - (j - i + 1));
        }
    }
     
    // Return answer
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
    int a[] = { 1, 3, 4, 9, 10,
                11, 12, 17, 20 };
    int n = a.length;
    int k = 4;
     
    System.out.println(removals(a, n, k));
}
}
 
// This code is contributed by adityapande88

蟒蛇3

# Python program for the
# above approach
 
# Function to find
# rightmost index
# which satisfies the condition
# arr[j] - arr[i] <= k
def findInd(key, i, n,
            k, arr):
   
     ind = -1
     
     # Initialising start
     # to i + 1
     start = i + 1
       
     # Initialising end
     # to n - 1
     end = n - 1;
     
     # Binary search implementation
     # to find the rightmost element
     # that satisfy the condition
     while (start < end):
          mid = int(start +
                   (end - start) / 2)
         
          # Check if the condition
          # satisfies
          if (arr[mid] - key <= k):
             
               # Store the position
               ind = mid
                 
               # Make start = mid + 1
               start = mid + 1
          else:
               # Make end = mid
               end = mid
                 
     # Return the rightmost position
     return ind
     
# Function to calculate
# minimum number of elements
# to be removed
def removals(arr, n, k):
   
     ans = n - 1
     
     # Sort the given array
     arr.sort()
     
     # Iterate from 0 to n-1
     for i in range(0, n):
       
          # Find j such that
          # arr[j] - arr[i] <= k
          j = findInd(arr[i], i,
                      n, k, arr)
           
          # If there exist such j
          # that satisfies the condition
          if (j != -1):
             
               # Number of elements
               # to be removed for this
               # particular case is
               # (j - i + 1)
               ans = min(ans, n -
                        (j - i + 1))
               
     # Return answer
     return ans
     
# Driver Code
a = [1, 3, 4, 9,
     10,11, 12, 17, 20]
n = len(a)
k = 4
print(removals(a, n, k))
 
# This code is contributed by Stream_Cipher

C#

// C# program for the above approach
using System;
 
class GFG{
     
// Function to find rightmost index
// which satisfies the condition
// arr[j] - arr[i] <= k
static int findInd(int key, int i,
                   int n, int k, int[] arr)
{
    int start, end, mid, ind = -1;
      
    // Initialising start to i + 1
    start = i + 1;
      
    // Initialising end to n - 1
    end = n - 1;
      
    // Binary search implementation
    // to find the rightmost element
    // that satisfy the condition
    while (start < end)
    {
        mid = start + (end - start) / 2;
          
        // Check if the condition satisfies
        if (arr[mid] - key <= k)
        {
             
            // Store the position
            ind = mid;
              
            // Make start = mid + 1
            start = mid + 1;
        }
        else
        {
             
            // Make end = mid
            end = mid;
        }
    }
     
    // Return the rightmost position
    return ind;
}
  
// Function to calculate minimum
// number of elements to be removed
static int removals(int[] arr, int n, int k)
{
    int i, j, ans = n - 1;
      
    // Sort the given array
    Array.Sort(arr);
      
    // Iterate from 0 to n-1
    for(i = 0; i < n; i++)
    {
          
        // Find j such that
        // arr[j] - arr[i] <= k
        j = findInd(arr[i], i, n, k, arr);
          
        // If there exist such j
        // that satisfies the condition
        if (j != -1)
        {
              
            // Number of elements
            // to be removed for this
            // particular case is
            // (j - i + 1)
            ans = Math.Min(ans,
                           n - (j - i + 1));
        }
    }
      
    // Return answer
    return ans;
}
 
// Driver code
static void Main()
{
    int[] a = { 1, 3, 4, 9, 10,
                11, 12, 17, 20 };
    int n = a.Length;
    int k = 4;
      
    Console.Write(removals(a, n, k));
}
}
 
// This code is contributed by sanjoy_62

Javascript


输出
5

时间复杂度: O(nlogn)

辅助空间: O(n)

方法:

  1. 该解决方案可以进一步优化。这个想法是按递增顺序对数组进行排序并遍历所有元素(假设索引 j)并找到其左侧的最小元素(索引 i),使得 arr[j] – arr[i] <= k 并存储它在 dp[j]。
  2. 因此,要删除的元素数量变为 n-(j-i+1)。可以通过取 n-(ji-1) 整体 j 的最小值来找到最小元素数。索引 i 的值可以通过其之前的 dp 数组元素值找到。

下面是该方法的实现:

C++

// C++ program for the above approach
#include
using namespace std;
 
// To sort the array and return the answer
int removals(int arr[], int n, int k)
{
   
  // sort the array
  sort(arr, arr + n);
  int dp[n];
 
  // fill all stated with -1
  // when only one element
  for(int i = 0; i < n; i++)
    dp[i] = -1;
 
  // as dp[0] = 0 (base case) so min
  // no of elements to be removed are
  // n-1 elements
  int ans = n - 1;
  dp[0] = 0;
  for (int i = 1; i < n; i++)
  {
    dp[i] = i;
    int j = dp[i - 1];
    while (j != i && arr[i] - arr[j] > k)
    {
      j++;
    }
    dp[i] = min(dp[i], j);
    ans = min(ans, (n - (i - j + 1)));
  }
  return ans;
}
 
// Driver code   
int main()
{
  int a[] = { 1, 3, 4, 9, 10, 11, 12, 17, 20 };
  int n = sizeof(a) / sizeof(a[0]);
  int k = 4;
  cout<

Java

// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
   
    // To sort the array and return the answer
    static int removals(int arr[], int n, int k)
    {
        // sort the array
        Arrays.sort(arr);
 
        // fill all stated with -1
        // when only one element
        int dp[] = new int[n];
        Arrays.fill(dp, -1);
       
        // as dp[0] = 0 (base case) so min
        // no of elements to be removed are
        // n-1 elements
        int ans = n - 1;
        dp[0] = 0;
       
        // Iterate from 1 to n - 1
        for (int i = 1; i < n; i++) {
            dp[i] = i;
            int j = dp[i - 1];
            while (j != i && arr[i] - arr[j] > k) {
                j++;
            }
            dp[i] = Integer.min(dp[i], j);
            ans = Integer.min(ans, (n - (i - j + 1)));
        }
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 1, 3, 4, 9, 10, 11, 12, 17, 20 };
        int n = a.length;
        int k = 4;
        System.out.print(removals(a, n, k));
    }
}

蟒蛇3

# Python3 program for the above approach
 
# To sort the array and return the answer
def removals(arr, n, k):
     
  # sort the array
  arr.sort()
  dp = [0 for i in range(n)]
 
  # Fill all stated with -1
  # when only one element
  for i in range(n):
    dp[i] = -1
 
  # As dp[0] = 0 (base case) so min
  # no of elements to be removed are
  # n-1 elements
  ans = n - 1
  dp[0] = 0
   
  for i in range(1, n):
    dp[i] = i
    j = dp[i - 1]
     
    while (j != i and arr[i] - arr[j] > k):
      j += 1
       
    dp[i] = min(dp[i], j)
    ans = min(ans, (n - (i - j + 1)))
     
  return ans
 
# Driver code   
a = [ 1, 3, 4, 9, 10, 11, 12, 17, 20 ]
n = len(a)
k = 4
 
print(removals(a, n, k))
 
# This code is contributed by rohan07

C#

// C# program for the above approach
using System;
 
class GFG{
     
// To sort the array and return the answer
static int removals(int[] arr, int n, int k)
{
     
    // Sort the array
    Array.Sort(arr);
    int[] dp = new int[n];
 
    // Fill all stated with -1
    // when only one element
    for(int i = 0; i < n; i++)
        dp[i] = -1;
 
    // As dp[0] = 0 (base case) so min
    // no of elements to be removed are
    // n-1 elements
    int ans = n - 1;
    dp[0] = 0;
     
    // Iterate from 1 to n - 1
    for(int i = 1; i < n; i++)
    {
        dp[i] = i;
        int j = dp[i - 1];
         
        while (j != i && arr[i] - arr[j] > k)
        {
            j++;
        }
        dp[i] = Math.Min(dp[i], j);
        ans = Math.Min(ans, (n - (i - j + 1)));
    }
    return ans;
}
 
// Driver code
static public void Main()
{
    int[] a = { 1, 3, 4, 9, 10, 11, 12, 17, 20 };
    int n = a.Length;
    int k = 4;
 
    Console.Write(removals(a, n, k));
}
}
 
// This code is contributed by lokeshpotta20

Javascript


输出
5

时间复杂度:O(nlog n)。由于外循环将进行 n 次迭代。对于所有外部迭代,内部循环最多迭代 n 次。因为我们从 dp[i-1] 开始 j 的值并循环它直到它到达 i 然后对于下一个元素我们再次从前一个 dp[i] 值开始。因此,如果我们不考虑排序的复杂度,则总时间复杂度为 O(n),因为在上述解决方案中也没有考虑到这一点。

辅助空间:O(n)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程