Maximum and minimum count of elements with sum at most K
Last Updated :
12 Oct, 2022
Given an array arr[] of size N and an integer K, the task is to find the maximum and minimum number of elements whose sum is less than equal to K.
Examples:
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
Approach: This problem can be solved by sorting the array arr[]. Follow the steps below to solve this problem:
- Sort the array arr[].
- Initialize variable maxNumEle and minNumEle as 0 to store minimum and maximum number of elements whose sum is less than equal to K.
- Initialize a variable cumSum1 to store the cumulative sum of the array arr[].
- Iterate in the range[0, N-1], using the variable i:
- Add current element in cumSum1.
- If cumSum1 is less than equal K, then increment by 1 in maxNumEle, Otherwise break the loop.
- Initialize a variable cumSum2 to store the cumulative sum of the array arr[].
- Iterate in the range[N-1, 0], using the variable i:
- Add current element in cumSum2.
- If cumSum2 is less than equal K, then increment by 1 in minNumEle, Otherwise break the loop.
- After completing the above steps, print the maxNumEle and minNumEle as the answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
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
<script>
// Javascript program for the above approach
// Function to find the maximum
// and minimum number of elements
// whose sum is less than equal
// to K
function 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
let maxNumEle = 0;
let minNumEle = 0;
// Store the cumulative sum
let 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
let 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
document.write(maxNumEle + " " + minNumEle);
}
// Driver Code
// Given Input
let N = 4;
let K = 7;
let arr = [6, 2, 1, 3];
// Function Call
findMinMax(arr, N, K);
// This code is contributed by gfgking
</script>
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Similar Reads
Minimum count of elements that sums to a given number Given infinite number of elements of form 10^n and 25*100^n ( n >= 0 ). The task is to find the minimum count of elements chosen such that there sum is equal to K. Examples: Input : K = 48 Output : 6 elements chosen are: (1 + 1 + 1 + 10 + 10 + 25)Input : 69 Output : 9 elements chosen are: (1 + 1
7 min read
Maximize number of elements from Array with sum at most K Given an array A[] of N integers and an integer K, the task is to select the maximum number of elements from the array whose sum is at most K. Examples: Input: A[] = {1, 12, 5, 111, 200, 1000, 10}, K = 50 Output: 4 Explanation: Maximum number of selections will be 1, 12, 5, 10 that is 1 + 12 + 5 + 1
6 min read
Count of ways to choose K elements from given Array with maximum sum Given an array, arr[] of size N and an integer K, the task is to find the number of ways of selecting K array elements, such that the sum of these K elements is the maximum possible sum. Examples: Input: arr[] = {3, 1, 1, 2}, K = 3 Output: 2Explanation: The possible ways of selecting 3 elements are:
8 min read
Minimize the maximum of Array by replacing any element with other element at most K times Given an array arr[] of size N and an integer K, the task is to minimize the value of the maximum element of the array arr[] after replacing any element of the array with any other element of that array at most K times.Examples:Input: arr[] = {5, 3, 3, 2, 1}, K = 3Output: 2Explanation: Replace the e
8 min read
Maximize the Sum of Minimum in each Group of size K Given an array nums[] of size N and a positive integer K, divide the elements of the array into N/K groups, each of size K such that the sum of the smallest elements in each group is maximized. Examples: Input: nums = {1,4,3,2}, K = 2Output: 4Explanation: All possible groupings (ignoring the orderin
8 min read