# Maximum segment value after putting k breakpoints in a number

Given a large number as string s and an integer k which denotes the number of breakpoints we must put in the number k <= string length. The task is to find maximum segment value after putting exactly k breakpoints.

Examples:

```Input : s = "8754", k = 2
Output : Maximum number = 87
Explanation : We need to two breakpoints. After
putting the breakpoints, we get following options
8 75 4
87 5 4
The maximum segment value is 87.

Input : s = "999", k = 1
Output : Maximum Segment Value = 99
Explanation : We need to one breakpoint. After
putting the breakpoint, we either get 99,9 or
9,99.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

One important observation is, the maximum would always be of length “string-length – k” which is the maximum value of any segment. Considering the fact, problem becomes like sliding window problem means we need to find maximum of all substrings of size (string-length – k).

## C++

 `// CPP program to find the maximum segment ` `// value after putting k breaks. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to Find Maximum Number ` `int` `findMaxSegment(string &s, ``int` `k) { ` ` `  `  ``// Maximum segment length ` `  ``int` `seg_len = s.length() - k; ` ` `  `  ``// Find value of first segment of seg_len ` `  ``int` `res = 0; ` `  ``for` `(``int` `i=0; i

## Java

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

## Python3

 `# Python3 program to find the maximum segment  ` `# value after putting k breaks.  ` ` `  `# Function to Find Maximum Number  ` `def` `findMaxSegment(s, k): ` ` `  `    ``# Maximum segment length  ` `    ``seg_len ``=` `len``(s) ``-` `k  ` ` `  `    ``# Find value of first segment of seg_len  ` `    ``res ``=` `0` `    ``for` `i ``in` `range``(seg_len): ` `        ``res ``=` `res ``*` `10` `+` `(``ord``(s[i]) ``-` `ord``(``'0'``))  ` ` `  `    ``# Find value of remaining segments ` `    ``# using sliding window  ` `    ``seg_len_pow ``=` `pow``(``10``, seg_len ``-` `1``)  ` `    ``curr_val ``=` `res  ` `    ``for` `i ``in` `range``(``1``, ``len``(s) ``-` `seg_len): ` ` `  `        ``# To find value of current segment,  ` `        ``# first remove leading digit from  ` `        ``# previous value      ` `        ``curr_val ``=` `curr_val ``-` `(``ord``(s[i ``-` `1``])``-`  `                               ``ord``(``'0'``)) ``*` `seg_len_pow  ` ` `  `        ``# Then add trailing digit  ` `        ``curr_val ``=` `(curr_val ``*` `10` `+`  `                   ``(``ord``(s[i ``+` `seg_len ``-` `1``]) ``-` `ord``(``'0'``)))  ` ` `  `        ``res ``=` `max``(res, curr_val) ` `    ``return` `res ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``s ``=` `"8754"` `    ``k ``=` `2` `    ``print``(``"Maximum number = "``, ` `         ``findMaxSegment(s, k)) ` ` `  `# This code is contributed by PranchalK `

## C#

 `// C# program to find the maximum segment ` `// value after putting k breaks. ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to Find Maximum Number ` `    ``static` `int` `findMaxSegment(``string` `s, ``int` `k) { ` `     `  `        ``// Maximum segment length ` `        ``int` `seg_len = s.Length - k; ` `     `  `        ``// Find value of first segment of seg_len ` `        ``int` `res = 0; ` `         `  `        ``for` `(``int` `i = 0; i < seg_len; i++) ` `            ``res = res * 10 + (s[i] - ``'0'``); ` `     `  `        ``// Find value of remaining segments using  ` `        ``// sliding window ` `        ``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++) { ` `     `  `            ``// To find value of current segment,  ` `            ``// first remove leading digit from  ` `            ``// previous value ` `            ``curr_val = curr_val -  ` `            ``(s[i - 1] - ``'0'``) * seg_len_pow; ` `         `  `            ``// Then add trailing digit ` `            ``curr_val = curr_val * 10 +  ` `            ``(s[i + seg_len - 1] - ``'0'``); ` `         `  `            ``res = Math.Max(res, curr_val); ` `        ``} ` `         `  `        ``return` `res; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() { ` `         `  `        ``String s = ``"8754"``; ` `        ``int` `k = 2; ` `         `  `        ``Console.WriteLine(``"Maximum number = "` `                        ``+ findMaxSegment(s, k)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

Output:

```Maximum number = 87
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : PranchalKatiyar

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.