📌  相关文章
📜  最大化可以选择的元素的总数与K之差最小的元素数

📅  最后修改于: 2021-05-17 20:29:50             🧑  作者: 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


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));
    }
}

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

Java脚本


输出:
3

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