Smallest odd number with even sum of digits from the given number N

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.

Examples

Input: str = “15470”
Output: 15
Explanation:
Two smallest odd digits are 1 & 5. Hence the required number is 15.

Input: str = “124”
Output: -1
Explanation:
There is no smallest odd digit other than 1. Hence the required number can’t be form.

Approach:
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.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the smallest odd number
// with even sum of digits from the given number N
#include<bits/stdc++.h>
using namespace std;
  
// Function to find the smallest odd number 
// whose sum of digits is even from the given string
int smallest(string s)
{
    // Converting the given string 
    // to a list of digits
    vector<int> a(s.length());
    for(int i = 0; i < s.length(); i++)
        a[i] = s[i]-'0';
      
    // An empty array to store the digits
    vector<int> b;
      
    // For loop to iterate through each digit
    for(int i = 0; i < a.size(); i++)
    {
          
        // If the given digit is odd then 
        // the digit is appended to the array b
        if((a[i]) % 2 != 0)
            b.push_back(a[i]);
    }
              
    // Sorting the list of digits
    sort(b.begin(),b.end());
      
    // If the size of the list is greater than 1
    // then a 2 digit smallest odd number is returned
    // Since the sum of two odd digits is always even
    if(b.size() > 1)
        return (b[0])*10 + (b[1]);
      
    // Else, -1 is returned 
    return -1;
}
      
// Driver code
int main()
{
    cout << (smallest("15470"));
}
  
// This code is contributed by Surendra_Gangwar

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find the smallest odd number
# with even sum of digits from the given number N
  
# Function to find the smallest odd number 
# whose sum of digits is even from the given string
def smallest(s):
      
    # Converting the given string 
    # to a list of digits
    a = list(s)
      
    # An empty array to store the digits
    b = []
      
    # For loop to iterate through each digit
    for i in range(len(a)):
          
        # If the given digit is odd then 
        # the digit is appended to the array b
        if(int(a[i])%2 != 0):
            b.append(a[i])
              
    # Sorting the list of digits
    b = sorted(b)
      
    # If the size of the list is greater than 1
    # then a 2 digit smallest odd number is returned
    # Since the sum of two odd digits is always even
    if(len(b)>1):
        return int(b[0])*10 + int(b[1])
      
    # Else, -1 is returned    
    return -1
  
  
# Driver code
if __name__ == "__main__":
    print(smallest("15470"))

chevron_right


Output:

15

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : SURENDRA_GANGWAR