Number formed by deleting digits such that sum of the digits becomes even and the number odd
Last Updated :
14 Mar, 2023
Given a non-negative number N, the task is to convert the number by deleting some digits of the number, such that the sum of the digits becomes even but the number is odd. In case there is no possible number, then print -1.
Note: There can be multiple numbers possible for a given N.
Examples:
Input: N = 18720
Output: 17
Explanation:
After Deleting 8, 2, 0 digits the number becomes 17 which is odd and the digit-sum is 8 which is even.
Input: N = 3
Output: -1
Explanation:
There is no possibility such that number becomes odd and the digit-sum is even.
Approach:
The idea is to use the fact that “Even number of odd digits will give the sum to even number”. So, If the digits in the number contain even count of odd digits then it is possible to convert the number otherwise converting such number is not possible.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void convertthenumber( int n)
{
string s = to_string(n);
string res;
for ( int i = 0; i < s.length(); i++) {
if (s[i] == '1' || s[i] == '3'
|| s[i] == '5' || s[i] == '7'
|| s[i] == '9' )
res += s[i];
if (res.size() == 2)
break ;
}
if (res.size() == 2)
cout << res << endl;
else
cout << "-1" << endl;
}
int main()
{
int n = 18720;
convertthenumber(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
static void convertthenumber( int n)
{
String s = Integer.toString(n);
String res = "" ;
for ( int i = 0 ; i < s.length(); i++)
{
if (s.charAt(i) == '1' || s.charAt(i) == '3'
|| s.charAt(i) == '5' || s.charAt(i) == '7'
|| s.charAt(i) == '9' )
res += s.charAt(i);
if (res.length() == 2 )
break ;
}
if (res.length() == 2 )
System.out.println(res);
else
System.out.println(- 1 );
}
public static void main (String[] args)
{
int n = 18720 ;
convertthenumber(n);
}
}
|
Python3
def convertthenumber(n) :
s = str (n);
res = "";
for i in range ( len (s)) :
if (s[i] = = '1' or s[i] = = '3'
or s[i] = = '5' or s[i] = = '7'
or s[i] = = '9' ) :
res + = s[i];
if ( len (res) = = 2 ) :
break ;
if ( len (res) = = 2 ) :
print (res);
else :
print ( "-1" );
if __name__ = = "__main__" :
n = 18720 ;
convertthenumber(n);
|
C#
using System;
class GFG
{
static void convertthenumber( int n)
{
String s = n.ToString();
String res = "" ;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == '1' || s[i] == '3'
|| s[i] == '5' || s[i] == '7'
|| s[i] == '9' )
res += s[i];
if (res.Length == 2)
break ;
}
if (res.Length == 2)
Console.WriteLine(res);
else
Console.WriteLine(-1);
}
public static void Main (String[] args)
{
int n = 18720;
convertthenumber(n);
}
}
|
Javascript
<script>
function convertthenumber(n)
{
var s = n.toString();
var res = "" ;
var i;
for (i = 0; i < s.length; i++) {
if (s[i] == '1' || s[i] == '3'
|| s[i] == '5' || s[i] == '7'
|| s[i] == '9' )
res += s[i];
if (res.length == 2)
break ;
}
if (res.length == 2)
document.write(res);
else
document.write( "-1" );
}
var n = 18720;
convertthenumber(n);
</script>
|
Time Complexity: O(|s|), where |s| represents the number of digits in the given number.
Auxiliary Space: O(|s|)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...