Split array into minimum number of subsets having maximum pair sum at most K
Last Updated :
24 Feb, 2025
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 = 5
Output: 3
Explanation: Subset having maximum pair sum less than or equal to k (= 5) are: {{2, 3}, {1, 4}, {5}}. Therefore, the required output is 3.
Input: arr[] = [2, 6, 8, 10, 20, 25], k = 26
Output: 3
Explanation: Subset having maximum pair sum less than or equal to K(=26) are: {{2, 6, 8, 10}, {20}, {25}}. Therefore, the required output is 3.
Approach:
The idea is to sort the given array and find the first index ind where arr[ind] + arr[ind-1] > k.
- All the elements till index ind - 1 can create a single subset, and all remaining elements will be in different subsets, because pairing any of them will result in sum greater than k.
- Thus the answer will be 1 + n - ind (we are adding 1 for the first subset).
- To do so, firstly sort the array and traverse till the index where arr[ind] + arr[ind-1] <= k.
- The loop will break once the condition is not met, or we read the end of array. At last return 1 + n - ind.
C++
#include <bits/stdc++.h>
using namespace std;
// Function to get the minimum count of subsets
// that satisfy the given condition
int minSubsetCount(vector<int> &arr, int k) {
int n = arr.size();
// Sort the array
sort(arr.begin(), arr.end());
// to store the index upto which
// arr[ind] + arr[ind-1] <= k
int ind = 1;
while(ind < n && arr[ind] + arr[ind-1] <= k)
ind++;
return 1 + n - ind;
}
int main() {
vector<int> arr = {2, 6, 8, 10, 20, 25};
int k = 26;
cout << minSubsetCount(arr, k);
return 0;
}
Java
// Function to get the minimum count of subsets
// that satisfy the given condition
import java.util.*;
class GFG {
static int minSubsetCount(ArrayList<Integer> arr, int k) {
int n = arr.size();
// Sort the array
Collections.sort(arr);
// to store the index upto which
// arr[ind] + arr[ind-1] <= k
int ind = 1;
while (ind < n && arr.get(ind) + arr.get(ind - 1) <= k)
ind++;
return 1 + n - ind;
}
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(2, 6, 8, 10, 20, 25));
int k = 26;
System.out.println(minSubsetCount(arr, k));
}
}
Python
# Function to get the minimum count of subsets
# that satisfy the given condition
def minSubsetCount(arr, k):
n = len(arr)
# Sort the array
arr.sort()
# to store the index upto which
# arr[ind] + arr[ind-1] <= k
ind = 1
while ind < n and arr[ind] + arr[ind - 1] <= k:
ind += 1
return 1 + n - ind
if __name__ == "__main__":
arr = [2, 6, 8, 10, 20, 25]
k = 26
print(minSubsetCount(arr, k))
C#
// Function to get the minimum count of subsets
// that satisfy the given condition
using System;
using System.Collections.Generic;
class GFG {
static int minSubsetCount(List<int> arr, int k) {
int n = arr.Count;
// Sort the array
arr.Sort();
// to store the index upto which
// arr[ind] + arr[ind-1] <= k
int ind = 1;
while (ind < n && arr[ind] + arr[ind - 1] <= k)
ind++;
return 1 + n - ind;
}
static void Main() {
List<int> arr = new List<int> {2, 6, 8, 10, 20, 25};
int k = 26;
Console.WriteLine(minSubsetCount(arr, k));
}
}
JavaScript
// Function to get the minimum count of subsets
// that satisfy the given condition
function minSubsetCount(arr, k) {
let n = arr.length;
// Sort the array
arr.sort((a, b) => a - b);
// to store the index upto which
// arr[ind] + arr[ind-1] <= k
let ind = 1;
while (ind < n && arr[ind] + arr[ind - 1] <= k)
ind++;
return 1 + n - ind;
}
let arr = [2, 6, 8, 10, 20, 25];
let k = 26;
console.log(minSubsetCount(arr, k));
Time Complexity: O(n * log (n)), to sort the array.
Auxiliary Space: O(1)
Similar Reads
Split array into minimum number of subsets having difference between maximum and minimum element at most K 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: 2E
6 min read
Split array into K subsets to maximize their sum of maximums and minimums Given an integer K and an array A[ ] whose length is multiple of K, the task is to split the elements of the given array into K subsets, each having an equal number of elements, such that the sum of the maximum and minimum elements of each subset is the maximum summation possible. Examples: Input: K
6 min read
Maximum number of subsets an array can be split into such that product of their minimums with size of subsets is at least K Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of disjoint subsets that the given array can be split into such that the product of the minimum element of each subset with the size of the subset is at least K. Examples: Input: arr[] = {7, 11, 2,
8 min read
Find sum of difference of maximum and minimum over all possible subsets of size K Given an array arr[] of N integers and an integer K, the task is to find the sum of the difference between the maximum and minimum elements over all possible subsets of size K. Examples: Input: arr[] = {1, 1, 3, 4}, K = 2Output: 11Explanation:There are 6 subsets of the given array of size K(= 2). Th
12 min read
Split array into maximum possible subsets having product of their length with the maximum element at least K Given an array arr[] consisting of N integers and a positive integer K, the task is to maximize the number of subsets having a product of its size and its maximum element at least K by dividing array element into disjoint subsets. Examples: Input: N = 5, K = 4, arr[] = {1, 5, 4, 2, 3}Output: 3Explan
5 min read