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.
Examples:
Input: N = 725
Output: 719
Explanation:
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
Output: 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))
Auxiliary Space:O(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:
Python
# Python program to implement # the above approach # Function to return the smaller # number closest to N made up of # only odd digits def closest_smaller(N): N = str(N) l = list(map(int, N)) length = len(l) # Stores the position of # first even digit of N pos = -1 # Iterate through each digit of N for i in range(length): # Check for even digit. if l[i] % 2 == 0: pos = i break # If the first even digit is 0 if l[pos] == 0: # Replace 0 with 9 l[pos] = 9 # Iterate over preceding # digits for i in range(pos - 1, -1, -1): # If current digit is 1 if l[i] == 1: # Check if it is the # first digit or not if i == 0: # Append leading 0's l[i] = 0 break # Otherwise, replace by 9 l[i] = 9 # Otherwise else: # Decrease its value by 2 l[i] -= 2 break # If the first even digit exceeds 0 else: # Reduce the digit by 1 l[pos] -= 1 # Replace all succeeding digits by 9 for i in range(pos + 1, length): l[i] = 9 # Remove leading 0s if l[0] == 0: l.pop(0) result = ''.join(map(str, l)) return result # Function to return the greater # number closest to N made up of # only odd digits def closest_greater(N): N = str(N) l = list(map(int, N)) length = len(l) # Stores the position of # first even digit of N pos = -1 # Iterate over each digit # of N for i in range(length): # If even digit is found if l[i] % 2 == 0: pos = i break # Increase value of first # even digit by 1 l[pos] += 1 for i in range(pos + 1, length): l[i] = 1 result = ''.join(map(str, l)) return result # Function to check if all # digits of N are odd or not def check_all_digits_odd(N): N = str(N) l = list(map(int, N)) length = len(l) # Stores the position of # first even digit of N pos = -1 # Iterating over each digit # of N for i in range(length): # If even digit is found if l[i] % 2 == 0: pos = i break # If no even digit is found if pos == -1: return True return False # Function to return the # closest number to N # having odd digits only def closestNumber(N): # If all digits of N are odd if check_all_digits_odd(N): print(N) else: # Find smaller number # closest to N l = int(closest_smaller(N)) # Find greater number # closest to N r = int(closest_greater(N)) # Print the number with least # absolute difference if abs(N - l) <= abs(N - r): print(l) else: print(r) # Driver Code. if __name__ == '__main__': N = 110 closestNumber(N) |
111
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.
Recommended Posts:
- Nth number made up of odd digits only
- Count N-digit numbers made up of X or Y whose sum of digits is also made up of X or Y
- 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
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Minimum absolute difference of a number and its closest prime
- 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
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.

