给定两个阵列的常用3 []和BRR [分别的尺寸N和M],任务是计数,可以从阵列ARR被去除的元素的最大数量[],使得元件在ARR总和[]大于或等于brr[]中元素的总和。
例子:
Input: arr[] = { 1, 2, 4, 6 }, brr[] = { 7 }
Output: 2
Explanation:
Removing arr[2] modifies arr[] to { 1, 2, 6 } and sum equal to 9 (> 7)
Removing arr[1] modifies arr[] to { 1, 6 } and sum equal to 7 (&gr; 7)
Removing arr[0] modifies arr[] to { 6 } and sum equal to 6 (< 7)
Therefore, the required output is 2.
Input: arr[] = { 10, 20 }, brr[] = { 5 }
Output: 1
Explanation:
Removing arr[1] modifies arr[] to { 10 } and sum equal to 10 (> 5)
Removing arr[0] modifies arr[] to { } and sum equal to 0 (< 5)
Therefore, the required output is 1.
方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:
- 按升序对数组 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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live