Given an array arr[] of N integers and an integer K, the task is to calculate the minimum number of subsets of almost 2 elements the array can be divided such that the sum of elements in each subset is almost K.
Examples:
Input: arr[] = {1, 2, 3}, K = 3
Output: 2
Explanation: The given array can be divided into subsets as {1, 2} and {3}, and the sum of both the subsets is atmost K. Hence, the count of subsets is 2 which is the minimum possible.
Input: arr[] = {3, 2, 2, 3, 1}, K = 3
Output: 4
Input: arr[] = {3, 2, 2, 3, 1}, K = 2
Output: -1
Approach: The given problem can be solved using a greedy approach with the help of two pointer approach. The idea is to group together the integer with minimum value with the maximum possible value such that their sum does not exceed K. Follow the steps to solve the given problem:
- Sort the given array in non-decreasing order.
- Create two variables, i = 0 and j = N – 1, where i represents the 1st index and j represents the last index of the array.
- If the sum of arr[i] and arr[j] is almost K, increment i and decrement j. Also, increment the subset count by 1.
- If the sum of arr[i] and arr[j] is more than K, decrement j and increment the subset count.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int minSubsetCnt(vector< int >& arr, int K)
{
sort(arr.begin(), arr.end());
if (arr[arr.size() - 1] > K) {
return -1;
}
int cnt = 0;
int i = 0;
int j = arr.size() - 1;
while (i <= j) {
if (arr[i] + arr[j] <= K) {
cnt++;
i++;
j--;
}
else {
cnt++;
j--;
}
}
return cnt;
}
int main()
{
vector< int > arr{ 3, 2, 2, 3, 1 };
int K = 3;
cout << minSubsetCnt(arr, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minSubsetCnt( int [] arr, int K)
{
Arrays.sort(arr);
if (arr[arr.length - 1 ] > K)
{
return - 1 ;
}
int cnt = 0 ;
int i = 0 ;
int j = arr.length - 1 ;
while (i <= j)
{
if (arr[i] + arr[j] <= K)
{
cnt++;
i++;
j--;
}
else
{
cnt++;
j--;
}
}
return cnt;
}
public static void main(String args[])
{
int [] arr = { 3 , 2 , 2 , 3 , 1 };
int K = 3 ;
System.out.print(minSubsetCnt(arr, K));
}
}
|
Python
def minSubsetCnt(arr, K):
arr.sort()
if (arr[ len (arr) - 1 ] > K):
return - 1
cnt = 0 ;
i = 0 ;
j = len (arr) - 1 ;
while (i < = j):
if (arr[i] + arr[j] < = K):
cnt = cnt + 1
i = i + 1
j = j - 1
else :
cnt = cnt + 1
j = j - 1
return cnt
arr = [ 3 , 2 , 2 , 3 , 1 ]
K = 3
print (minSubsetCnt(arr, K))
|
C#
using System;
class GFG
{
static int minSubsetCnt( int [] arr, int K)
{
Array.Sort(arr);
if (arr[arr.Length - 1] > K)
{
return -1;
}
int cnt = 0;
int i = 0;
int j = arr.Length - 1;
while (i <= j)
{
if (arr[i] + arr[j] <= K)
{
cnt++;
i++;
j--;
}
else
{
cnt++;
j--;
}
}
return cnt;
}
public static void Main()
{
int [] arr = { 3, 2, 2, 3, 1 };
int K = 3;
Console.Write(minSubsetCnt(arr, K));
}
}
|
Javascript
<script>
function minSubsetCnt(arr, K) {
arr.sort( function (a, b) { return a - b })
if (arr[arr.length - 1] > K) {
return -1;
}
let cnt = 0;
let i = 0;
let j = arr.length - 1;
while (i <= j) {
if (arr[i] + arr[j] <= K) {
cnt++;
i++;
j--;
}
else {
cnt++;
j--;
}
}
return cnt;
}
let arr = [3, 2, 2, 3, 1];
let K = 3;
document.write(minSubsetCnt(arr, K));
</script>
|
Time Complexity: O(N*log N)
Auxiliary space: O(1)