Minimum deletions to convert given integer to an odd number whose sum of digits is even | Set 2 | C ++ implementation of the above approach ; Function to find minimum count of digits required to be remove to make N odd and the sum of digits of N even ; Stores count of even digits ; Stores count of odd digits ; Iterate over the digits of N ; If current digit is even ; Update even ; Otherwise ; Update odd ; Base conditions ; Stores count of digits required to be removed to make N odd and the sum of digits of N even ; Iterate over the digits of N ; If current digit is even ; Update ans ; Otherwise , ; Update ans ; If count of odd digits is odd ; Update ans ; Finally print the ans ; Driver code ; Input string ; Function call
#include <bits/stdc++.h> NEW_LINE 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 " << " STRNEWLINE " ; } 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 ) ; }