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 = 3
Output: 2
Explanation:
One of the possible subsets of arr[] are {1, 3} and {7, 9, 10} where the difference between maximum and minimum element does not greater than K i.e, 3.
Input: arr[ ] = {1, 10, 8, 3, 9}, K =3
Output: 2.
Approach: Follow the steps below to solve the problem:
- Sort the array in ascending order.
- Iterate over the array, setting currMin as the first element of the array and keep updating currMax with the elements traversed.
- If at any index, the difference between currMax and currMin exceeds K, increment answer by 1 and set currMax and currMin to arr[i].
- Finally, return answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findCount( int arr[], int N, int K)
{
sort(arr, arr + N);
int result = 1;
int cur_max = arr[0];
int cur_min = arr[0];
for ( int i = 1; i < N; i++) {
cur_max = arr[i];
if (cur_max - cur_min > K) {
result++;
cur_max = arr[i];
cur_min = arr[i];
}
}
return result;
}
int main()
{
int arr[] = { 1,10, 8, 3, 9 };
int K = 3;
int N = sizeof (arr) / sizeof (arr[0]);
cout << findCount(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findCount( int arr[], int N, int K)
{
Arrays.sort(arr);
int result = 1 ;
int cur_max = arr[ 0 ];
int cur_min = arr[ 0 ];
for ( int i = 1 ; i < N; i++)
{
cur_max = arr[i];
if (cur_max - cur_min > K)
{
result++;
cur_max = arr[i];
cur_min = arr[i];
}
}
return result;
}
public static void main(String[] args)
{
int arr[] = { 1 , 10 , 8 , 3 , 9 };
int K = 3 ;
int N = arr.length;
System.out.print(findCount(arr, N, K));
}
}
|
Python3
def findCount(arr, N, K):
arr.sort()
result = 1
cur_max = arr[ 0 ]
cur_min = arr[ 0 ]
for i in range ( 1 , N):
cur_max = arr[i]
if (cur_max - cur_min > K):
result + = 1
cur_max = arr[i]
cur_min = arr[i]
return result
arr = [ 1 , 10 , 8 , 3 , 9 ]
K = 3
N = len (arr)
print (findCount(arr, N, K))
|
C#
using System;
class GFG{
static int findCount( int []arr,
int N, int K)
{
Array.Sort(arr);
int result = 1;
int cur_max = arr[0];
int cur_min = arr[0];
for ( int i = 1; i < N; i++)
{
cur_max = arr[i];
if (cur_max - cur_min > K)
{
result++;
cur_max = arr[i];
cur_min = arr[i];
}
}
return result;
}
public static void Main(String[] args)
{
int []arr = { 1, 10, 8, 3, 9 };
int K = 3;
int N = arr.Length;
Console.Write(findCount(arr, N, K));
}
}
|
Javascript
<script>
function findCount(arr, N, K)
{
arr.sort();
let result = 1;
let cur_max = arr[0];
let cur_min = arr[0];
for (let i = 1; i < N; i++)
{
cur_max = arr[i];
if (cur_max - cur_min > K)
{
result++;
cur_max = arr[i];
cur_min = arr[i];
}
}
return result;
}
let arr = [ 1, 10, 8, 3, 9 ];
let K = 3;
let N = arr.length;
document.write(findCount(arr, N, K));
</script>
|
Time Complexity: O(NLog(N))
Auxiliary Space: O(1)
Last Updated :
19 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...