Split array into minimum number of subsets having maximum pair sum at most K
Last Updated :
19 Jul, 2021
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 problem can be solved using two pointer technique. The idea is to partition the array such that the maximum pair sum of each subset is minimized. Follow the steps below to solve the problem:
- Sort the given array.
- Initialize a variable say, res to store the minimum number of subsets that satisfy the given condition.
- Initialize two variables, say start and end to store the start and end index of the sorted array respectively.
- Traverse the sorted array and check if arr[start] + arr[end] ? K or not. If found to be true, then increment the value of start by 1.
- Otherwise, decrement the value of end by 1 and increment the res by 1.
- Finally, print the value of res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntMinSub( int arr[],
int N, int K)
{
int res = 0;
int start = 0;
int end = N - 1;
sort(arr, arr + N);
while (end - start > 1) {
if (arr[start] + arr[end]
<= K) {
start++;
}
else {
res++;
end--;
}
}
if (end - start == 1) {
if (arr[start] + arr[end]
<= K) {
res++;
start++;
end--;
}
else {
res++;
end--;
}
}
if (start == end) {
res++;
}
return res;
}
int main()
{
int arr[] = { 2, 6, 8, 10, 20, 25 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 26;
cout << cntMinSub(arr, N, K);
}
|
Java
import java.util.*;
class GFG{
static int cntMinSub( int arr[],
int N, int K)
{
int res = 0 ;
int start = 0 ;
int end = N - 1 ;
Arrays.sort(arr);
while (end - start > 1 )
{
if (arr[start] +
arr[end] <= K)
{
start++;
}
else
{
res++;
end--;
}
}
if (end - start == 1 )
{
if (arr[start] +
arr[end] <= K)
{
res++;
start++;
end--;
}
else
{
res++;
end--;
}
}
if (start == end)
{
res++;
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 2 , 6 , 8 , 10 , 20 , 25 };
int N = arr.length;
int K = 26 ;
System.out.print(cntMinSub(arr, N, K));
}
}
|
Python3
def cntMinSub(arr, N, K):
res = 0
start = 0
end = N - 1
arr = sorted (arr)
while (end - start > 1 ):
if (arr[start] + arr[end] < = K):
start + = 1
else :
res + = 1
end - = 1
if (end - start = = 1 ):
if (arr[start] + arr[end] < = K):
res + = 1
start + = 1
end - = 1
else :
res + = 1
end - = 1
if (start = = end):
res + = 1
return res
if __name__ = = '__main__' :
arr = [ 2 , 6 , 8 , 10 , 20 , 25 ]
N = len (arr)
K = 26
print (cntMinSub(arr, N, K))
|
C#
using System;
class GFG{
static int cntMinSub( int []arr,
int N, int K)
{
int res = 0;
int start = 0;
int end = N - 1;
Array.Sort(arr);
while (end - start > 1)
{
if (arr[start] +
arr[end] <= K)
{
start++;
}
else
{
res++;
end--;
}
}
if (end - start == 1)
{
if (arr[start] +
arr[end] <= K)
{
res++;
start++;
end--;
}
else
{
res++;
end--;
}
}
if (start == end)
{
res++;
}
return res;
}
public static void Main(String[] args)
{
int []arr = {2, 6, 8, 10, 20, 25};
int N = arr.Length;
int K = 26;
Console.Write(cntMinSub(arr, N, K));
}
}
|
Javascript
<script>
function cntMinSub(arr, N, K)
{
var res = 0;
var start = 0;
var end = N - 1;
arr.sort();
while (end - start > 1)
{
if (arr[start] + arr[end] <= K)
{
start++;
}
else
{
res++;
end--;
}
}
if (end - start == 1)
{
if (arr[start] + arr[end] <= K)
{
res++;
start++;
end--;
}
else
{
res++;
end--;
}
}
if (start == end)
{
res++;
}
return res;
}
var arr = [ 2, 6, 8, 10, 20, 25 ];
var N = arr.length;
var K = 26;
document.write(cntMinSub(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...