Open In App

Maximize count of subsets into which the given array can be split such that it satisfies the given condition

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of size N and a positive integer X, the task is to partition the array into the maximum number of subsets such that the multiplication of the smallest element of each subset with the count of elements in the subsets is greater than or equal to K. Print the maximum count of such subsets possible.

Examples:

Input: arr[] = {1, 3, 3, 7}, X = 3
Output: 3
Explanation: Partition the array into 3 subsets { {1, 3}, {3}, {7} }. Therefore, the required output is 3.

Input: arr[] = {2, 4, 2, 5, 1}, X = 2
Output: 4

Approach: The problem can be solved using the Greedy technique. Follow the steps below to solve the problem:

  • Sort the array elements in decreasing order.
  • Traverse the array and keep track of the size of the current subset
  • As the array is sorted in decreasing order, the rightmost element of the subset will be the smallest element of the current division.
  • So, if (size of current subset * current element) is greater than or equal to X, then increment the count and reset the size of the current partition to 0.
  • Finally, print the count obtained.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to count maximum subsets into
// which the given array can be split such
// that it satisfies the given condition
void maxDivisions(int arr[], int N, int X)
{
 
    // Sort the array in decreasing order
    sort(arr, arr + N, greater<int>());
 
    // Stores count of subsets possible
    int maxSub = 0;
 
    // Stores count of elements
    // in current subset
    int size = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // Update size
        size++;
 
        // If product of the smallest element
        // present in the current subset and
        // size of current subset is >= K
        if (arr[i] * size >= X) {
 
            // Update maxSub
            maxSub++;
 
            // Update size
            size = 0;
        }
    }
 
    cout << maxSub << endl;
}
 
// Driver Code
int main()
{
 
    // Given array
    int arr[] = { 1, 3, 3, 7 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given value of X
    int X = 3;
 
    maxDivisions(arr, N, X);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to count maximum subsets into
// which the given array can be split such
// that it satisfies the given condition
static void maxDivisions(Integer arr[], int N, int X)
{
 
    // Sort the array in decreasing order
    Arrays.sort(arr,Collections.reverseOrder());
 
    // Stores count of subsets possible
    int maxSub = 0;
 
    // Stores count of elements
    // in current subset
    int size = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
 
        // Update size
        size++;
 
        // If product of the smallest element
        // present in the current subset and
        // size of current subset is >= K
        if (arr[i] * size >= X)
        {
 
            // Update maxSub
            maxSub++;
 
            // Update size
            size = 0;
        }
    }
    System.out.print(maxSub +"\n");
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Given array
    Integer arr[] = { 1, 3, 3, 7 };
 
    // Size of the array
    int N = arr.length;
 
    // Given value of X
    int X = 3;
    maxDivisions(arr, N, X);
 
}
}
 
// This code is contributed by shikhasingrajput


Python3




# Python3 program for the above approach
 
# Function to count maximum subsets into
# which the given array can be split such
# that it satisfies the given condition
def maxDivisions(arr, N, X) :
 
    # Sort the array in decreasing order
    arr.sort(reverse = True)
     
    # Stores count of subsets possible
    maxSub = 0;
 
    # Stores count of elements
    # in current subset
    size = 0;
 
    # Traverse the array arr[]
    for i in range(N) :
 
        # Update size
        size += 1;
 
        # If product of the smallest element
        # present in the current subset and
        # size of current subset is >= K
        if (arr[i] * size >= X) :
 
            # Update maxSub
            maxSub += 1;
 
            # Update size
            size = 0;
    print(maxSub);
 
# Driver Code
if __name__ == "__main__" :
 
    # Given array
    arr = [ 1, 3, 3, 7 ];
 
    # Size of the array
    N = len(arr);
 
    # Given value of X
    X = 3;
 
    maxDivisions(arr, N, X);
     
    # This code is contributed by AnkThon


C#




// C# program for the above approach
using System;
class GFG
{
 
  // Function to count maximum subsets into
  // which the given array can be split such
  // that it satisfies the given condition
  static void maxDivisions(int[] arr, int N, int X)
  {
 
    // Sort the array in decreasing order
    Array.Sort(arr);
    Array.Reverse(arr);
 
    // Stores count of subsets possible
    int maxSub = 0;
 
    // Stores count of elements
    // in current subset
    int size = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
 
      // Update size
      size++;
 
      // If product of the smallest element
      // present in the current subset and
      // size of current subset is >= K
      if (arr[i] * size >= X)
      {
 
        // Update maxSub
        maxSub++;
 
        // Update size
        size = 0;
      }
    }
 
    Console.WriteLine(maxSub);
  }
 
  // Driver Code
  public static void Main()
  {
 
    // Given array
    int[] arr = { 1, 3, 3, 7 };
 
    // Size of the array
    int N = arr.Length;
 
    // Given value of X
    int X = 3;
    maxDivisions(arr, N, X);
  }
}
 
// This code is contributed by subhammahato348.


Javascript




<script>
// javascript program of the above approach
 
// Function to count maximum subsets into
// which the given array can be split such
// that it satisfies the given condition
function maxDivisions(arr, N, X)
{
 
    // Sort the array in decreasing order
    arr.sort();
 
    // Stores count of subsets possible
    let maxSub = 0;
 
    // Stores count of elements
    // in current subset
    let size = 0;
 
    // Traverse the array arr[]
    for (let i = 0; i < N; i++)
    {
 
        // Update size
        size++;
 
        // If product of the smallest element
        // present in the current subset and
        // size of current subset is >= K
        if (arr[i] * size >= X)
        {
 
            // Update maxSub
            maxSub++;
 
            // Update size
            size = 0;
        }
    }
    document.write(maxSub + "<br/>");
}
 
    // Driver Code
     
    // Given array
    let arr = [ 1, 3, 3, 7 ];
 
    // Size of the array
    let N = arr.length;
 
    // Given value of X
    let X = 3;
    maxDivisions(arr, N, X);
 
</script>


Output

3

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

Another Approach:

  • Sort the given array arr[] in non-decreasing order.
  • Initialize two variables, ans and prod, to 0 and 1 respectively.                                                                                                                                               (a) ans: count of subsets into which the given array can be split such that the multiplication of the smallest element of each subset with the count of        elements in the subsets is greater than or equal to K.
    (b) prod: the product of elements in the current subset.
  • Traverse the array arr[] from left to right:
     (a) Multiply prod with arr[i] for every element in the array.                                                                                                                                                (b) If the product of elements so far is greater than or equal to X, then divide the current product by arr[i] and reset prod to 1. Increment the count         of subsets ans.
  • If there are still some elements left, then increment the count of subsets ans.
  • Return ans.

Below is the implementation of the above approach:

C++




#include<bits/stdc++.h>
using namespace std;
 
// Function to count maximum subsets into
// which the given array can be split such
// that it satisfies the given condition
int maxDivisions(int arr[], int N, int X)
{
    // Sort the array in non-decreasing order
    sort(arr, arr + N);
 
    int ans = 0, prod = 1;
 
    // Traverse the array arr[] from left to right.
    for (int i = 0; i < N; i++) {
        prod *= arr[i];
 
        // If the product of elements so far is >= X,
        // then divide the current product by arr[i]
        // and reset prod to 1.
        if (prod >= X) {
            ans++;
            prod = 1;
        }
    }
 
    // If there are still some elements left,
    // then increment the count of subsets.
    if (prod > 1) {
        ans++;
    }
 
    return ans;
}
 
// Driver code
int main()
{
    // Given array
    int arr[] = { 1, 3, 3, 7 };
  
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Given value of X
    int X = 3;
  
    cout << maxDivisions(arr, N, X) << endl;
  
    return 0;
}


Java




import java.util.*;
 
public class Main {
  // Function to count maximum subsets into
// which the given array can be split such
// that it satisfies the given condition
public static int maxDivisions(int arr[], int N, int X)
{
    // Sort the array in non-decreasing order
    Arrays.sort(arr);
 
    int ans = 0, prod = 1;
 
    // Traverse the array arr[] from left to right.
    for (int i = 0; i < N; i++) {
        prod *= arr[i];
 
        // If the product of elements so far is >= X,
        // then divide the current product by arr[i]
        // and reset prod to 1.
        if (prod >= X) {
            ans++;
            prod = 1;
        }
    }
 
    // If there are still some elements left,
    // then increment the count of subsets.
    if (prod > 1) {
        ans++;
    }
 
    return ans;
}
// Driver code
public static void main(String[] args) {
    // Given array
    int arr[] = { 1, 3, 3, 7 };
  
    // Size of the array
    int N = arr.length;
  
    // Given value of X
    int X = 3;
  
    System.out.println(maxDivisions(arr, N, X));
}
  }


Javascript




// Function to count maximum subsets into
// which the given array can be split such
// that it satisfies the given condition
function maxDivisions(arr, N, X) {
  // Sort the array in non-decreasing order
  arr.sort((a, b) => a - b);
 
  let ans = 0, prod = 1;
 
  // Traverse the array arr[] from left to right.
  for (let i = 0; i < N; i++) {
    prod *= arr[i];
 
    // If the product of elements so far is >= X,
    // then divide the current product by arr[i]
    // and reset prod to 1.
    if (prod >= X) {
      ans++;
      prod = 1;
    }
  }
 
  // If there are still some elements left,
  // then increment the count of subsets.
  if (prod > 1) {
    ans++;
  }
 
  return ans;
}
 
// Driver code
// Given array
const arr = [1, 3, 3, 7];
 
// Size of the array
const N = arr.length;
 
// Given value of X
const X = 3;
 
console.log(maxDivisions(arr, N, X));


C#




using System;
using System.Linq;
 
class GFG {
    // Function to count maximum subsets into
    // which the given array can be split such
    // that it satisfies the given condition
    static int maxDivisions(int[] arr, int N, int X)
    {
        // Sort the array in non-decreasing order
        Array.Sort(arr);
 
        int ans = 0, prod = 1;
 
        // Traverse the array arr[] from left to right.
        for (int i = 0; i < N; i++) {
            prod *= arr[i];
 
            // If the product of elements so far is >= X,
            // then divide the current product by arr[i]
            // and reset prod to 1.
            if (prod >= X) {
                ans++;
                prod = 1;
            }
        }
 
        // If there are still some elements left,
        // then increment the count of subsets.
        if (prod > 1) {
            ans++;
        }
 
        return ans;
    }
 
    // Driver code
    static void Main()
    {
        // Given array
        int[] arr = { 1, 3, 3, 7 };
 
        // Size of the array
        int N = arr.Length;
 
        // Given value of X
        int X = 3;
 
        Console.WriteLine(maxDivisions(arr, N, X));
    }
}
// This code is contributed by user_dtewbxkn77n


Python3




def maxDivisions(arr, N, X):
    # Sort the array in non-decreasing order
    arr.sort()
 
    ans = 0
    prod = 1
 
    # Traverse the array arr[] from left to right.
    for i in range(N):
        prod *= arr[i]
 
        # If the product of elements so far is >= X,
        # then divide the current product by arr[i]
        # and reset prod to 1.
        if prod >= X:
            ans += 1
            prod = 1
 
    # If there are still some elements left,
    # then increment the count of subsets.
    if prod > 1:
        ans += 1
 
    return ans
 
 
# Driver code
arr = [1, 3, 3, 7]
N = len(arr)
X = 3
print(maxDivisions(arr, N, X))


Output

3

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



Last Updated : 31 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads