📌  相关文章
📜  最大化可以选择的元素的数量,它们的总和与 K 之间的差异最小

📅  最后修改于: 2021-09-06 17:45:29             🧑  作者: Mango

给定一个数组arr[]和一个整数值K ,任务是计算可以选择的数组元素的最大数量,使得:

  • 所选数组元素的总和小于K
  • K –(所选元素的总和)大于或等于0并且是可能的最小值。

例子:

方法:解决这个问题的主要思想是使用贪心方法。请按照以下步骤解决此问题:

  • 对给定的数组进行排序。
  • 现在,从第一个数组元素开始,将当前元素添加到总和中。
  • 将计数增加 1。
  • 检查总和是否大于K。
    • 如果发现是真的,不要再选择任何元素。
    • 否则,重复上述过程,直到遍历完数组的最后一个元素。
  • 答案是所选元素的总数。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to count maximum number
// of elements that can be selected
int CountMaximum(int arr[], int n, int k)
{
 
    // Sort he array
    sort(arr, arr + n);
 
    int sum = 0, count = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
        // Add current element
        // to the sum
        sum += arr[i];
 
        // IF sum exceeds k
        if (sum > k)
            break;
 
        // Increment count
        count++;
    }
 
    // Return the count
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 30, 30, 10, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 50;
 
    cout << CountMaximum(arr, n, k);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.Arrays;
 
public class GFG {
    // Function to count maximum number
    // of elements that can be selected
    static int CountMaximum(int arr[], int n, int k)
    {
        // Sorting the array
        Arrays.sort(arr);
 
        int sum = 0, count = 0;
 
        // Traverse the array
        for (int i = 0; i < n; i++) {
            // Add the current
            // element to the sum
            sum += arr[i];
 
            // If sum exceeds k
            if (sum > k)
                break;
 
            // Increment count
            count++;
        }
 
        // Returning the count
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 30, 30, 10, 10 };
        int n = 4;
        int k = 50;
 
        // Function call
        System.out.println(
            CountMaximum(arr, n, k));
    }
}


Python3
# Python3 implementation of the above approach
 
# Function to count maximum number
# of elements that can be selected
def CountMaximum(arr, n, k) :
 
    # Sort he array
    arr.sort()
    Sum, count = 0, 0
 
    # Traverse the array
    for i in range(0, n) :
       
        # Add current element
        # to the sum
        Sum += arr[i]
 
        # IF sum exceeds k
        if (Sum > k) :
            break
 
        # Increment count
        count += 1
 
    # Return the count
    return count
 
  # Driver code
arr = [ 30, 30, 10, 10 ]
n = len(arr)
k = 50
 
print(CountMaximum(arr, n, k))
 
# This code is contributed by divyesh072019


C#
// C# implementation of the above approach
using System;
class GFG
{
 
  // Function to count maximum number
  // of elements that can be selected
  static int CountMaximum(int[] arr, int n, int k)
  {
    // Sorting the array
    Array.Sort(arr);
    int sum = 0, count = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
      // Add the current
      // element to the sum
      sum += arr[i];
 
      // If sum exceeds k
      if (sum > k)
        break;
 
      // Increment count
      count++;
    }
 
    // Returning the count
    return count;
  }
 
  // Driver code
  static public void Main()
  {
    int[] arr = new int[] { 30, 30, 10, 10 };
    int n = 4;
    int k = 50;
 
    // Function call
    Console.WriteLine(CountMaximum(arr, n, k));
  }
}
 
// This code is contributed by dharanendralv23


Javascript


C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to count maximum number
// of elements that can be selected
int CountMaximum(int arr[], int n, int k)
{
 
    // Sort the array
    sort(arr, arr + n);
 
    int sum = 0, count = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
        // Add current element
        // to the sum
        sum += arr[i];
 
        // IF sum exceeds k
        if (sum > k)
            break;
 
        // Increment count
        count++;
    }
 
    // Return the count
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 30, 30, 10, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 50;
 
    cout << CountMaximum(arr, n, k);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.Arrays;
 
public class GFG {
    // Function to count maximum number
    // of elements that can be selected
    static int CountMaximum(int arr[], int n, int k)
    {
        // Sorting the array
        Arrays.sort(arr);
 
        int sum = 0, count = 0;
 
        // Traverse the array
        for (int i = 0; i < n; i++) {
            // Add the current
            // element to the sum
            sum += arr[i];
 
            // If sum exceeds k
            if (sum > k)
                break;
 
            // Increment count
            count++;
        }
 
        // Returning the count
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 30, 30, 10, 10 };
        int n = 4;
        int k = 50;
 
