GFG App
Open App
Browser
Continue

# Minimize splits in given Array to find subsets of at most 2 elements with sum at most K

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

 `// C++ program of the above approach` `#include ` `using` `namespace` `std;`   `// Function to split array into minimum` `// subsets of at most 2 elements with` `// sum of each subset at most K` `int` `minSubsetCnt(vector<``int``>& arr, ``int` `K)` `{` `    ``// Sort arr in increasing order` `    ``sort(arr.begin(), arr.end());`   `    ``// If it is impossible` `    ``if` `(arr[arr.size() - 1] > K) {` `        ``return` `-1;` `    ``}`   `    ``// Stores the count of subsets` `    ``int` `cnt = 0;`   `    ``// Starting pointer` `    ``int` `i = 0;`   `    ``// End pointer` `    ``int` `j = arr.size() - 1;`   `    ``// Loop for the two` `    ``// pointer approach` `    ``while` `(i <= j) {` `        ``if` `(arr[i] + arr[j] <= K) {` `            ``cnt++;` `            ``i++;` `            ``j--;` `        ``}` `        ``else` `{` `            ``cnt++;` `            ``j--;` `        ``}` `    ``}`   `    ``// Return Answer` `    ``return` `cnt;` `}` `// Driver Code` `int` `main()` `{` `    ``vector<``int``> arr{ 3, 2, 2, 3, 1 };` `    ``int` `K = 3;` `    ``cout << minSubsetCnt(arr, K);`   `    ``return` `0;` `}`

## Java

 `// Java program of the above approach ` `import` `java.util.*;`   `class` `GFG{`   `// Function to split array into minimum` `// subsets of at most 2 elements with` `// sum of each subset at most K` `static` `int` `minSubsetCnt(``int``[] arr, ``int` `K)` `{` `    `  `    ``// Sort arr in increasing order` `    ``Arrays.sort(arr);`   `    ``// If it is impossible` `    ``if` `(arr[arr.length - ``1``] > K) ` `    ``{` `        ``return` `-``1``;` `    ``}`   `    ``// Stores the count of subsets` `    ``int` `cnt = ``0``;`   `    ``// Starting pointer` `    ``int` `i = ``0``;`   `    ``// End pointer` `    ``int` `j = arr.length - ``1``;`   `    ``// Loop for the two` `    ``// pointer approach` `    ``while` `(i <= j) ` `    ``{` `        ``if` `(arr[i] + arr[j] <= K) ` `        ``{` `            ``cnt++;` `            ``i++;` `            ``j--;` `        ``}` `        ``else` `        ``{` `            ``cnt++;` `            ``j--;` `        ``}` `    ``}`   `    ``// Return Answer` `    ``return` `cnt;` `}`   `// Driver Code` `public` `static` `void` `main(String args[])` `{` `    ``int``[] arr = { ``3``, ``2``, ``2``, ``3``, ``1` `};` `    ``int` `K = ``3``;` `    `  `    ``System.out.print(minSubsetCnt(arr, K));` `}` `}`   `// This code is contributed by sanjoy_62`

## Python

 `# Python program of the above approach`   `# Function to split array into minimum` `# subsets of at most 2 elements with` `# sum of each subset at most K` `def` `minSubsetCnt(arr, K):` `    `  `    ``# Sort arr in increasing order` `    ``arr.sort()`   `    ``# If it is impossible` `    ``if` `(arr[``len``(arr) ``-` `1``] > K):` `        ``return` `-``1`   `    ``# Stores the count of subsets` `    ``cnt ``=` `0``;`   `    ``# Starting pointer` `    ``i ``=` `0``;`   `    ``# End pointer` `    ``j ``=` `len``(arr) ``-` `1``;`   `    ``# Loop for the two` `    ``# pointer approach` `    ``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 Answer` `    ``return` `cnt`   `# Driver Code` `arr ``=` `[ ``3``, ``2``, ``2``, ``3``, ``1` `]` `K ``=` `3` `print``(minSubsetCnt(arr, K))`   `# This code is contributed by Samim Hossain Mondal.`

## C#

 `// C# implementation for the above approach` `using` `System;` `class` `GFG` `{` `  `  `// Function to split array into minimum` `// subsets of at most 2 elements with` `// sum of each subset at most K` `static` `int` `minSubsetCnt(``int``[] arr, ``int` `K)` `{` `    `  `    ``// Sort arr in increasing order` `    ``Array.Sort(arr);`   `    ``// If it is impossible` `    ``if` `(arr[arr.Length - 1] > K) ` `    ``{` `        ``return` `-1;` `    ``}`   `    ``// Stores the count of subsets` `    ``int` `cnt = 0;`   `    ``// Starting pointer` `    ``int` `i = 0;`   `    ``// End pointer` `    ``int` `j = arr.Length - 1;`   `    ``// Loop for the two` `    ``// pointer approach` `    ``while` `(i <= j) ` `    ``{` `        ``if` `(arr[i] + arr[j] <= K) ` `        ``{` `            ``cnt++;` `            ``i++;` `            ``j--;` `        ``}` `        ``else` `        ``{` `            ``cnt++;` `            ``j--;` `        ``}` `    ``}`   `    ``// Return Answer` `    ``return` `cnt;` `}`   `// Driver Code` `public` `static` `void` `Main()` `{` `    ``int``[] arr = { 3, 2, 2, 3, 1 };` `    ``int` `K = 3;` `    `  `    ``Console.Write(minSubsetCnt(arr, K));` `}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`4`

Time Complexity: O(N*log N)
Auxiliary space: O(1)

My Personal Notes arrow_drop_up