Given a positive integer N, the task is to find the minimum N-digit number such that performing the following operations on it in the following order results into the largest N-digit number:
- Convert the number to its Binary Coded Decimal form.
- Concatenate all the resulting nibbles to form a binary number.
- Remove the least significant N bits from the number.
- Convert this obtained binary number to its decimal form.
Input: N = 4
Largest 4 digit number = 9999
BCD of 9999 = 1001 1001 1001 1001
Binary form = 1001100110011001
Replacing last 4 bits by 0000: 1001 1001 1001 0000 = 9990
Therefore, the minimum N-digit number that can generate 9999 is 9990
Input: N = 5
Largest 5 digit number = 99999
BCD of 99999 = 1001 1001 1001 1001 1001
Binary for = 10011001100110011001
Replacing last 5 bits by 00000: 10011001100110000000 = 99980
Therefore, the minimum N-digit number that can generate 99999 is 99980
Approach: The probelm can be solved based on the following observations of BCD numbers. Follow the steps below to solve the problem:
- Each nibble in BCD does not increase beyond 1001 which is 9 in binary form, since the maximum single digit decimal number is 9.
- Thus, it can be concluded that the maximum binary number that can be obtained by bringing N nibbles together is 1001 concatenated N times, whose decimal representation is have to be the digit 9 concatenated N times.
- The last N LSBs from this binary form is required to be removed. Thus the values of these bits will not contribute in making the result larger. Therefore, keeping the last N bits as 9 is not necessary as we need the minimum number producing the maximum result.
- The value of floor(N/4) will give us the number of nibbles that will be completely removed from the number. Assign these nibbles the value of 0000 to minimize the number.
- The remainder of N/4 gives us the number of digits that would be switched to 0 from the LSB of the last non-zero nibble after having performed the previous step.
- This BCD formed by performing the above steps, when converted to decimal, generates the required maximized N digit number.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
- Maximum Possible Product in Array after performing given Operations
- Maximum count of equal numbers in an array after performing given operations
- Maximum sum of all elements of array after performing given operations
- Count of distinct possible strings after performing given operations
- Number of array elements derivable from D after performing certain operations
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Find the final number obtained after performing the given operation
- Minimum number of given operations required to convert a permutation into an identity permutation
- Sub-string that contains all lowercase alphabets after performing the given operation
- Minimum operations of the given type required to make a complete graph
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Count of operations to make all elements of array a equal to its min element by performing a[i] – b[i]
- Minimum number of operations required to sum to binary string S
- Find the minimum number of operations required to make all array elements equal
- Check if the last element of array is even or odd after performing a operation p times
- Check if original Array is retained after performing XOR with M exactly K times
- Count of minimum reductions required to get the required sum K
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all Array elements divisible by K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.