Given an array arr[] containing N elements, the task is to find the size of the largest subset for each array element arr[i] such that the sum of the subset is less than that element.
Examples:
Input: arr[] = { 5, 2, 1, 1, 1, 6, 8}
Output: 3 1 0 0 0 4 4
Explanation:
For i = 0 > subset = {1, 1, 1}, sum = 3. No other larger subset has sum less than 5.
For i = 1 > subset = {1}. Sum = 1 which is less than arr[1] = 2
For i = 2 > subset = {}. No element with value less than 1 present in array.
For i = 3 and i = 4 also subsets will have no element.
For i = 5 > subset = {2, 1, 1, 1}, sum = 5 which is less than arr[5] = 6 and largest in size.
For i = 6 > subset = {2, 1, 1, 1}, sum = 5 which is less than arr[6] = 8 and largest in size.Input: arr[] = { 2, 1, 4, 5, 3, 2, 1 }
Output: 1 0 2 3 2 1 0
Naive Approach: The simplest way to solve the problem is to form all the subsets for each arr[i] and find the largest one with a sum less than that element among all the subsets.
Time complexity: O(N*2^{N})
Auxiliary Space: O(2^{N})
Better Approach: A better approach to solve the problem is by using the Greedy method based on the following idea.
Make a copy of the actual array and sort the duplicate. After that for each array element (arr[i]) traverse the duplicate array and find the maximum of how many elements from the start can be added to a subset such that the sum is less than arr[i].
Follow the steps mentioned below to solve the problem:
 Make a copy (say v) of the array.
 Sort the duplicate in increasing order.
 Traverse the array from i = 0 to N1:
 For each element traverse from the starting of the duplicate and:
 Check if adding the current element of the duplicate keeps the sum less than arr[i] or not.
 If the sum exceeds arr[i]break the loop.
 Otherwise, traverse the loop and increase the size of the subset.
 Add the subset size to the result array.
 For each element traverse from the starting of the duplicate and:
 Return the result array.
Below is the implementation of the above approach.
C++

Time complexity: O(N^{2})
Auxiliary Space: O(N)
Efficient Approach: The efficient approach is similar to the previous approach but it uses the concept of prefix sum and lower bound as mentioned here;
After sorting the duplicate array build a prefix sum array of the duplicate array.
For each element instead of iterating the duplicate array use lower bound in the prefix sum array to find the number of elements that can be included in a subset.
Follow the steps mentioned below:
 Build a duplicate array and sort the duplicate (say v).
 Build prefix array for v.
 Iterate from i = 0 to N1 in arr[]:
 Use lower bound in prefix array and find how many elements can be included in the subset.
 Add the subset size in the result array.
 Return the result array.
Below is the implementation of the above approach.
C++

Time complexity: O(N * log(N))
Auxiliary Space: O(N)