Given an integer N, the task is to find the number closest to N having only odd digits. If more than one such number exists, print the minimum.
Input: N = 725
Numbers 719 and 731 consists of odd digits only and are closest to 725. Since 719 is the minimum, it is the required answer.
Input : N = 111
Naive Approach: The simplest approach to solve the problem is to iterate and find the closest numbers smaller and greater than N, having only odd numbers as its digits. Follow the steps below to solve the problem:
- If all the digits of N are odd, then print N as the answer.
- Find the closest smaller and greater numbers and return the one having the least difference with N. If both are found to be equidistant from N, print the smaller number.
Time Complexity: O(10(len(N) – 1))
Efficient Approach: Follow the steps below to optimize the above approach:
- If all the digits of N are odd, then return N.
- Calculate the closest smaller number to N efficiently by the following steps:
- Find the position of first even digit in N from left to right, i.e, pos.
- If the first even digit is 0, then replace 0 with 9 and iterate over all preceding digits and for every digit, if found to be 1, replace it with 9. Otherwise, decrease the digit by 2 and return the number.
- If no preceding digits are found to be exceeding 1, then replace the most significant digit with 0.
- If the first even digit is non-zero, then decrease that digit by 1. Replace all the succeeding digits with 9.
- Calculate the greater number closest to N by the steps below:
- Find the position of first even digit i.e pos.
- Increment the digits at pos by 1.
- Iterate over the succeeding digits and increment them by 1.
- Compare the absolute difference of the respective closest numbers obtained with N and print the one having least difference.
- If both the differences are found to be equal, print the smaller number.
Below is the implementation of the above approach:
Time Complexity: O(len(N))
Auxiliary Space: O(len(N))
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.
- Nth number made up of odd digits only
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find the n-th number made of even digits only
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Smallest multiple of a given number made of digits 0 and 9 only
- Number of digits in the nth number made of given four digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Sum of the digits of square of the given number which has only 1's as its digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Minimum digits to be removed to make either all digits or alternating digits same
- Count of Rectangles with area K made up of only 1s from given Binary Arrays
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Minimum number with digits as 4 and 7 only and given sum
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Minimum absolute difference of a number and its closest prime
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Minimum number of digits to be removed so that no two consecutive digits are same
- Convert to number with digits as 3 and 8 only
- Check if the given decimal number has 0 and 1 digits only
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.