Split array into minimum number of subsets having difference between maximum and minimum element at most K
Last Updated :
22 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 problem:
- 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:
- If the value of arr[end] - arr[begin] <= K, then increment the value of end.
- 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 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
<script>
// JavaScript Program to implement
// the above approach
// Function to find the minimum number
// of sets the array can be divided such
// that for each set max-min <= K
function minSetCount(arr, N, K) {
// Sort the input array
arr.sort(function (a, b) { return a - b })
// Stores the count of set required
let setCount = 1;
// Stores the beginning and ending
// of the current set
let 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
let arr = [5, 2, 9, 7, 3, 2, 4, 6, 14, 10];
let N = arr.length;
let K = 3;
document.write(minSetCount(arr, N, K));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Similar Reads
Split array into minimum number of subsets having maximum pair sum at most K Given an array, arr[] of size n, and an integer k, the task is to partition the array into the minimum number of subsets such that the maximum pair sum in each subset is less than or equal to k.Examples:Input: arr[] = [1, 2, 3, 4, 5], k = 5Output: 3Explanation: Subset having maximum pair sum less th
4 min read
Minimize sum of differences between maximum and minimum elements present in K subsets Given an array arr[] of size N and an integer K, the task is to minimize the sum of the difference between the maximum and minimum element of each subset by splitting the array into K subsets such that each subset consists of unique array elements only. Examples: Input: arr[] = { 6, 3, 8, 1, 3, 1, 2
12 min read
Minimize count of Subsets with difference between maximum and minimum element not exceeding K Given an array arr[ ] and an integer K, the task is to split the given array into minimum number of subsets having the difference between the maximum and the minimum element ⤠K. Examples: Input: arr[ ] = {1, 3, 7, 9, 10}, K = 3Output: 2Explanation:One of the possible subsets of arr[] are {1, 3} and
5 min read
Split a given array into K subarrays minimizing the difference between their maximum and minimum Given a sorted array arr[] of N integers and an integer K, the task is to split the array into K subarrays such that the sum of the difference of maximum and minimum element of each subarray is minimized. Examples: Input: arr[] = {1, 3, 3, 7}, K = 4 Output: 0 Explanation: The given array can be spli
6 min read
Split array into K Subarrays to minimize sum of difference between min and max Given a sorted array arr[] of size N and integer K, the task is to split the array into K non-empty subarrays such that the sum of the difference between the maximum element and the minimum element of each subarray is minimized. Note: Every element of the array must be included in one subarray and e
6 min read