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.



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 <bits/stdc++.h>
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<seg_len; i++)
     res = res * 10 + (s[i] - '0');

  // Find value of remaining segments using sliding
  // window
  int seg_len_pow = 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 = max(res, curr_val);
  }
  return res;
}

// Driver's Function
int main() {
  string s = "8754";
  int k = 2;
  cout << "Maximum number = " << findMaxSegment(s, k);
  return 0;
}

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.

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


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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




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

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 1 vote(s)






User Actions