Print digit 's position to be removed to make a number divisible by 6 | Python program to print digit 's position to be removed to make number divisible by 6 ; function to print the number divisible by 6 after exactly removing a digit ; stores the Sum of all elements ; traverses the string and converts string to number array and Sums up ; if second last is odd or Sum of n - 1 elements are not divisible by 3. ; second last is even and prn - 1 elements removing last digit ; last digit removed ; counter to check if any element after removing , its Sum % 3 == 0 ; traverse till second last element ; to check if any element after removing , its Sum % 3 == 0 ; the leftmost element ; break at the leftmost element ; stores the right most element ; if no element has been found as a [ i + 1 ] > a [ i ] ; if second last is even , then remove last if ( Sum - last ) % 3 == 0 ; if no element which on removing gives Sum % 3 == 0 ; driver program to test the above function
import math as mt NEW_LINE def greatest ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE a = [ 0 for i in range ( n ) ] NEW_LINE Sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE Sum += a [ i ] NEW_LINE if ( a [ n - 2 ] % 2 != 0 or ( Sum - a [ n - 1 ] ) % 3 != 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT re = Sum % 3 NEW_LINE dell = - 1 NEW_LINE flag = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( ( a [ i ] ) % 3 == re ) : NEW_LINE INDENT if ( a [ i + 1 ] > a [ i ] ) : NEW_LINE INDENT dell = i NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT dell = i NEW_LINE DEDENT DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT if ( a [ n - 2 ] % 2 == 0 and re == a [ n - 1 ] % 3 ) : NEW_LINE INDENT dell = n - 1 NEW_LINE DEDENT DEDENT if ( dell == - 1 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( dell + 1 ) NEW_LINE DEDENT DEDENT DEDENT s = "7510222" NEW_LINE greatest ( s ) NEW_LINE