Wrong Subtraction
Practice this coding question on Coding Gurukul.
Problem statement
A training calculator has a deliberately unusual countdown button. Instead of always subtracting one, it changes its behaviour according to the final decimal digit. A teacher presses the button a fixed number of times and asks students to predict the final display. The display contains a positive integer, and every press works on the number left by the previous press. Your task Perform exactly k operations on n. If the current number ends in a nonzero digit, subtract 1. If its last digit is zero, divide the number by 10, removing that zero. Print the number after the last operation. This is a simulation of the stated button rule, not ordinary subtraction of k. Important details The rule is checked again after each operation. Subtracting one may create a trailing zero, which changes what the next press does. Inputs guarantee the displayed number remains positive throughout. Worked example Starting at 31 with three presses produces 30, then 3, then 2. Therefore the final display is 2.
Input format
Two integers n and k.
Output format
Print the final integer.
Constraints
2 <= n <= 1000000000; 1 <= k <= 50; all intermediate results are positive.
Sample input
31 3
Sample output
2
Sign in to run or submit a solution in C++, Java or Python.