Largest Subset with sum at most K when one Array element can be halved
Given an array arr[] of size N and an integer K, the task is to find the size of the largest subset possible having a sum at most K when only one element can be halved (the halved value will be rounded to the closest greater integer).
Examples:
Input: arr[] = {4, 4, 5}, K = 15
Output: 3
Explanation: 4+4+5 = 13 which is less than 15. So subset can have all elements.
Input: arr[3] = {2, 3, 5}, K = 9
Output: 3
Explanation: 2 + 3 = 5 which is less than 9
2 + 3 + 5 = 10 which is greater than 9, So cannot be part of subset.
After halving i.e. ceil [5/2] = 3, sum = 2+3+3 = 8 which is less than 9.
So all 3 elements can be part of subset.
Input: arr[8] = {1, 2, 3, 4, 5, 6, 7, 8}, K = 20
Output: 6
Explanation: 1+2+3+4+5 = 15 which is less than 20
15 + 6 = 21 which is greater than 20.
After halving the value i.e. ceil [6/2] = 3
15 + 3 = 18 which is less than 20. So it can be part of subset.
Approach: The given problem can be solved using Sorting method based on the following idea:
In a sorted array keep on performing sum from i = 0 to N-1. If at any moment sum is greater than K, that element can only be included if after halving that value the sum is at most K
Follow the steps mentioned below to solve the problem:
- Sort the array in ascending order.
- Declare a variable (say Sum) to maintain the sum.
- Traverse the array from i = 0 to N-1:
- Add arr[i] to Sum.
- If Sum is greater than K, then make arr[i] = ceil(arr[i]/2) and check if that sum is less than K or not.
- If Sum is less than K then continue iteration.
- Increment the size of the subset.
- Return the size of the subset.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findcount( int arr[], int n, int m)
{
int ans = n, sum = 0, count = 0;
sort(arr, arr + n);
for ( int i = 0; i < n; i++) {
if (sum + (arr[i] + 1) / 2 > m) {
ans = i;
break ;
}
sum += arr[i];
count++;
}
return count;
}
int main()
{
int N = 8, K = 20;
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
cout << findcount(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int findcount( int arr[], int n, int m)
{
int ans = n, sum = 0 , count = 0 ;
Arrays.sort(arr);
for ( int i = 0 ; i < n; i++) {
if (sum + (arr[i] + 1 ) / 2 > m) {
ans = i;
break ;
}
sum += arr[i];
count++;
}
return count;
}
public static void main(String[] args)
{
int N = 8 , K = 20 ;
int arr[] = new int [] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
System.out.print(findcount(arr, N, K));
}
}
|
Python3
def findcount(n, k, arr):
arr.sort()
sum = 0
count = 0
for i in range (n):
if ( sum + (arr[i] + 1 ) / 2 > k):
ans = i
break
sum + = arr[i]
count = count + 1
return (count)
n = 8
k = 20
arr = [ 1 , 2 , 3 , 5 , 4 , 6 , 7 , 8 ]
result = findcount(n,k,arr)
print (result)
|
C#
using System;
class GFG {
static int findcount( int [] arr, int n, int m)
{
int ans = n, sum = 0, count = 0;
Array.Sort(arr);
for ( int i = 0; i < n; i++) {
if (sum + (arr[i] + 1) / 2 > m) {
ans = i;
break ;
}
sum += arr[i];
count++;
}
return count;
}
public static void Main()
{
int N = 8, K = 20;
int [] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };
Console.Write(findcount(arr, N, K));
}
}
|
Javascript
<script>
function findcount(arr, n, m)
{
var ans = n;
var sum = 0;
var count = 0;
arr.sort();
for ( var i = 0; i < n; i++) {
if (sum + Math.floor((arr[i] + 1) / 2) > m) {
ans = i;
break ;
}
sum += arr[i];
count++;
}
return count;
}
var N = 8;
var K = 20;
var arr = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
document.write(findcount(arr, N, K));
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Last Updated :
08 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...