Maximize count of elements that can be selected having minimum difference between their sum and K
Last Updated :
08 Mar, 2022
Given an array arr[] and an integer value K, the task is to count the maximum number of array elements that can be selected such that:
- Sum of the selected array elements is less than K.
- K – (total sum of selected elements) is greater than or equal to 0 and minimum possible.
Examples:
Examples:
Input: arr[] = {20, 90, 40, 90}, K = 100
Output: 2
Explanation: {20, 40} are selected such that their sum ( = 60) is less than K and K – total sum ( = 40) is greater than 0 and minimum possible.
Examples:
Input: arr[] = {30, 30, 10, 10}, K = 50
Output: 3
Explanation: {10, 10, 30} are selected< such that their sum ( = 50) is equal to K and K – total sum is 0 and minimum possible.
Approach: The main idea to solve this problem is to use a Greedy Approach. Follow the steps below to solve this problem:
- Sort the given array.
- Now, starting from the first array element, add the current element to the sum.
- Increment count by 1.
- Check if the sum is greater than the K or not.
- If found to be true, don’t select any more elements.
- Otherwise, repeat the above procedure until the last element of the array has been traversed.
- The answer is the total number of elements selected.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CountMaximum( int arr[], int n, int k)
{
sort(arr, arr + n);
int sum = 0, count = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
if (sum > k)
break ;
count++;
}
return count;
}
int main()
{
int arr[] = { 30, 30, 10, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 50;
cout << CountMaximum(arr, n, k);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static int CountMaximum( int arr[], int n, int k)
{
Arrays.sort(arr);
int sum = 0 , count = 0 ;
for ( int i = 0 ; i < n; i++) {
sum += arr[i];
if (sum > k)
break ;
count++;
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 30 , 30 , 10 , 10 };
int n = 4 ;
int k = 50 ;
System.out.println(
CountMaximum(arr, n, k));
}
}
|
Python3
def CountMaximum(arr, n, k) :
arr.sort()
Sum , count = 0 , 0
for i in range ( 0 , n) :
Sum + = arr[i]
if ( Sum > k) :
break
count + = 1
return count
arr = [ 30 , 30 , 10 , 10 ]
n = len (arr)
k = 50
print (CountMaximum(arr, n, k))
|
C#
using System;
class GFG
{
static int CountMaximum( int [] arr, int n, int k)
{
Array.Sort(arr);
int sum = 0, count = 0;
for ( int i = 0; i < n; i++)
{
sum += arr[i];
if (sum > k)
break ;
count++;
}
return count;
}
static public void Main()
{
int [] arr = new int [] { 30, 30, 10, 10 };
int n = 4;
int k = 50;
Console.WriteLine(CountMaximum(arr, n, k));
}
}
|
Javascript
<script>
function CountMaximum(arr, n, k)
{
arr.sort();
let sum = 0, count = 0;
for (let i = 0; i < n; i++) {
sum += arr[i];
if (sum > k)
break ;
count++;
}
return count;
}
let arr = [ 30, 30, 10, 10 ];
let n = 4;
let k = 50;
document.write(
CountMaximum(arr, n, k));
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(1)
C++
#include <bits/stdc++.h>
using namespace std;
int CountMaximum( int arr[], int n, int k)
{
sort(arr, arr + n);
int sum = 0, count = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
if (sum > k)
break ;
count++;
}
return count;
}
int main()
{
int arr[] = { 30, 30, 10, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 50;
cout << CountMaximum(arr, n, k);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static int CountMaximum( int arr[], int n, int k)
{
Arrays.sort(arr);
int sum = 0 , count = 0 ;
for ( int i = 0 ; i < n; i++) {
sum += arr[i];
if (sum > k)
break ;
count++;
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 30 , 30 , 10 , 10 };
int n = 4 ;
int k = 50 ;
System.out.println(
CountMaximum(arr, n, k));
}
}
|
Python3
def CountMaximum(arr, n, k) :
arr.sort()
Sum , count = 0 , 0
for i in range ( 0 , n) :
Sum + = arr[i]
if ( Sum > k) :
break
count + = 1
return count
arr = [ 30 , 30 , 10 , 10 ]
n = len (arr)
k = 50
print (CountMaximum(arr, n, k))
|
C#
using System;
class GFG
{
static int CountMaximum( int [] arr, int n, int k)
{
Array.Sort(arr);
int sum = 0, count = 0;
for ( int i = 0; i < n; i++)
{
sum += arr[i];
if (sum > k)
break ;
count++;
}
return count;
}
static public void Main()
{
int [] arr = new int [] { 30, 30, 10, 10 };
int n = 4;
int k = 50;
Console.WriteLine(CountMaximum(arr, n, k));
}
}
|
Javascript
<script>
function CountMaximum(arr, n, k)
{
arr.sort( function (a, b){ return a - b});
let sum = 0, count = 0;
for (let i = 0; i < n; i++) {
sum += arr[i];
if (sum > k)
break ;
count++;
}
return count;
}
let arr = [ 30, 30, 10, 10 ];
let n = arr.length;
let k = 50;
document.write(CountMaximum(arr, n, k));
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...