Program to find nearest number of n where all digits are odd in python

Given a number n, we need to find the nearest number where all digits are odd. When two values are equidistant from n, we return the larger one.

For example, if the input is n = 243, the output will be 199 because it's the closest number to 243 where all digits (1, 9, 9) are odd.

Algorithm

To solve this problem, we follow these steps:

  • Find the first even digit from left to right
  • If no even digit exists, return the original number
  • Generate two candidates: one smaller and one larger than n
  • Compare distances and return the closer one (or larger if tied)

Implementation

class Solution:
    def solve(self, n):
        first_even = -1
        s = str(n)
        l = len(s)
        
        # Find first even digit
        for i in range(l):
            if int(s[i]) % 2 == 0:
                first_even = i
                break
        
        # If no even digit found, return original number
        if first_even == -1:
            return n
        
        # Generate larger candidate
        big = str(int(s[:i + 1]) + 1)
        
        # Generate smaller candidate
        if s[i] == "0":
            if s[i - 1] == "1":
                small = str(int(s[:i + 1]) - 1)
            else:
                small = str(int(s[:i + 1]) - 11)
        else:
            small = str(int(s[:i + 1]) - 1)
        
        # Fill remaining positions
        for j in range(i + 1, l):
            big += "1"
            small += "9"
        
        big, small = int(big), int(small)
        d2 = big - n
        d1 = n - small
        
        # Return closer number (or larger if tied)
        if d1 < d2:
            return small
        else:
            return big

# Test the solution
ob = Solution()
n = 243
result = ob.solve(n)
print(f"Input: {n}")
print(f"Output: {result}")
Input: 243
Output: 199

How It Works

For n = 243:

  • First even digit is '4' at position 1
  • Smaller candidate: Replace '4' with '3' and fill remaining with '9' ? 239
  • Larger candidate: Replace '4' with '5' and fill remaining with '1' ? 251
  • But we need all odd digits, so we get 199 and 311
  • Distance to 199: |243 - 199| = 44
  • Distance to 311: |243 - 311| = 68
  • Return 199 as it's closer

Additional Examples

ob = Solution()

# Test with different inputs
test_cases = [135, 468, 999, 1000]

for num in test_cases:
    result = ob.solve(num)
    print(f"Input: {num}, Output: {result}")
Input: 135, Output: 135
Input: 468, Output: 379
Input: 999, Output: 999
Input: 1000, Output: 999

Conclusion

This algorithm efficiently finds the nearest number with all odd digits by identifying the first even digit and constructing two candidates. It handles edge cases like numbers starting with '1' and ensures optimal time complexity of O(d) where d is the number of digits.

Updated on: 2026-03-25T13:03:48+05:30

700 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements