Given an array arr[] consisting of N positive integers, the task is to maximize the number of subsequences that can be obtained from an array such that every element arr[i] that is part of any subsequence does not exceed the length of that subsequence.
Examples:
Input: arr[] = {1, 1, 1, 1}
Output: 4
Explanation:
Form 4 subsequences of 1 which satisfy the given condition {1}, {1}, {1}, {1}.
Input: arr[] = {2, 2, 3, 1, 2, 1}
Output: 3
Explanation:
The possible group are {1}, {1}, {2, 2}
So, the output is 3.
Approach: The idea is to use Greedy Technique to solve this problem. Follow the steps below to solve the problem:
- Initialize a map to store the frequency of every array element.
- Initialize a variable, say count to 0, to store the total number of subsequences obtained.
- Keep track of the number of elements that are left to be added.
- Now, iterate over the map and count the number of elements that can be included in a particular group.
- Keep on adding the elements on the valid subsequences.
- After completing the above steps, print the count of subsequences.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int No_Of_subsequences(map< int , int > mp)
{
int count = 0;
int left = 0;
for ( auto x : mp) {
x.second += left;
count += (x.second / x.first);
left = x.second % x.first;
}
return count;
}
void maximumsubsequences( int arr[], int n)
{
map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[arr[i]]++;
cout << No_Of_subsequences(mp);
}
int main()
{
int arr[] = { 1, 1, 1, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
maximumsubsequences(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int No_Of_subsequences(HashMap<Integer,
Integer> mp)
{
int count = 0 ;
int left = 0 ;
for (Map.Entry<Integer, Integer> x : mp.entrySet())
{
mp.replace(x.getKey(), x.getValue() + left);
count += (x.getValue() / x.getKey());
left = x.getValue() % x.getKey();
}
return count;
}
public static void maximumsubsequences( int [] arr,
int n)
{
HashMap<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (mp.containsKey(arr[i]))
{
mp.replace(arr[i], mp.get(arr[i]) + 1 );
}
else
{
mp.put(arr[i], 1 );
}
}
System.out.println(No_Of_subsequences(mp));
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 1 , 1 };
int N = arr.length;
maximumsubsequences(arr, N);
}
}
|
Python3
from collections import defaultdict
def No_Of_subsequences(mp):
count = 0
left = 0
for x in mp:
mp[x] + = left
count + = (mp[x] / / x)
left = mp[x] % x
return count
def maximumsubsequences(arr, n):
mp = defaultdict ( int )
for i in range (n):
mp[arr[i]] + = 1
print (No_Of_subsequences(mp))
if __name__ = = "__main__" :
arr = [ 1 , 1 , 1 , 1 ]
N = len (arr)
maximumsubsequences(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static int No_Of_subsequences(Dictionary< int ,
int > mp)
{
int count = 0;
int left = 0;
foreach (KeyValuePair< int ,
int > x in mp)
{
if (!mp.ContainsKey(x.Key))
mp.Add(x.Key, x.Value + left);
count += (x.Value / x.Key);
left = x.Value % x.Key;
}
return count;
}
public static void maximumsubsequences( int [] arr,
int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]] = mp[arr[i]] + 1;
}
else
{
mp.Add(arr[i], 1);
}
}
Console.WriteLine(No_Of_subsequences(mp));
}
public static void Main(String[] args)
{
int []arr = {1, 1, 1, 1};
int N = arr.Length;
maximumsubsequences(arr, N);
}
}
|
Javascript
<script>
function No_Of_subsequences(mp)
{
var count = 0;
var left = 0;
mp.forEach((value, key) => {
value += left;
count += (value / key);
left = value % key;
});
return count;
}
function maximumsubsequences(arr, n)
{
var mp = new Map();
for ( var i = 0; i < n; i++)
{
if (mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1);
}
document.write( No_Of_subsequences(mp));
}
var arr = [1, 1, 1, 1];
var N = arr.length;
maximumsubsequences(arr, N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)