📌  相关文章
📜  从一个数组中可能的最大删除,使其元素总和大于或等于另一个数组的元素总和

📅  最后修改于: 2021-10-26 06:40:05             🧑  作者: Mango

给定两个阵列的常用3 []BRR [分别的尺寸NM],任务是计数,可以从阵列ARR被去除的元素的最大数量[],使得元件在ARR总和[]大于或等于brr[]中元素的总和。

例子:

方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:

  • 按升序对数组 arr[] 进行排序。
  • 从阵列ARR []和检查除去的最小元素,如果ARR的元素之和[]是大于或等于总结数组元素BRR []与否。如果发现为真,则增加计数。
  • 最后,打印获得的计数。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to maximize the count of elements
// required to be removed from arr[] such that
// the sum of arr[] is greater than or equal
// to sum of the array brr[]
int maxCntRemovedfromArray(int arr[], int N,
                           int brr[], int M)
{
 
    // Sort the array arr[]
    sort(arr, arr + N);
 
    // Stores index of smallest
    // element of arr[]
    int i = 0;
 
    // Stores sum of elements
    // of the array arr[]
    int sumArr = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // Update sumArr
        sumArr += arr[i];
    }
 
    // Stores sum of elements
    // of the array brr[]
    int sumBrr = 0;
 
    // Traverse the array brr[]
    for (int i = 0; i < M; i++) {
 
        // Update sumArr
        sumBrr += brr[i];
    }
 
    // Stores count of
    // removed elements
    int cntRemElem = 0;
 
    // Repeatedly remove the smallest
    // element of arr[]
    while (i < N and sumArr >= sumBrr) {
 
        // Update sumArr
        sumArr -= arr[i];
 
        // Remove the smallest element
        i += 1;
 
        // If the sum of remaining elements
        // in arr[] >= sum of brr[]
        if (sumArr >= sumBrr) {
 
            // Update cntRemElem
            cntRemElem += 1;
        }
    }
 
    return cntRemElem;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 2, 4, 6 };
 
    int brr[] = { 7 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    int M = sizeof(brr) / sizeof(brr[0]);
 
    cout << maxCntRemovedfromArray(arr, N, brr, M);
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG
{
 
    // Function to maximize the count of elements
    // required to be removed from arr[] such that
    // the sum of arr[] is greater than or equal
    // to sum of the array brr[]
    static int maxCntRemovedfromArray(int[] arr, int N,
                                      int[] brr, int M)
    {
 
        // Sort the array arr[]
        Arrays.sort(arr);
 
        // Stores index of smallest
        // element of arr[]
        int i = 0;
 
        // Stores sum of elements
        // of the array arr[]
        int sumArr = 0;
 
        // Traverse the array arr[]
        for (i = 0; i < N; i++)       
        {
 
            // Update sumArr
            sumArr += arr[i];
        }
 
        // Stores sum of elements
        // of the array brr[]
        int sumBrr = 0;
 
        // Traverse the array brr[]
        for (i = 0; i < M; i++)
        {
 
            // Update sumArr
            sumBrr += brr[i];
        }
 
        // Stores count of
        // removed elements
        int cntRemElem = 0;
 
        // Repeatedly remove the smallest
        // element of arr[]
        while (i < N && sumArr >= sumBrr) {
 
            // Update sumArr
            sumArr -= arr[i];
 
            // Remove the smallest element
            i += 1;
 
            // If the sum of remaining elements
            // in arr[] >= sum of brr[]
            if (sumArr >= sumBrr) {
 
                // Update cntRemElem
                cntRemElem += 1;
            }
        }
        return cntRemElem;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int[] arr = new int[] { 1, 2, 4, 6 };
        int[] brr = new int[] { 7 };
        int N = arr.length;
        int M = brr.length;
        System.out.println(
            maxCntRemovedfromArray(arr, N, brr, M));
    }
}
 
// This code is contributed by Dharanendra L V


Python3
# Python3 program to implement
# the above approach
 
# Function to maximize the count of elements
# required to be removed from arr[] such that
# the sum of arr[] is greater than or equal
# to sum of the array brr[]
def maxCntRemovedfromArray(arr, N, brr, M):
     
    # Sort the array arr[]
    arr.sort(reverse = False)
 
    # Stores index of smallest
    # element of arr[]
    i = 0
 
    # Stores sum of elements
    # of the array arr[]
    sumArr = 0
 
    # Traverse the array arr[]
    for i in range(N):
         
        # Update sumArr
        sumArr += arr[i]
 
    # Stores sum of elements
    # of the array brr[]
    sumBrr = 0
 
    # Traverse the array brr[]
    for i in range(M):
 
        # Update sumArr
        sumBrr += brr[i]
 
    # Stores count of
    # removed elements
    cntRemElem = 0
 
    # Repeatedly remove the smallest
    # element of arr[]
    while (i < N and sumArr >= sumBrr):
         
        # Update sumArr
        sumArr -= arr[i]
 
        # Remove the smallest element
        i += 1
 
        # If the sum of remaining elements
        # in arr[] >= sum of brr[]
        if (sumArr >= sumBrr):
             
            # Update cntRemElem
            cntRemElem += 1
 
    return cntRemElem
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 4, 6 ]
 
    brr = [7]
 
    N = len(arr)
 
    M = len(brr)
 
    print(maxCntRemovedfromArray(arr, N, brr, M))
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
    // Function to maximize the count of elements
    // required to be removed from arr[] such that
    // the sum of arr[] is greater than or equal
    // to sum of the array brr[]
    static int maxCntRemovedfromArray(int[] arr, int N,
                                      int[] brr, int M)
    {
 
        // Sort the array arr[]
        Array.Sort(arr);
 
        // Stores index of smallest
        // element of arr[]
        int i = 0;
 
        // Stores sum of elements
        // of the array arr[]
        int sumArr = 0;
 
        // Traverse the array arr[]
        for (i = 0; i < N; i++)
        {
 
            // Update sumArr
            sumArr += arr[i];
        }
 
        // Stores sum of elements
        // of the array brr[]
        int sumBrr = 0;
 
        // Traverse the array brr[]
        for (i = 0; i < M; i++)
        {
 
            // Update sumArr
            sumBrr += brr[i];
        }
 
        // Stores count of
        // removed elements
        int cntRemElem = 0;
 
        // Repeatedly remove the smallest
        // element of arr[]
        while (i < N && sumArr >= sumBrr)
        {
 
            // Update sumArr
            sumArr -= arr[i];
 
            // Remove the smallest element
            i += 1;
 
            // If the sum of remaining elements
            // in arr[] >= sum of brr[]
            if (sumArr >= sumBrr)
            {
 
                // Update cntRemElem
                cntRemElem += 1;
            }
        }
        return cntRemElem;
    }
 
    // Driver Code
    static public void Main()
    {
        int[] arr = new int[] { 1, 2, 4, 6 };
        int[] brr = new int[] { 7 };
        int N = arr.Length;
        int M = brr.Length;
        Console.WriteLine(
            maxCntRemovedfromArray(arr, N, brr, M));
    }
}
 
// This code is contributed by Dharanendra L V


Javascript


输出:
2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程