给定一个数组arr []为正数,请找到满足以下属性的数组中的最小集合数:
1.一个集合中最多可以包含两个元素。这两个元素不必是连续的。
2. set元素的总和应小于或等于给定的Key。可以假定给定键大于或等于最大数组元素。
例子:
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)
这个想法是先对数组排序,然后遵循两个指针的方法。我们从排序数组的两个角开始两个指针。如果它们的总和小于或等于给定的键,则我们对它们进行设置,否则我们仅考虑最后一个元素。
下面是上述方法的实现:
C++
// C++ program to count minimum number of partitions
// of size 2 and sum smaller than or equal to given
// key.
#include
#include
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
输出:
4
时间复杂度: O(nlogn)