Given an integer N, the task is to form a minimum possible positive number (>0) by inverting some digits of N.
Inverting for a digit T is defined as subtracting it from 9 that is 9 – T.
Note: The final number should not start from zero.
Input:N = 4545
The minimum possible number is 4444 by subtracting the two 5 ( 9 – 5 = 4)
Input: N = 9000
The minimum possible number is 9000 cause the number has to be > 0 and hence 9 cannot be subtracted from itself.
Approach: The idea is to iterate over all the digits in the given number and check if 9 – current_digit is less than the current_digit then replace that digit with 9 – current_digit else don’t change the digit. If the first digit of the number is 9 then don’t change the digit and we can’t have trailing zero in the new number formed.
Below is the implementation of the above approach:
Time Complexity: O(log10N)
Auxiliary Space: O(log10N)
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.
- Find maximum number that can be formed using digits of a given number
- Find smallest number with given number of digits and sum of digits under given constraints
- Find smallest possible Number from a given large Number with same count of digits
- Find the smallest number whose digits multiply to a given number n
- Find smallest number with given number of digits and sum of digits
- Find the largest number that can be formed with the given digits
- Find the largest number that can be formed by changing at most K digits
- Find Nth even length palindromic number formed using digits X and Y
- Find all strings formed from characters mapped to digits of a number
- Find the kth smallest number with sum of digits as m
- Find the smallest number whose sum of digits is N
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Find the smallest positive number which can not be represented by given digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Smallest number by rearranging digits of a given number
- Immediate smallest number after re-arranging the digits of a given number
- Length of the smallest number which is divisible by K and formed by using 1's only
- Smallest number with given sum of digits and sum of square of digits
- Greatest number less than equal to B that can be formed from the digits of A
- Recursive sum of digits of a number formed by repeated appends
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.