Maximum segment value after putting k breakpoints in a number | C # 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
using System ; 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 [ 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 [ i - 1 ] - '0' ) * seg_len_pow ; curr_val = curr_val * 10 + ( s [ i + seg_len - 1 ] - '0' ) ; res = Math . Max ( res , curr_val ) ; } return res ; } public static void Main ( ) { String s = "8754" ; int k = 2 ; Console . WriteLine ( " Maximum ▁ number ▁ = ▁ " + findMaxSegment ( s , k ) ) ; } }