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