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.
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.
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).
# 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)
# Driver Code
if __name__ == ‘__main__’:
s = “8754”
k = 2
print(“Maximum number = “,
# This code is contributed by PranchalK
Maximum number = 87
- Check if number can be displayed using seven segment led
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Longest Common Extension / LCE | Set 3 (Segment Tree Method)
- Maximum sum and product of the M consecutive digits in a number
- Maximum number of removals of given subsequence from a string
- Maximum number of characters between any two same character in a string
- String with maximum number of unique characters
- Concatenate strings in any order to get Maximum Number of "AB"
- Number of subarrays with maximum values in given range
- Maximum number of Unique integers in Sub-Array of given size
- Find the number in a range having maximum product of the digits
- Maximum number of given operations to remove the entire string
- Queries to print the character that occurs the maximum number of times in a given range
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Sliding Window Maximum (Maximum of all subarrays of size k)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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