Maximize length of subsequence consisting of single distinct character possible by K increments in a string | Function to find the maximum length of a subsequence of same characters after at most K increment operations ; Store the size of s ; Sort the given string sort ( S . begin ( ) , S . end ( ) ) ; convert input string to char array ; sort tempArray ; Stores the maximum length and the sum of the sliding window ; Traverse the string S ; Add the current character to the window ; Decrease the window size ; Update the value of sum ; Increment the value of start ; Update the maximum window size ; Print the resultant maximum length of the subsequence ; Driver code
function maxSubsequenceLen ( s , K ) { var N = s . length ; var start = 0 , end = 0 ; var S = s . split ( ' ' ) ; S . sort ( ) ; var ans = Number . MIN_VALUE , sum = 0 ; for ( end = 0 ; end < N ; end ++ ) { sum = sum + ( S [ end ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; while ( sum + K < ( S [ end ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) * ( end - start + 1 ) ) { sum = sum - ( S [ start ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; start ++ ; } ans = Math . max ( ans , end - start + 1 ) ; } document . write ( ans ) ; } var S = " " ; var K = 1 ; maxSubsequenceLen ( S , K ) ;