The Wayback Machine - https://web.archive.org/web/20210903151959/https://www.geeksforgeeks.org/split-array-into-minimum-number-of-subsets-having-difference-between-maximum-and-minimum-element-at-most-k/
Skip to content
Related Articles

Related Articles

Improve Article

Split array into minimum number of subsets having difference between maximum and minimum element at most K

  • Last Updated : 03 Sep, 2021

Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum number of sets, the array elements can be divided into such that the difference between the maximum and minimum element of each set is at most K.

Examples:

Input: arr[] = {1, 2, 3, 4, 5}, K = 2 
Output: 2
Explanation:
The given array can be divided into two sets {1, 2, 3} having the difference between maximum and minimum as 3 – 1= 2 and {4, 5} having the difference between maximum and minimum as 5 – 4 = 1.

Input: arr[] = {5, 2, 9, 7, 3, 2, 4, 6, 14, 10}, K = 3
Output: 4

Approach: The given problem can be solved by sorting the given array and finding the minimum number of subarrays the array elements can be broken such that the difference between the maximum and minimum element at most K. Follow the steps below to solve the given problm:



  • Sort the given array arr[] in non-decreasing order.
  • Initialize two iterators begin and end as 0 representing the beginning and end of each set.
  • Initialize a variable, say setCount as 1 that stores the resultant minimum number of breaking of array elements into subarrays.
  • Iterate a loop until the value of end is less than N and perform the following steps:
    1. If the value of arr[end] – arr[begin] <= K, then increment the value of end.
    2. Otherwise, increment the value setCount by 1 and update the value of begin to end representing the new set.
  • After completing the above steps, print the value of setCount as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
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 begning 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;
}
Output:
4

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :