human
What is a code to minimum and maximum number of digits required to be removed to make a given number divisible by 3 and the other steps required given the following:
Minimum and maximum number of digits required to be removed to make a given number divisible by 3 | Function to find the maximum and minimum number of digits to be removed to make str divisible by 3.
Convert the string into array of digits.
Count of 0 s , 1 s , and 2 s.
Traverse the array.
Find the sum of digits % 3.
Cases to find minimum number of digits to be removed.
Cases to find maximum number of digits to be removed.
Driver Code.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to minimum and maximum number of digits required to be removed to make a given number divisible by 3 and the other steps required.
 def minMaxDigits ( str , N ) :
	arr = [ 0 ] * N
	for i in range ( N ) :
		arr [ i ] = ( ord ( str [ i ] ) - ord ( '0' ) ) % 3
	zero = 0
	one = 0
	two = 0
	for i in range ( N ) :
		if ( arr [ i ] == 0 ) :
			zero += 1
		if ( arr [ i ] == 1 ) :
			one += 1
		if ( arr [ i ] == 2 ) :
			two += 1
	sum = 0
	for i in range ( N ) :
		sum = ( sum + arr [ i ] ) % 3
	if ( sum == 0 ) :
		print ( "0" , end = " ▁ " )
	if ( sum == 1 ) :
		if ( one and N > 1 ) :
			print ( "1" , end = " ▁ " )
		elif ( two > 1 and N > 2 ) :
			print ( "2" , end = " ▁ " )
		else :
			print ( " - 1" , end = " ▁ " )
	if ( sum == 2 ) :
		if ( two and N > 1 ) :
			print ( "1" , end = " ▁ " )
		elif ( one > 1 and N > 2 ) :
			print ( "2" , end = " ▁ " )
		else :
			print ( " - 1" , end = " ▁ " )
	if ( zero > 0 ) :
		print ( N - 1 , end = " ▁ " )
	elif ( one > 0 and two > 0 ) :
		print ( N - 2 , end = " ▁ " )
	elif ( one > 2 or two > 2 ) :
		print ( N - 3 , end = " ▁ " )
	else :
		print ( " - 1" , end = " ▁ " )
str = "12345"
N = len ( str )
minMaxDigits ( str , N )