Maximize subsequences having array elements not exceeding length of the subsequence | Java 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
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 ) ; } }