Minimum partitions of maximum size 2 and sum limited by given value
Last Updated :
28 Feb, 2023
Given an array arr[] of positive numbers, find minimum number of sets in array which satisfy following property,
- A set can contain maximum two elements in it. The two elements need not to be contiguous.
- Sum of elements of set should be less than 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;
}
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.
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
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.
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
?>
JavaScript
<script>
// Javascript program to count minimum number of partitions
// of size 2 and sum smaller than or equal to given
// key.
function minimumSets(arr, n, key)
{
var i, j;
// sort the array
arr.sort((a,b)=> a-b)
// 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;
}
var arr = [3, 5, 3, 4];
var n = arr.length;
var key = 5;
document.write( minimumSets(arr, n, key));
</script>
Complexity Analysis:
- Time complexity: O(nlogn)
- Auxiliary Space: O(1)
Similar Reads
Maximize the Sum of Minimum in each Group of size K Given an array nums[] of size N and a positive integer K, divide the elements of the array into N/K groups, each of size K such that the sum of the smallest elements in each group is maximized. Examples: Input: nums = {1,4,3,2}, K = 2Output: 4Explanation: All possible groupings (ignoring the orderin
8 min read
Minimum sum by choosing minimum of pairs from array Given an array A[] of n-elements. We need to select two adjacent elements and delete the larger of them and store smaller of them to another array say B[]. We need to perform this operation till array A[] contains only single element. Finally, we have to construct the array B[] in such a way that to
4 min read
Find minimum and maximum number of terms to divide N as sum of 4 or 6 Given an integer N, the task is to find the minimum and the maximum number of terms required to divide N as the sum of 4 or 6. Examples: Input: N = 3Output: NOT POSSIBLEExplanation: As the number is less than 4, it is not possible. Input: N = 10 Output : Minimum Terms = 2, Maximum Terms = 2Explanati
8 min read
Find the maximum possible value of the minimum value of modified array Given an array of size N and a number S . The task is to modify the given array such that: The difference between the sum of the array elements before and after modification is exactly equal to S.Modified array elements should be non-negative.The minimum value in the modified array must be maximized
9 min read
Minimum operations to make Array sum at most S from given Array Given an array arr[], of size N and an integer S, the task is to find the minimum operations to make the sum of the array less than or equal to S. In each operation: Any element can be chosen and can be decremented by 1, orCan be replaced by any other element in the array. Examples: Input: arr[]= {1
10 min read