# Convert a number of length N such that it contains any one digit at least ‘K’ times

Given the value **‘N’** which is length of a number **‘A’**. Your task is to convert the digits such that at least **‘K’** times in the number **‘A’** any digit exists. In order to replace one of **‘N’** digits you also needs to calculate the cost which is the absolute differences between the old digit and the new one. The task is to print the minimum cost took to convert the initial number to the final number also print the final number.

**Note:** If there are several such numbers, then print the lexicographically minimum one.

**Examples:**

Input:N = 6, K = 5, A = 898196

Output:4, 888188

Number = “898196”, the second digit as “9” will be replaced to “8” costs |9 – 8| = 1 . Replacing the fifth digit with an “8” will cost the same. Replacing the fifth digit cost |6 – 8| = 2. As a result, 4 will be the total cost and the final number will be “888188”.

Input:N = 16, K = 14, A = 6124258626539246

Output:22, 4444448444449444

**Approach:**

- Initialize a number ‘A’ of length ‘N’ .
- Initialize a PAIR STL to store the minimum cost and Number.
- Store the number as string in the temp variable.
- Using two for loop checks all the digits with difference of ‘j’ and replace them with ‘i’, break if cost is achieved.
- Replace the minimum cost with the previous one.
- At last, print the minimum cost and the final number.

Below is the implementation of above approach :

`// C++ program to illustrate ` `// the above problem ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calculate the minimum ` `// value and the final number ` `int` `finalNumber(` `int` `n, ` `int` `k, string a) ` `{ ` ` ` `// modtemp = modified temp string ` ` ` `int` `modtemp; ` ` ` ` ` `// store the count of numbers changed to k ` ` ` `int` `co; ` ` ` ` ` `// temporary temp string ` ` ` `string temp; ` ` ` ` ` `// To store the minimum cost and no ` ` ` `pair<` `int` `, string> ans = make_pair(INT_MAX, ` `""` `); ` ` ` ` ` `for` `(` `int` `i = 0; i < 10; i++) { ` ` ` `// 'i' will replace the digits of N's to ` ` ` `// generate a number with k same digits ` ` ` ` ` `// store the main str in temp str for modification ` ` ` `temp = a; ` ` ` ` ` `// To store the temporary value of the modified number ` ` ` `modtemp = 0; ` ` ` ` ` `// Initial count for the given number to replace 'i' ` ` ` `co = count(a.begin(), a.end(), i + ` `'0'` `); ` ` ` ` ` `// 'j' manages the difference 'i' and 'j' ` ` ` `for` `(` `int` `j = 1; j < 10; j++) { ` ` ` ` ` `// For the elements ahead of 'i' index ` ` ` `if` `(i + j < 10) { ` ` ` ` ` `// Checks all elements with difference 'j' ` ` ` `// and replaces them with 'i' ` ` ` `for` `(` `int` `p = 0; p < n; p++) { ` ` ` ` ` `// Break if count is achieved ` ` ` `if` `(co >= k) ` ` ` `break` `; ` ` ` ` ` `if` `(i + ` `'0'` `== temp[p] - j) { ` ` ` ` ` `// Replaces all elements with difference ` ` ` `// 'j' and with 'i' ` ` ` `temp[p] = i + ` `'0'` `; ` ` ` `modtemp += j; ` ` ` `co++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `// For the elements before 'i' index ` ` ` `if` `(i - j >= 0) { ` ` ` `for` `(` `int` `p = n - 1; p >= 0; p--) { ` ` ` `if` `(co >= k) ` ` ` `break` `; ` ` ` ` ` `if` `(i + ` `'0'` `== temp[p] + j) { ` ` ` `temp[p] = i + ` `'0'` `; ` ` ` `modtemp += j; ` ` ` `co++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// replace the minimum cost with the previous one ` ` ` `ans = min(ans, make_pair(modtemp, temp)); ` ` ` `} ` ` ` `// print the minimum cost and the final number ` ` ` `cout << ans.first << endl ` ` ` `<< ans.second << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// initilaize number length and k ` ` ` `int` `n = 5, k = 4; ` ` ` ` ` `// initialize the number ` ` ` `string a = ` `"21122"` `; ` ` ` ` ` `finalNumber(n, k, a); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 21222

**Explanation:** As on converting 1 to 2 just one time. 2 becomes k times in the number. So the cost is 2-1 = 1.

## Recommended Posts:

- Perform n steps to convert every digit of a number in the format [count][digit]
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Append a digit in the end to make the number equal to the length of the remaining string
- Smallest integer greater than n such that it consists of digit m exactly k times
- Count of Numbers in a Range where digit d occurs exactly K times
- Count of Numbers in Range where first digit is equal to last digit of the number
- Find the remainder when First digit of a number is divided by its Last digit
- Largest number less than N with digit sum greater than the digit sum of N
- Convert all substrings of length 'k' from base 'b' to decimal
- Convert to a string that is repetition of a substring of k length
- Number of times the largest perfect square number can be subtracted from N
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Queries on sum of odd number digit sums of all the factors of a number
- Times required by Simple interest for the Principal to become Y times itself

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.