Given a string consisting of some numbers, not separated by any separator. The numbers are positive integers and the sequence increases by one at each number except the missing number. The task is to find the missing number. The numbers will have no more than six digits. Print -1 if input sequence is not valid.
Input : 89101113 Output : 12 Input : 9899101102 Output : 100 Input : 596597598600601602: Output : 599 Input : 909192939495969798100101 Output : 99 Input : 11111211311411511 Output : -1
The idea is to try all lengths from 1 to 6. For every length we try, we check if the current length satisfies the property of all consecutive numbers and one missing. An interesting thing is number of digits may change as we increment numbers. For example when we move to 100 from 99. To handle this situation, we find number of digits using log base 10.
Below is the implementation of above approach:
This article is contributed by Roshni Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Find the last two missing digits of the given phone number
- String Range Queries to find the number of subsets equal to a given String
- Find numbers of balancing positions in string
- Missing characters to make a string Pangram
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Find the missing value from the given equation a + b = c
- Find the number of consecutive zero at the end after multiplying n numbers
- Find position of the given number among the numbers made of 4 and 7
- Find the missing element in an array of integers represented in binary format
- Find the maximum possible Binary Number from given string
- Find total number of distinct years from a string
- Find minimum number of steps to reach the end of String
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Find the number of strings formed using distinct characters of a given string