Given a linked list which represents an integer number where every node is a digit if the represented integer. The task is to add a… Read More »
The post Add the given digit to a number stored in a linked list appeared first on GeeksforGeeks.
Professional way of Programming: Learn C, C++, Java, Python, Dot Net, Android the professional way
Given a linked list which represents an integer number where every node is a digit if the represented integer. The task is to add a… Read More »
The post Add the given digit to a number stored in a linked list appeared first on GeeksforGeeks.
Given two positive integers N and K. Find the minimum number of digits that can be removed from the number N such that after removals the number is divisible by 10K or print -1 if it is impossible.
Examples:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<strong>Input :</strong> N = 10904025, K = 2 <strong>Output :</strong> 3 <strong>Explanation :</strong> We can remove the digits 4, 2 and 5 such that the number becomes 10900 which is divisible by 10<sup>2</sup>. <strong>Input :</strong> N = 1000, K = 5 <strong>Output :</strong> 3 <strong>Explanation :</strong> We can remove the digits 1 and any two zeroes such that the number becomes 0 which is divisible by 10<sup>5</sup><strong>Input :</strong> N = 23985, K = 2 <strong>Output :</strong> -1 |
Approach : The idea is to start traversing the number from the last digit while keeping a counter. If the current digit is not zero, increment the counter variable, otherwise decrement variable K. When K becomes zero, return counter as answer. After traversing the whole number, check if the current value of K is zero or not. If it is zero, return counter as answer, otherwise return answer as number of digits in N – 1, since we need to reduce the whole number to a single zero which is divisible by any number. Also, if the given number does not contain any zero, return -1 as answer.
Below is the implementation of above approach.
|
|
Time Complexity :Number of digits in the given number.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the “Improve Article” button below.
thumb_up
Be the First to upvote.
Please write to us at [email protected] to report any issue with the above content.