Find the largest number that can be formed by changing at most K digits

Given a string str representing a number and an integer K, the task is to find the largest number that can be formed by changing at most K digits in the given number.

Examples:

Input: str = “569431”, K = 3
Output: 999931
Replace first, second and fourth digits with 9.



Input: str = “5687”, K = 2
Output: 9987

Approach: In order to get the maximum number possible, leftmost digits must be replaced with 9s. For every digit of the number starting from the leftmost digit, if it is not already 9 and K is greater than 0 then replace it with 9 and decrement K by 1. Repeat these steps for every digit while K is greater than 0. Finally, print the updated number.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function to return the maximum number
// that can be formed by changing
// at most k digits in str
string findMaximumNum(string str, int n, int k)
{
  
    // For every digit of the number
    for (int i = 0; i < n; i++) {
  
        // If no more digits can be replaced
        if (k < 1)
            break;
  
        // If current digit is not already 9
        if (str[i] != '9') {
  
            // Replace it with 9
            str[i] = '9';
  
            // One digit has been used
            k--;
        }
    }
  
    return str;
}
  
// Driver code
int main()
{
    string str = "569431";
    int n = str.length();
    int k = 3;
  
    cout << findMaximumNum(str, n, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
    // Function to return the maximum number
    // that can be formed by changing
    // at most k digits in str
    static StringBuilder findMaximumNum(StringBuilder str, 
                                             int n, int k)
    {
      
        // For every digit of the number
        for (int i = 0; i < n; i++) 
        {
      
            // If no more digits can be replaced
            if (k < 1)
                break;
      
            // If current digit is not already 9
            if (str.charAt(i) != '9'
            {
      
                // Replace it with 9
                str.setCharAt(i, '9');
      
                // One digit has been used
                k--;
            }
        }
        return str;
    }
      
    // Driver code
    public static void main (String [] args)
    {
        StringBuilder str = new StringBuilder("569431");
          
        int n = str.length();
        int k = 3;
      
        System.out.println(findMaximumNum(str, n, k));
    }
}
  
// This code is contributed by ihritik

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the maximum number 
# that can be formed by changing 
# at most k digits in str 
def findMaximumNum(st, n, k):
  
    # For every digit of the number 
    for i in range(n): 
  
        # If no more digits can be replaced 
        if (k < 1): 
            break
  
        # If current digit is not already 9 
        if (st[i] != '9'): 
  
            # Replace it with 9 
            st = st[0:i] + '9' + st[i + 1:] 
  
            # One digit has been used 
            k -= 1
  
    return st
  
# Driver code 
st = "569431"
n = len(st) 
k = 3
print(findMaximumNum(st, n, k))
  
# This code is contributed by
# divyamohan123

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
using System.Text;
  
class GFG
{
    // Function to return the maximum number
    // that can be formed by changing
    // at most k digits in str
    static StringBuilder findMaximumNum(StringBuilder str, 
                                        int n, int k)
    {
      
        // For every digit of the number
        for (int i = 0; i < n; i++) 
        {
      
            // If no more digits can be replaced
            if (k < 1)
                break;
      
            // If current digit is not already 9
            if (str[i] != '9')
            {
      
                // Replace it with 9
                str[i] = '9';
      
                // One digit has been used
                k--;
            }
        }
        return str;
    }
      
    // Driver code
    public static void Main ()
    {
        StringBuilder str = new StringBuilder("569431");
          
        int n = str.Length;
        int k = 3;
      
        Console.WriteLine(findMaximumNum(str, n, k));
    }
}
  
// This code is contributed by ihritik

chevron_right


Output:

999931


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 : ihritik, divyamohan123



Article Tags :
Practice Tags :


Be the First to upvote.


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