# Number formed by deleting digits such that sum of the digits becomes even and the number odd

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++

`// C++ implementation to convert ` `// a number into odd number such ` `// that digit-sum is odd ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to convert a number into ` `// odd number such that digit-sum is odd ` `void` `converthenumber(` `int` `n) ` `{ ` ` ` `string s = to_string(n); ` ` ` `string res; ` ` ` ` ` `// Loop to find any first two ` ` ` `// odd number such that their ` ` ` `// sum is even and number is odd ` ` ` `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` `; ` ` ` `} ` ` ` ` ` `// Print the result ` ` ` `if` `(res.size() == 2) ` ` ` `cout << res << endl; ` ` ` `else` ` ` `cout << ` `"-1"` `<< endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 18720; ` ` ` `converthenumber(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to convert ` `// a number into odd number such ` `// that digit-sum is odd ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `Main ` `{ ` ` ` `// Function to convert a number into ` `// odd number such that digit-sum is odd ` `static` `void` `converthenumber(` `int` `n) ` `{ ` ` ` `String s = Integer.toString(n); ` ` ` `String res = ` `""` `; ` ` ` ` ` `// Loop to find any first two ` ` ` `// odd number such that their ` ` ` `// sum is even and number is odd ` ` ` `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` `; ` ` ` `} ` ` ` ` ` `// Print the result ` ` ` `if` `(res.length() == ` `2` `) ` ` ` `System.out.println(res); ` ` ` `else` ` ` `System.out.println(-` `1` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `18720` `; ` ` ` `converthenumber(n); ` `} ` `} ` ` ` `// This code is contributed by Subhadeep Gupta ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to convert ` `# a number into odd number such ` `# that digit-sum is odd ` ` ` `# Function to convert a number into ` `# odd number such that digit-sum is odd ` `def` `converthenumber(n) : ` ` ` `s ` `=` `str` `(n); ` ` ` `res ` `=` `""; ` ` ` ` ` `# Loop to find any first two ` ` ` `# odd number such that their ` ` ` `# sum is even and number is odd ` ` ` `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` `; ` ` ` ` ` `# Print the result ` ` ` `if` `(` `len` `(res) ` `=` `=` `2` `) : ` ` ` `print` `(res); ` ` ` ` ` `else` `: ` ` ` `print` `(` `"-1"` `); ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `18720` `; ` ` ` `converthenumber(n); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to convert ` `// a number into odd number such ` `// that digit-sum is odd ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to convert a number into ` `// odd number such that digit-sum is odd ` `static` `void` `converthenumber(` `int` `n) ` `{ ` ` ` `String s = n.ToString(); ` ` ` `String res = ` `""` `; ` ` ` ` ` `// Loop to find any first two ` ` ` `// odd number such that their ` ` ` `// sum is even and number is odd ` ` ` `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` `; ` ` ` `} ` ` ` ` ` `// Print the result ` ` ` `if` `(res.Length == 2) ` ` ` `Console.WriteLine(res); ` ` ` `else` ` ` `Console.WriteLine(-1); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main (String[] args) ` `{ ` ` ` `int` `n = 18720; ` ` ` `converthenumber(n); ` `} ` `} ` ` ` `// This code is contributed by Mohit kuamr 29 ` |

*chevron_right*

*filter_none*

**Output:**

17

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find the Largest Cube formed by Deleting minimum Digits from a number
- Find maximum number that can be formed using digits of a given number
- Greatest number less than equal to B that can be formed from the digits of A
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Find the largest number that can be formed by changing at most K digits
- Recursive sum of digits of a number formed by repeated appends
- Find Nth even length palindromic number formed using digits X and Y
- Find all strings formed from characters mapped to digits of a number
- Find smallest number with given number of digits and sum of digits under given constraints
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits
- Minimum number of digits to be removed so that no two consecutive digits are same
- Find the average of k digits from the beginning and l digits from the end of the given number
- Smallest number with given sum of digits and sum of square of digits
- Check if the sum of digits of number is divisible by all of its digits

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.