Lexicographically smallest numeric string having odd digit counts
Given a positive integer N, the task is to generate a lexicographically smallest numeric string of size N having an odd count of each digit.
Input: N = 4
Digit 1 and 2 both have an even count and is the lexicographically smallest string possible.
Input: N = 5
Digit 1 has an odd count and is the lexicographically smallest string possible.
Approach: The given problem can be solved based on the observation that if the value of N is even, then the resulting string contains 1s, (N – 1) number of times followed by a single 2 is the smallest lexicographic string possible. Otherwise, the resulting string contains 1s, N number of times is the smallest lexicographic string possible.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)