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++
#include <bits/stdc++.h>
using namespace std;
int findMinMax( int arr[], int N, int K)
{
sort(arr, arr + N);
int maxNumEle = 0;
int minNumEle = 0;
int i, cumSum1 = 0;
for (i = 0; i < N; i++) {
cumSum1 += arr[i];
if (cumSum1 <= K)
maxNumEle += 1;
else
break ;
}
int cumSum2 = 0;
for (i = N - 1; i >= 0; i--) {
cumSum2 += arr[i];
if (cumSum2 <= K)
minNumEle += 1;
else
break ;
}
cout << maxNumEle << " " << minNumEle;
}
int main()
{
int N = 4;
int K = 7;
int arr[] = { 6, 2, 1, 3 };
findMinMax(arr, N, K);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static void findMinMax( int arr[], int N, int K)
{
Arrays.sort(arr);
int maxNumEle = 0 ;
int minNumEle = 0 ;
int i, cumSum1 = 0 ;
for (i = 0 ; i < N; i++)
{
cumSum1 += arr[i];
if (cumSum1 <= K)
maxNumEle += 1 ;
else
break ;
}
int cumSum2 = 0 ;
for (i = N - 1 ; i >= 0 ; i--)
{
cumSum2 += arr[i];
if (cumSum2 <= K)
minNumEle += 1 ;
else
break ;
}
System.out.println(maxNumEle + " " + minNumEle);
}
public static void main(String[] args)
{
int N = 4 ;
int K = 7 ;
int arr[] = { 6 , 2 , 1 , 3 };
findMinMax(arr, N, K);
}
}
|
Python3
def findMinMax(arr, N, K):
arr.sort()
maxNumEle = minNumEle = 0
cumSum1 = 0
for i in range (N):
cumSum1 + = arr[i]
if cumSum1 < = K:
maxNumEle + = 1
else :
break
cumSum2 = 0
for i in range (N - 1 , 0 , - 1 ):
cumSum2 + = arr[i]
if cumSum2 < = K:
minNumEle + = 1
else :
break
print (maxNumEle, minNumEle)
if __name__ = = '__main__' :
N = 4
K = 7
arr = [ 6 , 2 , 1 , 3 ]
findMinMax(arr, N, K)
|
C#
using System;
class GFG{
static void findMinMax( int [] arr, int N, int K)
{
Array.Sort(arr);
int maxNumEle = 0;
int minNumEle = 0;
int i, cumSum1 = 0;
for (i = 0; i < N; i++)
{
cumSum1 += arr[i];
if (cumSum1 <= K)
maxNumEle += 1;
else
break ;
}
int cumSum2 = 0;
for (i = N - 1; i >= 0; i--)
{
cumSum2 += arr[i];
if (cumSum2 <= K)
minNumEle += 1;
else
break ;
}
Console.WriteLine(maxNumEle + " " + minNumEle);
}
static public void Main()
{
int N = 4;
int K = 7;
int [] arr = { 6, 2, 1, 3 };
findMinMax(arr, N, K);
}
}
|
Javascript
<script>
function findMinMax(arr, N, K) {
arr.sort();
let maxNumEle = 0;
let minNumEle = 0;
let i,
cumSum1 = 0;
for (i = 0; i < N; i++) {
cumSum1 += arr[i];
if (cumSum1 <= K) maxNumEle += 1;
else break ;
}
let cumSum2 = 0;
for (i = N - 1; i >= 0; i--) {
cumSum2 += arr[i];
if (cumSum2 <= K) minNumEle += 1;
else break ;
}
document.write(maxNumEle + " " + minNumEle);
}
let N = 4;
let K = 7;
let arr = [6, 2, 1, 3];
findMinMax(arr, N, K);
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...