总和最多为 K 的元素的最大和最小计数
给定一个大小为N的数组arr[]和一个整数K ,任务是找到总和小于等于K的元素的最大和最小数量。
例子:
Input: N = 4, arr[ ] = {6, 2, 1, 3}, K = 7
Output: 3 1
Explanation:
Maximum number of elements whose sum is less than equal to K is 3 i.e [1, 2, 3]
Minimum number of elements whose sum is less than equal to K is 1 i.e [6]
Input: N = 5, arr[] = {6, 2, 11, 3, 20}, K = 50
Output: 5 5
方法:这个问题可以通过对数组arr[]进行排序来解决。请按照以下步骤解决此问题:
- 对数组 arr[] 进行排序。
- 将变量maxNumEle和minNumEle初始化为0以存储总和小于等于K的元素的最小和最大数量。
- 初始化一个变量cumSum1来存储数组arr[] 的累积和。
- 使用变量i在[0, N-1]范围内迭代:
- 在cumSum1中添加当前元素。
- 如果cumSum1小于等于K,则在maxNumEle中增加1 ,否则中断循环。
- 初始化变量cumSum2以存储数组arr[]的累积和。
- 使用变量i在[N-1, 0]范围内迭代:
- 在cumSum2中添加当前元素。
- 如果cumSum2小于等于K ,则在minNumEle中增加1 ,否则中断循环。
- 完成上述步骤后,打印maxNumEle和minNumEle作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum
// and minimum number of elements
// whose sum is less than equal
// to K
int findMinMax(int arr[], int N, int K)
{
// Sorting both arrays
sort(arr, arr + N);
// To store the minimum and maximum
// number of elements whose sum is
// less than equal to K
int maxNumEle = 0;
int minNumEle = 0;
// Store the cumulative sum
int i, cumSum1 = 0;
// Iterate in the range [0, N-1]
for (i = 0; i < N; i++) {
cumSum1 += arr[i];
// If cumSum1 is less than K
if (cumSum1 <= K)
maxNumEle += 1;
else
break;
}
// Store the cumulative sum
int cumSum2 = 0;
// Iterate in the range [N-1, 0]
for (i = N - 1; i >= 0; i--) {
cumSum2 += arr[i];
// If cumSum2 is less than K
if (cumSum2 <= K)
minNumEle += 1;
else
break;
}
// Print the value of maxNumEle and minNumEle
cout << maxNumEle << " " << minNumEle;
}
// Driver Code
int main()
{
// Given Input
int N = 4;
int K = 7;
int arr[] = { 6, 2, 1, 3 };
// Function Call
findMinMax(arr, N, K);
return 0;
}
// This code is contributed by Potta Lokesh
Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the maximum
// and minimum number of elements
// whose sum is less than equal
// to K
static void findMinMax(int arr[], int N, int K)
{
// Sorting both arrays
Arrays.sort(arr);
// To store the minimum and maximum
// number of elements whose sum is
// less than equal to K
int maxNumEle = 0;
int minNumEle = 0;
// Store the cumulative sum
int i, cumSum1 = 0;
// Iterate in the range [0, N-1]
for(i = 0; i < N; i++)
{
cumSum1 += arr[i];
// If cumSum1 is less than K
if (cumSum1 <= K)
maxNumEle += 1;
else
break;
}
// Store the cumulative sum
int cumSum2 = 0;
// Iterate in the range [N-1, 0]
for(i = N - 1; i >= 0; i--)
{
cumSum2 += arr[i];
// If cumSum2 is less than K
if (cumSum2 <= K)
minNumEle += 1;
else
break;
}
// Print the value of maxNumEle and minNumEle
System.out.println(maxNumEle + " " + minNumEle);
}
// Driver code
public static void main(String[] args)
{
// Given Input
int N = 4;
int K = 7;
int arr[] = { 6, 2, 1, 3 };
// Function Call
findMinMax(arr, N, K);
}
}
// This code is contributed by sanjoy_62
Python3
# Python program for the above approach
# Function to find the maximum
# and minimum number of elements
# whose sum is less than equal
# to K
def findMinMax(arr, N, K):
# Sorting both arrays
arr.sort()
# To store the minimum and maximum
# number of elements whose sum is
# less than equal to K
maxNumEle = minNumEle = 0
# Store the cumulative sum
cumSum1 = 0
# Iterate in the range [0, N-1]
for i in range(N):
cumSum1 += arr[i]
# If cumSum1 is less than K
if cumSum1 <= K:
maxNumEle += 1
else:
break
# Store the cumulative sum
cumSum2 = 0
# Iterate in the range [N-1, 0]
for i in range(N-1, 0, -1):
cumSum2 += arr[i]
# If cumSum2 is less than K
if cumSum2 <= K:
minNumEle += 1
else:
break
# Print the value of maxNumEle and minNumEle
print(maxNumEle, minNumEle)
# Driver Code
if __name__ == '__main__':
# Given Input
N = 4
K = 7
arr = [ 6, 2, 1, 3 ]
# Function Call
findMinMax(arr, N, K)
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the maximum
// and minimum number of elements
// whose sum is less than equal
// to K
static void findMinMax(int[] arr, int N, int K)
{
// Sorting both arrays
Array.Sort(arr);
// To store the minimum and maximum
// number of elements whose sum is
// less than equal to K
int maxNumEle = 0;
int minNumEle = 0;
// Store the cumulative sum
int i, cumSum1 = 0;
// Iterate in the range [0, N-1]
for(i = 0; i < N; i++)
{
cumSum1 += arr[i];
// If cumSum1 is less than K
if (cumSum1 <= K)
maxNumEle += 1;
else
break;
}
// Store the cumulative sum
int cumSum2 = 0;
// Iterate in the range [N-1, 0]
for(i = N - 1; i >= 0; i--)
{
cumSum2 += arr[i];
// If cumSum2 is less than K
if (cumSum2 <= K)
minNumEle += 1;
else
break;
}
// Print the value of maxNumEle and minNumEle
Console.WriteLine(maxNumEle + " " + minNumEle);
}
// Driver code
static public void Main()
{
// Given Input
int N = 4;
int K = 7;
int[] arr = { 6, 2, 1, 3 };
// Function Call
findMinMax(arr, N, K);
}
}
// This code is contributed by target_2
Javascript
输出
3 1
时间复杂度: O(NlogN)
辅助空间: O(N)