Given an array arr[] of positive numbers, find minimum number of sets in array which satisfy following property,

1. A set can contain maximum two elements in it. The two elements need not to be contiguous.

2. Sum of elements of set should be less then or equal to given Key. It may be assumed that given key is greater than or equal to the largest array element.

Examples:

Input: arr[] = [10, 20, 12], key = 25 Output: 2 We break into two parts {10, 12} and {2} Input : arr[] = [3, 5, 3, 4], key=5 Output : 4 Explanation: 4 sets (3), (5), (3), (4)

The idea is to first sort the array, then follow two pointer approach. We begin two pointers from two corners of the sorted array. If their sum is smaller than or equal to given key, then we make set of them, else we consider the last element alone.

Below is the implementation of the above approach :

## C++

`// C++ program to count minimum number of partitions ` `// of size 2 and sum smaller than or equal to given ` `// key. ` `#include <algorithm> ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `minimumSets(` `int` `arr[], ` `int` `n, ` `int` `key) ` `{ ` ` ` `int` `i, j; ` ` ` ` ` `// sort the array ` ` ` `sort(arr, arr + n); ` ` ` ` ` `// if sum of ith smaller and jth larger element is ` ` ` `// less than key, then pack both numbers in a set ` ` ` `// otherwise pack the jth larger number ` ` ` `// alone in the set ` ` ` `for` `(i = 0, j = n - 1; i <= j; ++i) ` ` ` `if` `(arr[i] + arr[j] <= key) ` ` ` `j--; ` ` ` ` ` `// After ending of loop i will contain minimum ` ` ` `// number of sets ` ` ` `return` `i; ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 5, 3, 4 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `key = 5; ` ` ` `cout << minimumSets(arr, n, key); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count minimum number of partitions ` `// of size 2 and sum smaller than or equal to given ` `// key. ` ` ` `import` `java.util.Arrays; ` `class` `GFG { ` ` ` ` ` `static` `int` `minimumSets(` `int` `arr[], ` `int` `n, ` `int` `key) ` `{ ` ` ` `int` `i, j; ` ` ` ` ` `// sort the array ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// if sum of ith smaller and jth larger element is ` ` ` `// less than key, then pack both numbers in a set ` ` ` `// otherwise pack the jth larger number ` ` ` `// alone in the set ` ` ` `for` `(i = ` `0` `, j = n - ` `1` `; i <= j; ++i) ` ` ` `if` `(arr[i] + arr[j] <= key) ` ` ` `j--; ` ` ` ` ` `// After ending of loop i will contain minimum ` ` ` `// number of sets ` ` ` `return` `i; ` `} ` ` ` ` ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `[]arr = { ` `3` `, ` `5` `, ` `3` `, ` `4` `}; ` ` ` `int` `n =arr.length; ` ` ` `int` `key = ` `5` `; ` ` ` `System.out.println( minimumSets(arr, n, key)); ` ` ` `} ` `} ` `// This code is contributed by chandan_jnu. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to count minimum number ` `# of partitions of size 2 and sum smaller ` `# than or equal to given key. ` ` ` `def` `minimumSets(arr, n, key): ` ` ` ` ` `# sort the array ` ` ` `arr.sort(reverse ` `=` `False` `) ` ` ` ` ` `# if sum of ith smaller and jth larger ` ` ` `# element is less than key, then pack ` ` ` `# both numbers in a set otherwise pack ` ` ` `# the jth larger number alone in the set ` ` ` `j ` `=` `n ` `-` `1` ` ` `for` `i ` `in` `range` `(` `0` `, j ` `+` `1` `, ` `1` `): ` ` ` `if` `(arr[i] ` `+` `arr[j] <` `=` `key): ` ` ` `j ` `-` `=` `1` ` ` ` ` `# After ending of loop i will ` ` ` `# contain minimum number of sets ` ` ` `return` `i ` `+` `1` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `3` `, ` `5` `, ` `3` `, ` `4` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `key ` `=` `5` ` ` `print` `(minimumSets(arr, n, key)) ` ` ` `# This code is contributed by ` `# Shashank_Sharma ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count minimum ` `// number of partitions of size ` `// 2 and sum smaller than or ` `// equal to given key. ` `using` `System; ` `class` `GFG ` `{ ` ` ` `static` `int` `minimumSets(` `int` `[]arr, ` ` ` `int` `n, ` `int` `key) ` `{ ` ` ` `int` `i, j; ` ` ` ` ` `// sort the array ` ` ` `Array.Sort(arr); ` ` ` ` ` `// if sum of ith smaller and ` ` ` `// jth larger element is less ` ` ` `// than key, then pack both ` ` ` `// numbers in a set otherwise ` ` ` `// pack the jth larger number ` ` ` `// alone in the set ` ` ` `for` `(i = 0, j = n - 1; i <= j; ++i) ` ` ` `if` `(arr[i] + arr[j] <= key) ` ` ` `j--; ` ` ` ` ` `// After ending of loop i ` ` ` `// will contain minimum ` ` ` `// number of sets ` ` ` `return` `i; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `[]arr = { 3, 5, 3, 4 }; ` ` ` `int` `n =arr.Length; ` ` ` `int` `key = 5; ` ` ` `Console.WriteLine(minimumSets(arr, n, key)); ` `} ` `} ` ` ` `// This code is contributed ` `// by chandan_jnu. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count minimum ` `// number of partitions of size ` `// 2 and sum smaller than or ` `// equal to given key. ` `function` `minimumSets(` `$arr` `, ` `$n` `, ` `$key` `) ` `{ ` ` ` `$i` `; ` `$j` `; ` ` ` ` ` `// sort the array ` ` ` `sort(` `$arr` `); ` ` ` ` ` `// if sum of ith smaller and ` ` ` `// jth larger element is less ` ` ` `// than key, then pack both ` ` ` `// numbers in a set otherwise ` ` ` `// pack the jth larger number ` ` ` `// alone in the set ` ` ` `for` `(` `$i` `= 0, ` `$j` `= ` `$n` `- 1; ` `$i` `<= ` `$j` `; ++` `$i` `) ` ` ` `if` `(` `$arr` `[` `$i` `] + ` `$arr` `[` `$j` `] <= ` `$key` `) ` ` ` `$j` `--; ` ` ` `return` `$i` `; ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `( 3, 5, 3, 4 ); ` `$n` `= ` `count` `(` `$arr` `); ` `$key` `= 5; ` `echo` `minimumSets(` `$arr` `, ` `$n` `, ` `$key` `); ` ` ` `// This code is contributed ` `// by chandan_jnu ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time complexity:** O(nlogn)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Partitions possible such that the minimum element divides all the other elements of the partition
- Maximum number of partitions that can be sorted individually to make sorted
- Find duplicates in a given array when elements are not limited to a range
- Minimum difference between maximum and minimum value of Array with given Operations
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Find frequency of each element in a limited range array in less than O(n) time
- Find even occurring elements in an array of limited range
- Find the missing number in a sorted array of limited range
- Smallest element repeated exactly ‘k’ times (not limited to small range)
- Find if array can be sorted by swaps limited to multiples of k
- Sum of minimum and maximum elements of all subarrays of size k.
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Find maximum (or minimum) sum of a subarray of size k
- Minimum size Subarray with maximum sum in non-increasing order
- Smallest subarray of size greater than K with sum greater than a given value
- Maximum of minimum difference of all pairs from subsequences of given size
- Minimum array size after repeated replacement of even sum pair with sum
- First subarray having sum at least half the maximum sum of any subarray of size K
- Min difference between maximum and minimum element in all Y size subarrays

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.