Maximum segment value after putting k breakpoints in a number | Java program to find the maximum segment value after putting k breaks . ; Function to Find Maximum Number ; Maximum segment length ; Find value of first segment of seg_len ; Find value of remaining segments using sliding window ; To find value of current segment , first remove leading digit from previous value ; Then add trailing digit ; Driver code
class GFG { static int findMaxSegment ( String s , int k ) { int seg_len = s . length ( ) - k ; int res = 0 ; for ( int i = 0 ; i < seg_len ; i ++ ) res = res * 10 + ( s . charAt ( i ) - '0' ) ; int seg_len_pow = ( int ) Math . pow ( 10 , seg_len - 1 ) ; int curr_val = res ; for ( int i = 1 ; i <= ( s . length ( ) - seg_len ) ; i ++ ) { curr_val = curr_val - ( s . charAt ( i - 1 ) - '0' ) * seg_len_pow ; curr_val = curr_val * 10 + ( s . charAt ( i + seg_len - 1 ) - '0' ) ; res = Math . max ( res , curr_val ) ; } return res ; } public static void main ( String [ ] args ) { String s = "8754" ; int k = 2 ; System . out . print ( " Maximum ▁ number ▁ = ▁ " + findMaxSegment ( s , k ) ) ; } }