Frequency of smallest character in first sentence less than that of second sentence | JS program for the above approach ; Function to count the frequency of minimum character ; Sort the string s ; Return the count with smallest character ; Function to count number of frequency of smallest character of string arr1 [ ] is less than the string in arr2 [ ] ; To store the frequency of smallest character in each string of arr2 ; Traverse the arr2 [ ] ; Count the frequency of smallest character in string s ; Append the frequency to freq [ ] ; Sort the frequency array ; Traverse the array arr1 [ ] ; Count the frequency of smallest character in string s ; find the element greater than f ; Find the count such that arr1 [ i ] < arr2 [ j ] ; Print the count ; Driver Code ; Function Call
function upper_bound ( freq , f ) { let low = 0 , high = freq . length - 1 ; while ( low < high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( freq [ mid ] > f ) high = mid ; else low = mid + 1 ; } return ( freq [ low ] < f ) ? low ++ : low ; } function countMinFreq ( s ) { s = s . split ( ' ' ) . sort ( ) . join ( ' ' ) ; let x = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == s [ 0 ] ) x += 1 ; } return x ; } function countLessThan ( arr1 , arr2 ) { let freq = [ ] ; for ( let i = 0 ; i < arr2 . length ; i ++ ) { let f = countMinFreq ( arr2 [ i ] ) ; freq . push ( f ) ; } freq = freq . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < arr1 . length ; i ++ ) { let f = countMinFreq ( arr1 [ i ] ) ; let it = upper_bound ( freq , f ) ; let cnt = freq . length - ( it ) ; document . write ( cnt , ' ' ) ; } } let arr1 , arr2 ; arr1 = [ " " , " " ] ; arr2 = [ " " , " " , " " , " " ] ; countLessThan ( arr1 , arr2 ) ;