Open In App

Maximum non overlapping Subset with sum greater than K

Given an array, arr[] and integer K, the task is to find the maximum number of non-overlapping subsets such that the sum of the subsets is strictly greater than K when you may also change the value of each element to the maximum value of the particular subset.

Examples:



Input: arr[]= {90, 80, 70, 60, 50, 100}, K = 180
Output: 2

Input: arr[] = {3, 2, 1}, K = 8
Output: 1



Approach: To solve the problem follow the below idea:

In order to form a maximum number of subsets we need to form a sequence with a minimum number of elements as well as the sum of values of elements should be greater than K. We start forming subsequences with elements having maximum value and changing the rest of the element value to the maximum value in the same subsequence.

Follow the steps mentioned below to implement the idea:  

Below is the implementation of the above approach:




// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function for finding out maximum
// subsequences formed
int maximumSequence(vector<int>& arr, int K, int n)
{
 
    // Sorting array in non-increasing order
    sort(arr.begin(), arr.end(), greater<int>());
 
    int i = 0;
    int sequence_formed = 0;
    int elements_required = 0;
 
    while (elements_required + (K / arr[i]) + 1 <= n) {
 
        // Increasing Sequence formed
        sequence_formed += 1;
 
        // After forming of a new sequence,
        // increasing elements used who have
        // assigned to any of the subsequence
        elements_required += ((K / arr[i]) + 1);
 
        i++;
    }
 
    return sequence_formed;
}
 
// Driver code
int main()
{
    vector<int> power = { 90, 80, 70, 60, 50, 100 };
    int n = power.size();
 
    int k = 180;
 
    // Function call
    cout << maximumSequence(power, k, n);
}




def maximum_sequence(arr, K, n):
    # Sorting array in non-increasing order
    arr.sort(reverse=True)
 
    i = 0
    sequence_formed = 0
    elements_required = 0
 
    while elements_required + (K // arr[i]) + 1 <= n:
        # Increasing Sequence formed
        sequence_formed += 1
 
        # After forming of a new sequence,
        # increasing elements used who have
        # assigned to any of the subsequence
        elements_required += ((K // arr[i]) + 1)
 
        i += 1
 
    return sequence_formed
 
# Driver code
if __name__ == "__main__":
    power = [90, 80, 70, 60, 50, 100]
    n = len(power)
 
    k = 180
 
    # Function call
    print(maximum_sequence(power, k, n))
#This code is contributed by sanjanasikarwar24




import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
public class Main {
    // Function for finding out maximum
    // subsequences formed
    public static int maximumSequence(List<Integer> arr, int K, int n) {
        // Sorting array in non-increasing order
        Collections.sort(arr, Collections.reverseOrder());
 
        int i = 0;
        int sequence_formed = 0;
        int elements_required = 0;
 
        while (elements_required + (K / arr.get(i)) + 1 <= n) {
            // Increasing Sequence formed
            sequence_formed += 1;
 
            // After forming of a new sequence,
            // increasing elements used who have
            // assigned to any of the subsequence
            elements_required += ((K / arr.get(i)) + 1);
 
            i++;
        }
 
        return sequence_formed;
    }
 
    public static void main(String[] args) {
        List<Integer> power = Arrays.asList(90, 80, 70, 60, 50, 100);
        int n = power.size();
 
        int k = 180;
 
        // Function call
        System.out.println(maximumSequence(power, k, n));
    }
}
//This code is contributed by sanjanasikarwar24




using System;
using System.Collections.Generic;
using System.Linq;
 
namespace ConsoleApp
{
    class Program
    {
        // Function for finding out maximum
        // subsequences formed
        public static int MaximumSequence(List<int> arr, int K, int n)
        {
            // Sorting array in non-increasing order
            arr.Sort((a, b) => b.CompareTo(a));
 
            int i = 0;
            int sequence_formed = 0;
            int elements_required = 0;
 
            while (elements_required + (K / arr[i]) + 1 <= n)
            {
                // Increasing Sequence formed
                sequence_formed += 1;
 
                // After forming of a new sequence,
                // increasing elements used who have
                // assigned to any of the subsequence
                elements_required += ((K / arr[i]) + 1);
 
                i++;
            }
 
            return sequence_formed;
        }
 
        static void Main(string[] args)
        {
            List<int> power = new List<int> { 90, 80, 70, 60, 50, 100 };
            int n = power.Count;
 
            int k = 180;
 
            // Function call
            Console.WriteLine(MaximumSequence(power, k, n));
        }
    }
}
//This code is contributed by sanjanasikarwar24




using System;
using System.Collections.Generic;
using System.Linq;
 
namespace ConsoleApp
{
    class Program
    {
        // Function for finding out maximum
        // subsequences formed
        public static int MaximumSequence(List<int> arr, int K, int n)
        {
            // Sorting array in non-increasing order
            arr.Sort((a, b) => b.CompareTo(a));
 
            int i = 0;
            int sequence_formed = 0;
            int elements_required = 0;
 
            while (elements_required + (K / arr[i]) + 1 <= n)
            {
                // Increasing Sequence formed
                sequence_formed += 1;
 
                // After forming of a new sequence,
                // increasing elements used who have
                // assigned to any of the subsequence
                elements_required += ((K / arr[i]) + 1);
 
                i++;
            }
 
            return sequence_formed;
        }
 
        static void Main(string[] args)
        {
            List<int> power = new List<int> { 90, 80, 70, 60, 50, 100 };
            int n = power.Count;
 
            int k = 180;
 
            // Function call
            Console.WriteLine(MaximumSequence(power, k, n));
        }
    }
}
//This code is contributed by sanjanasikarwar24

Output
2

Time Complexity: O(N * logN)
Auxiliary Space: O(1)

Related Articles:


Article Tags :