Minimize the number by changing at most K digits

Given a number N, the task is to minimize the number by changing at most K digits. Note that the number should not contain any leading zeros.

Examples:

Input: N = 91945, K = 3
Output: 10045

Input: N = 1, K = 0
Output: 1

Approach:



  • Replace the first digit with 1 if its not already 1 and update K accordingly.
  • Now for the rest of the digits, replace the next K – 1 non-zero digits with a 0.

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 minimized number
string minNum(string num, int k)
{
  
    // Total digits in the number
    int len = num.length();
  
    // If the string is empty or there
    // are no operations to perform
    if (len == 0 || k == 0)
        return num;
  
    // "0" is a valid number
    if (len == 1)
        return "0";
  
    // If the first digit is not already 1 then
    // update it to 1 and decrement k
    if (num[0] != '1') {
        num[0] = '1';
        k--;
    }
  
    int i = 1;
    // While there are operations left
    // and the number can still be updated
    while (k > 0 && i < len) {
  
        // If the current digit is not already 0
        // then update it to 0 and decrement k
        if (num[i] != '0') {
            num[i] = '0';
            k--;
        }
  
        i++;
    }
  
    // Return the minimised number
    return num;
}
  
// Driver code
int main()
{
    string num = "91945";
    int k = 3;
  
    cout << minNum(num, 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 minimized number 
    static String minNum(char num[], int k) 
    
      
        // Total digits in the number 
        int len = num.length; 
      
        // If the string is empty or there 
        // are no operations to perform 
        if (len == 0 || k == 0
        {
            String num_str = new String(num);
            return num_str; 
        }
      
        // "0" is a valid number 
        if (len == 1
            return "0"
      
        // If the first digit is not already 1 then 
        // update it to 1 and decrement k 
        if (num[0] != '1'
        
            num[0] = '1'
            k--; 
        
      
        int i = 1
          
        // While there are operations left 
        // and the number can still be updated 
        while (k > 0 && i < len) 
        
      
            // If the current digit is not already 0 
            // then update it to 0 and decrement k 
            if (num[i] != '0')
            
                num[i] = '0'
                k--; 
            
            i++; 
        
          
        String num_str = new String(num);
          
        // Return the minimised number 
        return num_str; 
    
      
    // Driver code 
    public static void main(String args[]) 
    
        String num = "91945"
        int k = 3
      
        System.out.println(minNum(num.toCharArray(), k)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the approach 
  
# Function to return the minimized number 
def minNum(num, k) : 
  
    # Total digits in the number 
    len_ = len(num) 
  
    # If the string is empty or there 
    # are no operations to perform 
    if len_ == 0 or k == 0
        return num 
  
    # "0" is a valid number 
    if len_ == 1
        return "0"
  
    # If the first digit is not already 1 then 
    # update it to 1 and decrement k 
    if num[0] != '1'
        num = '1' + num[1:] 
        k -= 1
  
    i = 1
      
    # While there are operations left 
    # and the number can still be updated 
    while k > 0 and i < len_ : 
  
        # If the current digit is not already 0 
        # then update it to 0 and decrement k 
        if num[i] != '0'
            num = num[:i] + '0' + num[i + 1:]
            k -= 1
  
        i += 1
   
    # Return the minimised number 
    return num 
  
# Driver code 
num = "91945"
k = 3
  
print(minNum(num, 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;
      
class GFG
{
      
    // Function to return the minimized number 
    static String minNum(char []num, int k) 
    
      
        // Total digits in the number 
        int len = num.Length; 
      
        // If the string is empty or there 
        // are no operations to perform 
        if (len == 0 || k == 0) 
        {
            return String.Join("", num); 
        }
      
        // "0" is a valid number 
        if (len == 1) 
            return "0"
      
        // If the first digit is not already 1 then 
        // update it to 1 and decrement k 
        if (num[0] != '1'
        
            num[0] = '1'
            k--; 
        
      
        int i = 1; 
          
        // While there are operations left 
        // and the number can still be updated 
        while (k > 0 && i < len) 
        
      
            // If the current digit is not already 0 
            // then update it to 0 and decrement k 
            if (num[i] != '0')
            
                num[i] = '0'
                k--; 
            
            i++; 
        
          
        // Return the minimised number 
        return String.Join("", num); 
    
      
    // Driver code 
    public static void Main(String []args) 
    
        String num = "91945"
        int k = 3; 
      
        Console.WriteLine(minNum(num.ToCharArray(), k)); 
    
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

10045

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

Recommended Posts:


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.