Given a string ‘str’ of digits and an integer ‘n’, build the lowest possible number by removing ‘n’ digits from the string and not changing the order of input digits.
Input: str = "4325043", n = 3 Output: "2043" Input: str = "765028321", n = 5 Output: "0221" Input: str = "121198", n = 2 Output: "1118"
The idea is based on the fact that a character among first (n+1) characters must be there in resultant number. So we pick the smallest of first (n+1) digits and put it in result, and recur for remaining characters. Below is complete algorithm.
Initialize result as empty string res = "" buildLowestNumber(str, n, res) 1) If n == 0, then there is nothing to remove. Append the whole 'str' to 'res' and return 2) Let 'len' be length of 'str'. If 'len' is smaller or equal to n, then everything can be removed Append nothing to 'res' and return 3) Find the smallest character among first (n+1) characters of 'str'. Let the index of smallest character be minIndex. Append 'str[minIndex]' to 'res' and recur for substring after minIndex and for n = n-minIndex buildLowestNumber(str[minIndex+1..len-1], n-minIndex).
Below is the implementation of above algorithm.
Below is an optimised code in C++ contributed by Gaurav Mamgain
Time Complexity: O(n)
Space Complexity: O(n)
This article is contributed by Pallav Gurha. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Lowest Common Ancestor in a Binary Search Tree.
- Lowest Common Ancestor in a Binary Tree | Set 1
- Reduce the fraction to its lowest form
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Reduce the number to minimum multiple of 4 after removing the digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Number of cards needed build a House of Cards of a given level N
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Smallest number with given sum of digits and sum of square of digits
- Find the average of k digits from the beginning and l digits from the end of the given number
- Sum of the digits of square of the given number which has only 1's as its digits
- Program to build DFA that starts and end with 'a' from input (a, b)
- Program to build a DFA that checks if a string ends with "01" or "10"
- Program to build a DFA to accept strings that start and end with same character
- Program to build a DFA that accepts strings starting and ending with different character
- Build a DFA to accept Binary strings that starts or ends with "01"