给定一个由 N 个整数组成的数组A[]和一个整数K ,任务是从数组中选择最大数量的元素,其总和最多为K 。
例子:
Input: A[] = {1, 12, 5, 111, 200, 1000, 10}, K = 50
Output: 4
Explanation:
Maximum number of selections will be 1, 12, 5, 10 that is 1 + 12 + 5 + 10 = 28 < 50.
Input: A[] = {3, 7, 2, 9, 4}, K = 15
Output: 3
Explanation:
Maximum number of selections will be 3, 2, 4 that is 3 + 2 + 4 =9 < 15.
朴素的方法:想法是生成数组的所有可能的子序列,并找到所有生成的子序列的元素之和。找到最大长度且总和小于或等于K的子序列。
时间复杂度: O(2 N )
辅助空间: (1)
高效方法:可以使用贪婪技术解决有效方法。以下是步骤:
- 对给定的数组进行排序。
- 在数组中迭代并跟踪元素总和,直到总和小于或等于 K 。
- 如果在上述步骤中迭代时的总和超过K,则中断循环并打印 count 的值直到该索引。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to select a maximum number of
// elements in array whose sum is at most K
int maxSelections(int A[], int n, int k)
{
// Sort the array
sort(A, A + n);
// Calculate the sum and count while
// iterating the sorted array
int sum = 0;
int count = 0;
// Iterate for all the
// elements in the array
for (int i = 0; i < n; i++) {
// Add the current element to sum
sum = sum + A[i];
if (sum > k) {
break;
}
// Increment the count
count++;
}
// Return the answer
return count;
}
// Driver Code
int main()
{
// Given array
int A[] = { 3, 7, 2, 9, 4 };
// Given sum k
int k = 15;
int n = sizeof(A) / sizeof(A[0]);
// Function Call
cout << maxSelections(A, n, k);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to select a maximum number of
// elements in array whose sum is at most K
static int maxSelections(int A[], int n, int k)
{
// Sort the array
Arrays.sort(A);
// Calculate the sum and count while
// iterating the sorted array
int sum = 0;
int count = 0;
// Iterate for all the
// elements in the array
for(int i = 0; i < n; i++)
{
// Add the current element to sum
sum = sum + A[i];
if (sum > k)
{
break;
}
// Increment the count
count++;
}
// Return the answer
return count;
}
// Driver Code
public static void main(String[] args)
{
// Given array
int A[] = { 3, 7, 2, 9, 4 };
// Given sum k
int k = 15;
int n = A.length;
// Function call
System.out.print(maxSelections(A, n, k));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for
# the above approach
# Function to select a maximum
# number of elements in array
# whose sum is at most K
def maxSelections(A, n, k):
# Sort the array
A.sort();
# Calculate the sum and
# count while iterating
# the sorted array
sum = 0;
count = 0;
# Iterate for all the
# elements in the array
for i in range(n):
# Add the current element to sum
sum = sum + A[i];
if (sum > k):
break;
# Increment the count
count += 1;
# Return the answer
return count;
# Driver Code
if __name__ == '__main__':
# Given array
A = [3, 7, 2, 9, 4];
# Given sum k
k = 15;
n = len(A);
# Function call
print(maxSelections(A, n, k));
# This code is contributed by gauravrajput1
C#
// C# program for the above approach
using System;
class GFG{
// Function to select a maximum number of
// elements in array whose sum is at most K
static int maxSelections(int[] A, int n, int k)
{
// Sort the array
Array.Sort(A);
// Calculate the sum and count while
// iterating the sorted array
int sum = 0;
int count = 0;
// Iterate for all the
// elements in the array
for(int i = 0; i < n; i++)
{
// Add the current element to sum
sum = sum + A[i];
if (sum > k)
{
break;
}
// Increment the count
count++;
}
// Return the answer
return count;
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int[] A = { 3, 7, 2, 9, 4 };
// Given sum k
int k = 15;
int n = A.Length;
// Function call
Console.Write(maxSelections(A, n, k));
}
}
// This code is contributed by gauravrajput1
Javascript
输出:
3
时间复杂度: O(N*log N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。