📌  相关文章
📜  最多以K为总和来最大化Array中的元素数量

📅  最后修改于: 2021-05-19 17:36:49             🧑  作者: Mango

给定一个由N个整数和整数K组成的数组A [] ,任务是从该数组中选择总和最多为K的元素的最大数量。

例子:

天真的方法:想法是生成数组的所有可能的子序列,并找到所生成的所有子序列的元素之和。找到最大长度且总和小于或等于K的子序列。
时间复杂度: O(2 N )
辅助空间: (1)

高效方法:可以使用贪婪技术解决高效方法。步骤如下:

  1. 对给定的数组进行排序。
  2. 迭代数组并跟踪元素之和,直到总和小于或等于K为止。
  3. 如果在上述步骤中进行迭代时的总和超过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


输出:
3


时间复杂度: O(N * log N)
辅助空间: O(1)