Minimum deletions to convert given integer to an odd number whose sum of digits is even | Set 2 | Java 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
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 " + "NEW_LINE"); } 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 + "NEW_LINE"); } } public static void main ( String [ ] args ) { String N = "12345" ; minOperations ( N ) ; } }