Maximize subsequences having array elements not exceeding length of the subsequence | C ++ program for the above approach ; Function to calculate the number of subsequences that can be formed ; Stores the number of subsequences ; Iterate over the map ; Count the number of subsequences that can be formed from x . first ; Number of occurrences of x . first which are left ; Return the number of subsequences ; Function to create the maximum count of subsequences that can be formed ; Stores the frequency of arr [ ] ; Update the frequency ; Print the number of subsequences ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE 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 ; }