📌  相关文章
📜  将数组拆分为最小数量的子集,最大和最小元素之间的差异最多为 K

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

将数组拆分为最小数量的子集,最大和最小元素之间的差异最多为 K

给定一个由N个整数和一个整数K组成的数组arr[] ,任务是找到最小的集合数,可以将数组元素划分为使得每个集合的最大和最小元素之间的差最多为K .

例子:

方法:给定问题可以通过对给定数组进行排序并找到可以分解数组元素的最小子数组数来解决,使得最大和最小元素之间的差异最多为 K 。请按照以下步骤解决给定的问题:

  • 以非递减顺序对给定数组arr[]进行排序。
  • 将两个迭代器开始结束初始化为0 ,表示每个集合的开始和结束。
  • 初始化一个变量,比如setCount1 ,它将数组元素分解为子数组的结果最小数量。
  • 迭代一个循环,直到end的值小于N并执行以下步骤:
    1. 如果arr[end] – arr[begin] <= K的值,则增加end的值。
    2. 否则,将值setCount增加1并更新表示新集合的beginend的值。
  • 完成上述步骤后,打印setCount的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of sets the array can be divided such
// that for each set max-min <= K
int minSetCount(int arr[], int N, int K)
{
    // Sort the input array
    sort(arr, arr + N);
 
    // Stores the count of set required
    int setCount = 1;
 
    // Stores the beginning and ending
    // of the current set
    int begin = 0, end = 0;
 
    // Loop to iterate over the array
    while (end < N) {
 
        // If arr[end] can be included
        // in the current set else
        // begin a new set
        if (arr[end] - arr[begin] <= K) {
            end++;
        }
        else {
            // Increment the set count
            setCount++;
            begin = end;
        }
    }
 
    // Return answer
    return setCount;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 2, 9, 7, 3, 2, 4, 6, 14, 10 };
    int N = sizeof(arr) / sizeof(int);
    int K = 3;
    cout << minSetCount(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG {
 
    // Function to find the minimum number
    // of sets the array can be divided such
    // that for each set max-min <= K
    static int minSetCount(int[] arr, int N, int K)
    {
        // Sort the input array
        Arrays.sort(arr);
 
        // Stores the count of set required
        int setCount = 1;
 
        // Stores the beginning and ending
        // of the current set
        int begin = 0, end = 0;
 
        // Loop to iterate over the array
        while (end < N) {
 
            // If arr[end] can be included
            // in the current set else
            // begin a new set
            if (arr[end] - arr[begin] <= K) {
                end++;
            }
            else {
                // Increment the set count
                setCount++;
                begin = end;
            }
        }
 
        // Return answer
        return setCount;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 5, 2, 9, 7, 3, 2, 4, 6, 14, 10 };
        int N = arr.length;
        int K = 3;
        System.out.print(minSetCount(arr, N, K));
    }
}
 
// This code is contributed by subham348.


Python3
# Python 3 program for the above approach
 
# Function to find the minimum number
# of sets the array can be divided such
# that for each set max-min <= K
def minSetCount(arr, N, K):
    # Sort the input array
    arr.sort()
 
    # Stores the count of set required
    setCount = 1
 
    # Stores the beginning and ending
    # of the current set
    begin = 0
    end = 0
 
    # Loop to iterate over the array
    while (end < N):
       
        # If arr[end] can be included
        # in the current set else
        # begin a new set
        if (arr[end] - arr[begin] <= K):
            end += 1
        else:
            # Increment the set count
            setCount += 1
            begin = end
 
    # Return answer
    return setCount
 
# Driver Code
if __name__ == '__main__':
    arr = [5, 2, 9, 7, 3, 2, 4, 6, 14, 10]
    N = len(arr)
    K = 3
    print(minSetCount(arr, N, K))
 
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
    // Function to find the minimum number
    // of sets the array can be divided such
    // that for each set max-min <= K
    static int minSetCount(int[] arr, int N, int K)
    {
       
        // Sort the input array
        Array.Sort(arr);
 
        // Stores the count of set required
        int setCount = 1;
 
        // Stores the beginning and ending
        // of the current set
        int begin = 0, end = 0;
 
        // Loop to iterate over the array
        while (end < N) {
 
            // If arr[end] can be included
            // in the current set else
            // begin a new set
            if (arr[end] - arr[begin] <= K) {
                end++;
            }
            else {
                // Increment the set count
                setCount++;
                begin = end;
            }
        }
 
        // Return answer
        return setCount;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] arr = { 5, 2, 9, 7, 3, 2, 4, 6, 14, 10 };
        int N = arr.Length;
        int K = 3;
        Console.WriteLine(minSetCount(arr, N, K));
    }
}
 
// This code is contributed by AnkThon


Javascript


输出:
4

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