Problem Description
Given a positive integer n, find the smallest fair integer that is greater than or equal to n. An integer is defined as fair if the count of even digits is equal to the count of odd digits.
Key Insights
- A fair integer must have an even total number of digits.
- If n has an odd number of digits, the search should start from the smallest number with an even digit count.
- A brute-force iteration checking each number works since the maximum value is 10^9.
- Efficient digit counting can be done by iterating over the string representation or via numerical operations.
Space and Time Complexity
Time Complexity: O((m - n) * D) where m is the fair integer found and D is the number of digits in the candidate number. Space Complexity: O(1)
Solution
The solution employs a brute-force approach starting at n and incrementing until a fair integer is found. A helper function is used to count even and odd digits in each candidate. If n has an odd number of digits, it is adjusted to the smallest number with an even digit count (for example, from 99 to 100). This ensures that only numbers with an even number of digits are considered, since only they can potentially be fair. The algorithm continues checking sequentially, with an extra check to adjust when the number of digits changes from even to odd.