Minimum partitions of maximum size 2 and sum limited by given value | C ++ program to count minimum number of partitions of size 2 and sum smaller than or equal to given key . ; sort the array ; 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 ; After ending of loop i will contain minimum number of sets ; Driver Code
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int minimumSets ( int arr [ ] , int n , int key ) { int i , j ; sort ( arr , arr + n ) ; for ( i = 0 , j = n - 1 ; i <= j ; ++ i ) if ( arr [ i ] + arr [ j ] <= key ) j -- ; 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 ; }