Open In App

Maximize groups to be formed such that product of size of group with its minimum element is at least K

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array, arr[] of length N, and an integer K. The value of the i-th element is arr[i]. The task is to find the maximum number of groups such that for each group the product of the number of elements in that group and the minimum element is at least K

Note: Every element should belong to exactly one group and some elements may not be part of any group.

Examples:

Input: N=5, arr[]={7, 2, 11, 9, 5}, K=10
Output: 2
Explanation:

  • Make one group [11, 7] (group size=2) where product of size of the group and minimum element of group is 2*7=14 which is greater than k
  • Make another group [9, 5] (group size=2) where product of size of group and minimum element of group is 2*5=10 is equal to k.
  • Thus we can make maximum 2 groups

Input: N=4, arr[]={1, 7, 3, 3}, K=11
Output: 0
Explanation:

  • If we make a group [7, 3, 3]  then product of size of group (3) and minimum element of the group(3) is 3*3=9 which is less than k.
  • If we make a group [7, 3, 3, 1]  then product of size of group (4) and minimum element of the group(1) is 1*4=4 which is less than k.
  • If we make a group [7, 3]  then product of size of group (2) and minimum element of the group(3) is 2*3=6 which is less than k.
  • Thus we cannot make any group with the given array such that the product of size of group and the minimum element is at least k.

Approach: The given problem can be solved by a greedy approach. To maximize the number of groups, sort the array and start creating the groups by taking the bigger elements first because this will help us in maximizing the minimum element of each group. Thus the number of elements required in each group will reduce and we will maximize the number of groups. Follow the steps below to solve the problem:

  • Sort the given array in increasing order.
  • Initialize variables ans and count to 0 and 1 respectively, ans will store the total number of groups that can be created and count will store the size of the current group.
  • Traverse the given array from [N-1 to 0] using the variable i and perform these steps :
    • If the product of arr[i] (minimum element of the current group ) and count (size of the current group) is greater equal to k then increase the count (total number of groups) by 1 and initialize the count to 1.
    • Otherwise, increase the number of elements in the current group by 1.
  • After completing these steps print the value of ans as the answer.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the maximum number
// of groups that can be formed from given array
int maximumgroups(vector<int>& arr, int N, int k)
{
 
    // Sorting the given array in increasing order
    sort(arr.begin(), arr.end());
 
    int ans = 0, count = 1;
 
    // Start creating the groups by taking
    // the bigger elements first because this
    // will help us in maximizing our
    // minimum element of each group
    for (int i = N - 1; i >= 0; i--) {
 
        // If the product of minimum element of
        // current group and count is greater equal
        // to k then increase the ans by one and
        // initialize the count to 1
        if (arr[i] * count >= k) {
            ans++;
            count = 1;
        }
        // Otherwise increase the number of elements
        // in the current group by one
        else {
            count++;
        }
    }
 
    // Return the maximum number of groups possible
    return ans;
}
 
// Driver Code
int main()
{
    int N = 5;
    int k = 10;
    vector<int> arr = { 7, 11, 2, 9, 5 };
    int res = maximumgroups(arr, N, k);
 
    cout << res << endl;
 
    return 0;
}


Java




// Java program for the above approach
import java.util.Arrays;
 
class GFG {
 
  // Function to return the maximum number
  // of groups that can be formed from given array
  public static int maximumgroups(int[] arr, int N, int k) {
 
    // Sorting the given array in increasing order
    Arrays.sort(arr);
 
    int ans = 0, count = 1;
 
    // Start creating the groups by taking
    // the bigger elements first because this
    // will help us in maximizing our
    // minimum element of each group
    for (int i = N - 1; i >= 0; i--) {
 
      // If the product of minimum element of
      // current group and count is greater equal
      // to k then increase the ans by one and
      // initialize the count to 1
      if (arr[i] * count >= k) {
        ans++;
        count = 1;
      }
      // Otherwise increase the number of elements
      // in the current group by one
      else {
        count++;
      }
    }
 
    // Return the maximum number of groups possible
    return ans;
  }
 
  // Driver Code
  public static void main(String args[]) {
    int N = 5;
    int k = 10;
    int[] arr = { 7, 11, 2, 9, 5 };
    int res = maximumgroups(arr, N, k);
 
    System.out.println(res);
  }
}
 
// This code is contributed by saurabh_jaiswal.


Python3




# Python 3 program for the above approach
 
# Function to return the maximum number
# of groups that can be formed from given array
def maximumgroups(arr, N, k):
 
    # Sorting the given array in increasing order
    arr.sort();
    ans = 0
    count = 1;
 
    # Start creating the groups by taking
    # the bigger elements first because this
    # will help us in maximizing our
    # minimum element of each group
    for i in range(N - 1, -1, -1):
 
        # If the product of minimum element of
        # current group and count is greater equal
        # to k then increase the ans by one and
        # initialize the count to 1
        if (arr[i] * count >= k):
            ans += 1
            count = 1;
 
        # Otherwise increase the number of elements
        # in the current group by one
        else:
            count += 1
   
    # Return the maximum number of groups possible
    return ans;
 
# Driver Code
if __name__ == "__main__":
   
    N = 5;
    k = 10;
    arr = [ 7, 11, 2, 9, 5 ];
    res = maximumgroups(arr, N, k);
 
    print(res );
 
    # This code is contributed by ukasp.


C#




// C# program for the above approach
using System;
 
public class GFG {
 
  // Function to return the maximum number
  // of groups that can be formed from given array
  public static int maximumgroups(int[] arr, int N, int k) {
 
    // Sorting the given array in increasing order
    Array.Sort(arr);
 
    int ans = 0, count = 1;
 
    // Start creating the groups by taking
    // the bigger elements first because this
    // will help us in maximizing our
    // minimum element of each group
    for (int i = N - 1; i >= 0; i--) {
 
      // If the product of minimum element of
      // current group and count is greater equal
      // to k then increase the ans by one and
      // initialize the count to 1
      if (arr[i] * count >= k) {
        ans++;
        count = 1;
      }
      // Otherwise increase the number of elements
      // in the current group by one
      else {
        count++;
      }
    }
 
    // Return the maximum number of groups possible
    return ans;
  }
 
  // Driver Code
  public static void Main(String []args) {
    int N = 5;
    int k = 10;
    int[] arr = { 7, 11, 2, 9, 5 };
    int res = maximumgroups(arr, N, k);
 
    Console.WriteLine(res);
  }
}
 
// This code is contributed by 29AjayKumar


Javascript




  <script>
      // JavaScript code for the above approach
 
 
      // Function to return the maximum number
      // of groups that can be formed from given array
      function maximumgroups(arr, N, k) {
 
          // Sorting the given array in increasing order
          arr.sort(function (a, b) { return a - b })
 
          let ans = 0, count = 1;
 
          // Start creating the groups by taking
          // the bigger elements first because this
          // will help us in maximizing our
          // minimum element of each group
          for (let i = N - 1; i >= 0; i--) {
 
              // If the product of minimum element of
              // current group and count is greater equal
              // to k then increase the ans by one and
              // initialize the count to 1
              if (arr[i] * count >= k) {
                  ans++;
                  count = 1;
              }
              // Otherwise increase the number of elements
              // in the current group by one
              else {
                  count++;
              }
          }
 
          // Return the maximum number of groups possible
          return ans;
      }
 
      // Driver Code
 
      let N = 5;
      let k = 10;
      let arr = [7, 11, 2, 9, 5];
      let res = maximumgroups(arr, N, k);
 
      document.write(res)
 
// This code is contributed by Potta Lokesh
  </script>


 
 

Output

2

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



Last Updated : 07 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads