human
Largest number N which can be reduced to 0 in K steps | Utility function to return the first digit of a number ..
Remove last digit from number till only one digit is left.
return the first digit.
Utility function that returns the count of numbers written down when starting from n.
Function to find the largest number N which can be reduced to 0 in K steps.
Get the sequence length of the mid point.
Until k sequence length is reached.
Update mid point.
Get count of the new mid point.
Update right to mid.
Update left to mid.
Increment mid point by one while count is equal to k to get the maximum value of mid point.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to largest number N which can be reduced to 0 in K steps and the other steps required.
 def firstDigit ( n ) :
	while ( n >= 10 ) :
		n //= 10 ;
	return n ;
def getCount ( n ) :
	count = 1 ;
	while ( n != 0 ) :
		leadDigit = firstDigit ( n ) ;
		n -= leadDigit ;
		count += 1 ;
	return count ;
def getLargestNumber ( k ) :
	left = k ;
	right = k * 10 ;
	mid = ( left + right ) // 2 ;
	length = getCount ( mid ) ;
	while ( length != k ) :
		mid = ( left + right ) // 2 ;
		length = getCount ( mid ) ;
		if ( length > k ) :
			right = mid ;
		else :
			left = mid ;
	while ( length == k ) :
		if ( length != getCount ( mid + 1 ) ) :
			break ;
		mid += 1 ;
	return mid ;
if __name__ == " _ _ main _ _ " :
	k = 3 ;
	print ( getLargestNumber ( k ) ) ;