Minimum deletions to convert given integer to an odd number whose sum of digits is even | Set 2
Given a positive integer N, the task is to find the minimum number of digits required to be removed to convert N to an odd number whose sum of digits is even. If it is impossible to do so, then print “Not Possible”.
Examples:
Input: N = 12345
Output: 1
Explanation:
Removing the digit 3 modifies N to 1245.
Since the sum of digits of N is 14 and N is odd, the required output is 1.
Input: N = 222
Output: Not Possible
Approach: The idea is to use the fact that summation of even count of odd numbers results in an even number. Follow the steps below to solve the problem:
- Count total odd and even digits present in the integer N and store them in variables, say Odd and Even.
- If Odd = 0, then one cannot convert the integer to an odd integer by deleting any number of digits. Hence, print “Not Possible”.
- Otherwise, if Odd = 1, then to make the sum of digits even, one will have to delete that odd digit, which results in an even number. Therefore, print “Not Possible”.
- Now, count the number of digits to be deleted after the last occurrence of an odd digit and store it in a variable, say ans.
- If Odd is odd, then increment the count of ans by one, as one will have to delete an odd digit to make the sum even.
- Finally, print ans if none of the above cases satisfies.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minOperations(string& N)
{
int even = 0;
int odd = 0;
for ( auto it : N) {
if ((it - '0' ) % 2 == 0) {
even++;
}
else {
odd++;
}
}
if (odd == 0 || odd == 1) {
cout << "Not Possible"
<< "\n" ;
}
else {
int ans = 0;
for ( auto it : N) {
if ((it - '0' ) % 2 == 0) {
ans++;
}
else {
ans = 0;
}
}
if (odd % 2) {
ans++;
}
cout << ans << endl;
}
}
int main()
{
string N = "12345" ;
minOperations(N);
}
|
Java
import java.util.*;
class GFG
{
static void minOperations(String N)
{
int even = 0 ;
int odd = 0 ;
for ( int it : N.toCharArray())
{
if ((it - '0' ) % 2 == 0 )
{
even++;
}
else
{
odd++;
}
}
if (odd == 0 || odd == 1 )
{
System.out.print( "Not Possible"
+ "\n" );
}
else
{
int ans = 0 ;
for ( int it : N.toCharArray())
{
if ((it - '0' ) % 2 == 0 )
{
ans++;
}
else
{
ans = 0 ;
}
}
if (odd % 2 != 0 )
{
ans++;
}
System.out.print(ans + "\n" );
}
}
public static void main(String[] args)
{
String N = "12345" ;
minOperations(N);
}
}
|
Python3
def minOperations(N):
even = 0 ;
odd = 0 ;
for it in N:
if ( int ( ord (it) - ord ( '0' )) % 2 = = 0 ):
even + = 1 ;
else :
odd + = 1 ;
if (odd = = 0 or odd = = 1 ):
print ( "Not Possible" + "");
else :
ans = 0 ;
for it in N:
if ( int ( ord (it) - ord ( '0' )) % 2 = = 0 ):
ans + = 1 ;
else :
ans = 0 ;
if (odd % 2 ! = 0 ):
ans + = 1 ;
print (ans, end = " " );
if __name__ = = '__main__' :
N = "12345" ;
minOperations(N);
|
C#
using System;
class GFG
{
static void minOperations(String N)
{
int even = 0;
int odd = 0;
foreach ( int it in N.ToCharArray())
{
if ((it - '0' ) % 2 == 0)
{
even++;
}
else
{
odd++;
}
}
if (odd == 0 || odd == 1)
{
Console.Write( "Not Possible"
+ "\n" );
}
else
{
int ans = 0;
foreach ( int it in N.ToCharArray())
{
if ((it - '0' ) % 2 == 0)
{
ans++;
}
else
{
ans = 0;
}
}
if (odd % 2 != 0)
{
ans++;
}
Console.Write(ans + "\n" );
}
}
public static void Main(String[] args)
{
String N = "12345" ;
minOperations(N);
}
}
|
Javascript
<script>
function minOperations(N)
{
var even = 0;
var odd = 0;
for ( var it =0; it<N.length; it++) {
if ((N[it] - '0' ) % 2 == 0) {
even++;
}
else {
odd++;
}
}
if (odd == 0 || odd == 1) {
document.write( "Not Possible"
+ "<br>" );
}
else {
var ans = 0;
for ( var it =0; it<N.length; it++){
if ((N[it] - '0' ) % 2 == 0) {
ans++;
}
else {
ans = 0;
}
}
if (odd % 2) {
ans++;
}
document.write( ans );
}
}
var N = "12345" ;
minOperations(N);
</script>
|
Time Complexity: O(log10(N))
Auxiliary Space: O(1)
Last Updated :
10 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...