📌  相关文章
📜  通过最多 K 个替换来最小化 Array 的最大值和最小值之间的差异

📅  最后修改于: 2021-09-02 06:01:04             🧑  作者: Mango

给定一个数组arr[]和一个 整数K ,该任务是选择数组中至多 K 个元素并将其替换为任意数字。求最多执行K 次替换后数组的最大值和最小值之间的最小差值

例子:

方法:这个想法是使用两个指针的概念。以下是步骤:

  1. 对给定的数组进行排序。
  2. 维护两个指针,一个指向数组的最后一个元素,另一个指向数组的第K元素。
  3. 遍历数组K+1次,每次找出两个指针指向的元素的差值。
  4. 每次找到差异时,跟踪变量中可能的最小差异并在最后返回该值。

下面是上述方法的实现:

C++
// C++ program of the approach
#include 
using namespace std;
 
// Function to find minimum difference
// between the maximum and the minimum
// elements arr[] by at most K replacements
int maxMinDifference(int arr[], int n, int k)
{
    // Check if turns are more than
    // or equal to n-1 then simply
    // return zero
    if (k >= n - 1)
        return 0;
 
    // Sort the array
    sort(arr, arr + n);
 
    // Set difference as the
    // maximum possible difference
    int ans = arr[n - 1] - arr[0];
 
    // Iterate over the array to
    // track the minimum difference
    // in k turns
    for (int i = k, j = n - 1;
         i >= 0; --i, --j) {
 
        ans = min(arr[j] - arr[i], ans);
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 4, 6, 11, 15 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given K replacements
    int K = 3;
 
    // Function Call
    cout << maxMinDifference(arr, N, K);
    return 0;
}


Java
// Java program of the approach
import java.io.*;
import java.util.Arrays;
class GFG{
 
// Function to find minimum difference
// between the maximum and the minimum
// elements arr[] by at most K replacements
static int maxMinDifference(int arr[], int n, int k)
{
    // Check if turns are more than
    // or equal to n-1 then simply
    // return zero
    if (k >= n - 1)
        return 0;
 
    // Sort the array
    Arrays.sort(arr);
 
    // Set difference as the
    // maximum possible difference
    int ans = arr[n - 1] - arr[0];
 
    // Iterate over the array to
    // track the minimum difference
    // in k turns
    for (int i = k, j = n - 1;
             i >= 0; --i, --j)
    {
        ans = Math.min(arr[j] - arr[i], ans);
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given array arr[]
    int arr[] = { 1, 4, 6, 11, 15 };
    int N = arr.length;
 
    // Given K replacements
    int K = 3;
 
    // Function Call
    System.out.print(maxMinDifference(arr, N, K));
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 program for the above approach
 
# Function to find minimum difference
# between the maximum and the minimum
# elements arr[] by at most K replacements
def maxMinDifference(arr, n, k):
 
    # Check if turns are more than
    # or equal to n-1 then simply
    # return zero
    if(k >= n - 1):
        return 0
 
    # Sort the array
    arr.sort()
 
    # Set difference as the
    # maximum possible difference
    ans = arr[n - 1] - arr[0]
 
    # Iterate over the array to
    # track the minimum difference
    # in k turns
    i = k
    j = n - 1
    while i >= 0:
        ans = min(arr[j] - arr[i], ans)
        i -= 1
        j -= 1
 
    # Return the answer
    return ans
 
# Driver code
if __name__ == '__main__':
 
    # Given array arr[]
    arr = [ 1, 4, 6, 11, 15 ]
    N = len(arr)
 
    # Given K replacements
    K = 3
 
    # Function Call
    print(maxMinDifference(arr, N, K))
 
# This code is contributed by Shivam Singh


C#
// C# program of the approach
using System;
class GFG{
  
// Function to find minimum difference
// between the maximum and the minimum
// elements arr[] by at most K replacements
static int maxMinDifference(int []arr, int n, int k)
{
    // Check if turns are more than
    // or equal to n-1 then simply
    // return zero
    if (k >= n - 1)
        return 0;
  
    // Sort the array
    Array.Sort(arr);
  
    // Set difference as the
    // maximum possible difference
    int ans = arr[n - 1] - arr[0];
  
    // Iterate over the array to
    // track the minimum difference
    // in k turns
    for (int i = k, j = n - 1;
             i >= 0; --i, --j)
    {
        ans = Math.Min(arr[j] - arr[i], ans);
    }
  
    // Return the answer
    return ans;
}
  
// Driver Code
public static void Main(string[] args)
{
    // Given array arr[]
    int [] arr = new  int[] { 1, 4, 6, 11, 15 };
    int N = arr.Length;
  
    // Given K replacements
    int K = 3;
  
    // Function Call
    Console.Write(maxMinDifference(arr, N, K));
}
}
  
// This code is contributed by Ritik Bansal


Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live