Given a large number in form of string str. The task is to find the smallest odd number whose sum of digits is even by removing zero or more characters from the given string str, where the digits can be rearranged.
Input: str = “15470”
Two smallest odd digits are 1 & 5. Hence the required number is 15.
Input: str = “124”
There is no smallest odd digit other than 1. Hence the required number can’t be form.
On observing closely, by intuition, it can be understood that the number of digits in the smallest odd number possible is 2. And every digit in this number is odd because the sum of two odd digits is always even. Therefore, the idea to solve this problem is to iterate through the given string and store every odd number in an array. This array can be sorted and the first two digits together form the smallest odd number whose sum of its digits is even.
Below is the implementation of the above approach.
Time Complexity: O(N) where N = length of string.
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.
- Find smallest number with given number of digits and sum of digits under given constraints
- Find smallest number formed by inverting digits of given number N
- Find smallest possible Number from a given large Number with same count of digits
- Find smallest number with given number of digits and sum of digits
- Find the smallest number whose digits multiply to a given number n
- Smallest number by rearranging digits of a given number
- Immediate smallest number after re-arranging the digits of a given number
- Smallest number with given sum of digits and sum of square of digits
- Get the kth smallest number using the digits of the given number
- Smallest even digits number not less than N
- Smallest odd digits number not less than N
- Smallest Even number with N digits
- Smallest odd number with N digits
- Smallest number with sum of digits as N and divisible by 10^N
- Find the smallest number whose sum of digits is N
- Smallest number whose square has N digits
- Find the kth smallest number with sum of digits as m
- Smallest number with at least n digits in factorial
- Smallest number whose sum of digits is square of N
- Smallest number greater than or equal to N using only digits 1 to K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : SURENDRA_GANGWAR