        // Function call
        System.out.println(
            CountMaximum(arr, n, k));
    }
}


Python3
# Python3 implementation of the above approach
 
# Function to count maximum number
# of elements that can be selected
def CountMaximum(arr, n, k) :
 
    # Sort he array
    arr.sort()
    Sum, count = 0, 0
 
    # Traverse the array
    for i in range(0, n) :
       
        # Add current element
        # to the sum
        Sum += arr[i]
 
        # IF sum exceeds k
        if (Sum > k) :
            break
 
        # Increment count
        count += 1
 
    # Return the count
    return count
 
  # Driver code
arr = [ 30, 30, 10, 10 ]
n = len(arr)
k = 50
 
print(CountMaximum(arr, n, k))
 
# This code is contributed by divyesh072019


C#
// C# implementation of the above approach
using System;
class GFG
{
 
  // Function to count maximum number
  // of elements that can be selected
  static int CountMaximum(int[] arr, int n, int k)
  {
    // Sorting the array
    Array.Sort(arr);
    int sum = 0, count = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
      // Add the current
      // element to the sum
      sum += arr[i];
 
      // If sum exceeds k
      if (sum > k)
        break;
 
      // Increment count
      count++;
    }
 
    // Returning the count
    return count;
  }
 
  // Driver code
  static public void Main()
  {
    int[] arr = new int[] { 30, 30, 10, 10 };
    int n = 4;
    int k = 50;
 
    // Function call
    Console.WriteLine(CountMaximum(arr, n, k));
  }
}
 
// This code is contributed by dharanendralv23


Javascript


C++

// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to count maximum number
// of elements that can be selected
int CountMaximum(int arr[], int n, int k)
{
 
    // Sort the array
    sort(arr, arr + n);
 
    int sum = 0, count = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
        // Add current element
        // to the sum
        sum += arr[i];
 
        // IF sum exceeds k
        if (sum > k)
            break;
 
        // Increment count
        count++;
    }
 
    // Return the count
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 30, 30, 10, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 50;
 
    cout << CountMaximum(arr, n, k);
 
    return 0;
}

Java

// Java implementation of the above approach
import java.util.Arrays;
 
public class GFG {
    // Function to count maximum number
    // of elements that can be selected
    static int CountMaximum(int arr[], int n, int k)
    {
        // Sorting the array
        Arrays.sort(arr);
 
        int sum = 0, count = 0;
 
        // Traverse the array
        for (int i = 0; i < n; i++) {
            // Add the current
            // element to the sum
            sum += arr[i];
 
            // If sum exceeds k
            if (sum > k)
                break;
 
            // Increment count
            count++;
        }
 
        // Returning the count
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 30, 30, 10, 10 };
        int n = 4;
        int k = 50;
 
        // Function call
        System.out.println(
            CountMaximum(arr, n, k));
    }
}

蟒蛇3

# Python3 implementation of the above approach
 
# Function to count maximum number
# of elements that can be selected
def CountMaximum(arr, n, k) :
 
    # Sort he array
    arr.sort()
    Sum, count = 0, 0
 
    # Traverse the array
    for i in range(0, n) :
       
        # Add current element
        # to the sum
        Sum += arr[i]
 
        # IF sum exceeds k
        if (Sum > k) :
            break
 
        # Increment count
        count += 1
 
    # Return the count
    return count
 
  # Driver code
arr = [ 30, 30, 10, 10 ]
n = len(arr)
k = 50
 
print(CountMaximum(arr, n, k))
 
# This code is contributed by divyesh072019

C#

// C# implementation of the above approach
using System;
class GFG
{
 
  // Function to count maximum number
  // of elements that can be selected
  static int CountMaximum(int[] arr, int n, int k)
  {
    // Sorting the array
    Array.Sort(arr);
    int sum = 0, count = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
      // Add the current
      // element to the sum
      sum += arr[i];
 
      // If sum exceeds k
      if (sum > k)
        break;
 
      // Increment count
      count++;
    }
 
    // Returning the count
    return count;
  }
 
  // Driver code
  static public void Main()
  {
    int[] arr = new int[] { 30, 30, 10, 10 };
    int n = 4;
    int k = 50;
 
    // Function call
    Console.WriteLine(CountMaximum(arr, n, k));
  }
}
 
// This code is contributed by dharanendralv23

Javascript


输出:
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live