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++
#include<bits/stdc++.h>
using namespace std;
int smallest(string s)
{
vector< int > a(s.length());
for ( int i = 0; i < s.length(); i++)
a[i] = s[i]- '0' ;
vector< int > b;
for ( int i = 0; i < a.size(); i++)
{
if ((a[i]) % 2 != 0)
b.push_back(a[i]);
}
sort(b.begin(),b.end());
if (b.size() > 1)
return (b[0])*10 + (b[1]);
return -1;
}
int main()
{
cout << (smallest( "15470" ));
}
|
Java
import java.util.*;
class GFG{
public static int smallest(String s)
{
int [] a = new int [s.length()];
for ( int i = 0 ; i < s.length(); i++)
a[i] = s.charAt(i) - '0' ;
Vector<Integer> b = new Vector<Integer>();
for ( int i = 0 ; i < a.length; i++)
{
if (a[i] % 2 != 0 )
b.add(a[i]);
}
Collections.sort(b);
if (b.size() > 1 )
return (b.get( 0 )) * 10 + (b.get( 1 ));
return - 1 ;
}
public static void main(String[] args)
{
System.out.print(smallest( "15470" ));
}
}
|
Python
def smallest(s):
a = list (s)
b = []
for i in range ( len (a)):
if ( int (a[i]) % 2 ! = 0 ):
b.append(a[i])
b = sorted (b)
if ( len (b)> 1 ):
return int (b[ 0 ]) * 10 + int (b[ 1 ])
return - 1
if __name__ = = "__main__" :
print (smallest( "15470" ))
|
C#
using System;
using System.Collections;
class GFG{
public static int smallest( string s)
{
int [] a = new int [s.Length];
for ( int i = 0; i < s.Length; i++)
a[i] = ( int )(s[i] - '0' );
ArrayList b = new ArrayList();
for ( int i = 0; i < a.Length; i++)
{
if (a[i] % 2 != 0)
b.Add(a[i]);
}
b.Sort();
if (b.Count > 1)
return (( int )b[0] * 10 + ( int )b[1]);
return -1;
}
public static void Main( string [] args)
{
Console.Write(smallest( "15470" ));
}
}
|
Javascript
<script>
function smallest(s)
{
var a = Array(s.length);
for ( var i = 0; i < s.length; i++)
a[i] = s[i].charCodeAt(0) -
'0' .charCodeAt(0);
var b = [];
for ( var i = 0; i < a.length; i++)
{
if ((a[i]) % 2 != 0)
b.push(a[i]);
}
b.sort((a, b) => a - b);
if (b.length > 1)
return (b[0]) * 10 + (b[1]);
return -1;
}
document.write(smallest( "15470" ));
</script>
|
Time Complexity: O(N) where N = length of string.
Auxiliary Space: O(N)
Last Updated :
30 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...