Find the missing number in a string of numbers with no separator

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.

Examples:

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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find a missing number in a
// string of consecutive numbers without any
// separator.
#include<bits/stdc++.h>
using namespace std;
const int MAX_DIGITS = 6;
  
// gets the integer at position i with length m,
// returns it or -1, if none
int getValue(const string& str, int i, int m)
{
    if (i + m > str.length())
        return -1;
  
    // Find value at index i and length m.
    int value = 0;
    for (int j = 0; j < m; j++)
    {
        int c = str[i + j] - '0';
        if (c < 0 || c > 9)
            return -1;
        value = value * 10 + c;
    }
    return value;
}
  
// Returns value of missing number
int findMissingNumber(const string& str)
{
    // Try all lengths for first number
    for (int m=1; m<=MAX_DIGITS; ++m)
    {
        // Get value of first number with current
        // length/
        int n = getValue(str, 0, m);
        if (n == -1)
           break;
  
        // To store missing number of current length
        int missingNo = -1;
  
        // To indicate whether the sequence failed
        // anywhere for current length.
        bool fail = false;
  
        // Find subsequent numbers with previous number as n
        for (int i=m; i!=str.length(); i += 1 + log10l(n))
        {
            // If we haven't yet found the missing number
            // for current length. Next number is n+2. Note
            // that we use Log10 as (n+2) may have more
            // length than n.
            if ((missingNo == -1) &&
                (getValue(str, i, 1+log10l(n+2)) == n+2))
            {
                missingNo = n + 1;
                n += 2;
            }
  
            // If next value is (n+1)
            else if (getValue(str, i, 1+log10l(n+1)) == n+1)
                n++;
  
            else
            {
                fail = true;
                break;
            }
        }
  
        if (!fail)
          return missingNo;
    }
    return -1; // not found or no missing number
}
  
// Driver code
int main()
{
    cout << findMissingNumber("99101102");
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find a missing number in a
// string of consecutive numbers without any
// separator.
  
class GFG {
  
    static final int MAX_DIGITS = 6;
  
// gets the integer at position i with length m,
// returns it or -1, if none
    static int getValue(String str, int i, int m) {
        if (i + m > str.length()) {
            return -1;
        }
  
        // Find value at index i and length m.
        int value = 0;
        for (int j = 0; j < m; j++) {
            int c = str.charAt(i + j) - '0';
            if (c < 0 || c > 9) {
                return -1;
            }
            value = value * 10 + c;
        }
        return value;
    }
  
// Returns value of missing number
    static int findMissingNumber(String str) {
        // Try all lengths for first number
        for (int m = 1; m <= MAX_DIGITS; ++m) {
            // Get value of first number with current
            // length/
            int n = getValue(str, 0, m);
            if (n == -1) {
                break;
            }
  
            // To store missing number of current length
            int missingNo = -1;
  
            // To indicate whether the sequence failed
            // anywhere for current length.
            boolean fail = false;
  
            // Find subsequent numbers with previous number as n
            for (int i = m; i != str.length(); i += 1 + Math.log10(n)) {
                // If we haven't yet found the missing number
                // for current length. Next number is n+2. Note
                // that we use Log10 as (n+2) may have more
                // length than n.
                if ((missingNo == -1)
                        && (getValue(str, i, (int) (1 + Math.log10(n + 2))) == n + 2)) {
                    missingNo = n + 1;
                    n += 2;
                } // If next value is (n+1)
                else if (getValue(str, i, (int) (1 + Math.log10(n + 1))) == n + 1) {
                    n++;
                } else {
                    fail = true;
                    break;
                }
            }
  
            if (!fail) {
                return missingNo;
            }
        }
        return -1; // not found or no missing number
    }
// Driver code
  
    public static void main(String[] args) {
        System.out.println(findMissingNumber("99101102"));
    }
}
// This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find a missing number in a 
// string of consecutive numbers without any 
// separator. 
using System;
public class GFG { 
  
    static readonly int MAX_DIGITS = 6; 
  
// gets the integer at position i with length m, 
// returns it or -1, if none 
    static int getValue(String str, int i, int m) { 
        if (i + m > str.Length) { 
            return -1; 
        
  
        // Find value at index i and length m. 
        int value = 0; 
        for (int j = 0; j < m; j++) { 
            int c = str[i + j] - '0'
            if (c < 0 || c > 9) { 
                return -1; 
            
            value = value * 10 + c; 
        
        return value; 
    
  
// Returns value of missing number 
    static int findMissingNumber(String str) { 
        // Try all lengths for first number 
        for (int m = 1; m <= MAX_DIGITS; ++m) { 
            // Get value of first number with current 
            // length/ 
            int n = getValue(str, 0, m); 
            if (n == -1) { 
                break
            
  
            // To store missing number of current length 
            int missingNo = -1; 
  
            // To indicate whether the sequence failed 
            // anywhere for current length. 
            bool fail = false
  
            // Find subsequent numbers with previous number as n 
            for (int i = m; i != str.Length; i += 1 + (int)Math.Log10(n)) { 
                // If we haven't yet found the missing number 
                // for current length. Next number is n+2. Note 
                // that we use Log10 as (n+2) may have more 
                // length than n. 
                if ((missingNo == -1) 
                        && (getValue(str, i, (int) (1 + Math.Log10(n + 2))) == n + 2)) { 
                    missingNo = n + 1; 
                    n += 2; 
                } // If next value is (n+1) 
                else if (getValue(str, i, (int) (1 + Math.Log10(n + 1))) == n + 1) { 
                    n++; 
                } else
                    fail = true
                    break
                
            
  
            if (!fail) { 
                return missingNo; 
            
        
        return -1; // not found or no missing number 
    
// Driver code 
  
    public static void Main() { 
        Console.WriteLine(findMissingNumber("99101102")); 
    
  //This code is contributed by PrinciRaj1992

chevron_right



Output:

100

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 contribute@geeksforgeeks.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.



My Personal Notes arrow_drop_up

Improved By : 29AjayKumar, princiraj1992



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.