Split array into minimum number of subsets having maximum pair sum at most K
Last Updated :
23 Jul, 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)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem