Maximize count of subsets into which the given array can be split such that it satisfies the given condition
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++
#include <bits/stdc++.h>
using namespace std;
void maxDivisions( int arr[], int N, int X)
{
sort(arr, arr + N, greater< int >());
int maxSub = 0;
int size = 0;
for ( int i = 0; i < N; i++) {
size++;
if (arr[i] * size >= X) {
maxSub++;
size = 0;
}
}
cout << maxSub << endl;
}
int main()
{
int arr[] = { 1, 3, 3, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
int X = 3;
maxDivisions(arr, N, X);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void maxDivisions(Integer arr[], int N, int X)
{
Arrays.sort(arr,Collections.reverseOrder());
int maxSub = 0 ;
int size = 0 ;
for ( int i = 0 ; i < N; i++)
{
size++;
if (arr[i] * size >= X)
{
maxSub++;
size = 0 ;
}
}
System.out.print(maxSub + "\n" );
}
public static void main(String[] args)
{
Integer arr[] = { 1 , 3 , 3 , 7 };
int N = arr.length;
int X = 3 ;
maxDivisions(arr, N, X);
}
}
|
Python3
def maxDivisions(arr, N, X) :
arr.sort(reverse = True )
maxSub = 0 ;
size = 0 ;
for i in range (N) :
size + = 1 ;
if (arr[i] * size > = X) :
maxSub + = 1 ;
size = 0 ;
print (maxSub);
if __name__ = = "__main__" :
arr = [ 1 , 3 , 3 , 7 ];
N = len (arr);
X = 3 ;
maxDivisions(arr, N, X);
|
C#
using System;
class GFG
{
static void maxDivisions( int [] arr, int N, int X)
{
Array.Sort(arr);
Array.Reverse(arr);
int maxSub = 0;
int size = 0;
for ( int i = 0; i < N; i++)
{
size++;
if (arr[i] * size >= X)
{
maxSub++;
size = 0;
}
}
Console.WriteLine(maxSub);
}
public static void Main()
{
int [] arr = { 1, 3, 3, 7 };
int N = arr.Length;
int X = 3;
maxDivisions(arr, N, X);
}
}
|
Javascript
<script>
function maxDivisions(arr, N, X)
{
arr.sort();
let maxSub = 0;
let size = 0;
for (let i = 0; i < N; i++)
{
size++;
if (arr[i] * size >= X)
{
maxSub++;
size = 0;
}
}
document.write(maxSub + "<br/>" );
}
let arr = [ 1, 3, 3, 7 ];
let N = arr.length;
let X = 3;
maxDivisions(arr, N, X);
</script>
|
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;
int maxDivisions( int arr[], int N, int X)
{
sort(arr, arr + N);
int ans = 0, prod = 1;
for ( int i = 0; i < N; i++) {
prod *= arr[i];
if (prod >= X) {
ans++;
prod = 1;
}
}
if (prod > 1) {
ans++;
}
return ans;
}
int main()
{
int arr[] = { 1, 3, 3, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
int X = 3;
cout << maxDivisions(arr, N, X) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int maxDivisions( int arr[], int N, int X)
{
Arrays.sort(arr);
int ans = 0 , prod = 1 ;
for ( int i = 0 ; i < N; i++) {
prod *= arr[i];
if (prod >= X) {
ans++;
prod = 1 ;
}
}
if (prod > 1 ) {
ans++;
}
return ans;
}
public static void main(String[] args) {
int arr[] = { 1 , 3 , 3 , 7 };
int N = arr.length;
int X = 3 ;
System.out.println(maxDivisions(arr, N, X));
}
}
|
Javascript
function maxDivisions(arr, N, X) {
arr.sort((a, b) => a - b);
let ans = 0, prod = 1;
for (let i = 0; i < N; i++) {
prod *= arr[i];
if (prod >= X) {
ans++;
prod = 1;
}
}
if (prod > 1) {
ans++;
}
return ans;
}
const arr = [1, 3, 3, 7];
const N = arr.length;
const X = 3;
console.log(maxDivisions(arr, N, X));
|
C#
using System;
using System.Linq;
class GFG {
static int maxDivisions( int [] arr, int N, int X)
{
Array.Sort(arr);
int ans = 0, prod = 1;
for ( int i = 0; i < N; i++) {
prod *= arr[i];
if (prod >= X) {
ans++;
prod = 1;
}
}
if (prod > 1) {
ans++;
}
return ans;
}
static void Main()
{
int [] arr = { 1, 3, 3, 7 };
int N = arr.Length;
int X = 3;
Console.WriteLine(maxDivisions(arr, N, X));
}
}
|
Python3
def maxDivisions(arr, N, X):
arr.sort()
ans = 0
prod = 1
for i in range (N):
prod * = arr[i]
if prod > = X:
ans + = 1
prod = 1
if prod > 1 :
ans + = 1
return ans
arr = [ 1 , 3 , 3 , 7 ]
N = len (arr)
X = 3
print (maxDivisions(arr, N, X))
|
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
Last Updated :
31 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...