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 the 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 the number of digits may change as we increment numbers. For example when we move to 100 from 99. To handle this situation, we find the number of digits using log base 10.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
const int MAX_DIGITS = 6;
int getValue( const string& str, int i, int m)
{
if (i + m > str.length())
return -1;
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;
}
int findMissingNumber( const string& str)
{
for ( int m=1; m<=MAX_DIGITS; ++m)
{
int n = getValue(str, 0, m);
if (n == -1)
break ;
int missingNo = -1;
bool fail = false ;
for ( int i=m; i!=str.length(); i += 1 + log10l(n))
{
if ((missingNo == -1) &&
(getValue(str, i, 1+log10l(n+2)) == n+2))
{
missingNo = n + 1;
n += 2;
}
else if (getValue(str, i, 1+log10l(n+1)) == n+1)
n++;
else
{
fail = true ;
break ;
}
}
if (!fail)
return missingNo;
}
return -1;
}
int main()
{
cout << findMissingNumber( "99101102" );
return 0;
}
|
Java
class GFG {
static final int MAX_DIGITS = 6 ;
static int getValue(String str, int i, int m) {
if (i + m > str.length()) {
return - 1 ;
}
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;
}
static int findMissingNumber(String str) {
for ( int m = 1 ; m <= MAX_DIGITS; ++m) {
int n = getValue(str, 0 , m);
if (n == - 1 ) {
break ;
}
int missingNo = - 1 ;
boolean fail = false ;
for ( int i = m; i != str.length(); i += 1 + Math.log10(n)) {
if ((missingNo == - 1 )
&& (getValue(str, i, ( int ) ( 1 + Math.log10(n + 2 ))) == n + 2 )) {
missingNo = n + 1 ;
n += 2 ;
}
else if (getValue(str, i, ( int ) ( 1 + Math.log10(n + 1 ))) == n + 1 ) {
n++;
} else {
fail = true ;
break ;
}
}
if (!fail) {
return missingNo;
}
}
return - 1 ;
}
public static void main(String[] args) {
System.out.println(findMissingNumber( "99101102" ));
}
}
|
Python3
import math
MAX_DIGITS = 6
def getValue( Str , i, m):
if (i + m > len ( Str )):
return - 1
value = 0
for j in range (m):
c = ( ord ( Str [i + j]) -
ord ( '0' ))
if (c < 0 or c > 9 ):
return - 1
value = value * 10 + c
return value
def findMissingNumber( Str ):
for m in range ( 1 , MAX_DIGITS + 1 ):
n = getValue( Str , 0 , m)
if (n = = - 1 ):
break
missingNo = - 1
fail = False
i = m
while (i ! = len ( Str )):
if ((missingNo = = - 1 ) and
(getValue( Str , i, 1 +
int (math.log10(n + 2 ))) = =
n + 2 )):
missingNo = n + 1
n + = 2
elif ((getValue( Str , i, 1 +
int (math.log10(n + 1 ))) = =
n + 1 )):
n + = 1
else :
fail = True
break
i + = 1 + int (math.log10(n))
if ( not fail):
return missingNo
return - 1
print (findMissingNumber( "99101102" ))
|
C#
using System;
public class GFG {
static readonly int MAX_DIGITS = 6;
static int getValue(String str, int i, int m) {
if (i + m > str.Length) {
return -1;
}
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;
}
static int findMissingNumber(String str) {
for ( int m = 1; m <= MAX_DIGITS; ++m) {
int n = getValue(str, 0, m);
if (n == -1) {
break ;
}
int missingNo = -1;
bool fail = false ;
for ( int i = m; i != str.Length; i += 1 + ( int )Math.Log10(n)) {
if ((missingNo == -1)
&& (getValue(str, i, ( int ) (1 + Math.Log10(n + 2))) == n + 2)) {
missingNo = n + 1;
n += 2;
}
else if (getValue(str, i, ( int ) (1 + Math.Log10(n + 1))) == n + 1) {
n++;
} else {
fail = true ;
break ;
}
}
if (!fail) {
return missingNo;
}
}
return -1;
}
public static void Main() {
Console.WriteLine(findMissingNumber( "99101102" ));
}
}
|
Javascript
<script>
let MAX_DIGITS = 6;
function getValue(str, i, m)
{
if (i + m > str.length)
{
return -1;
}
let value = 0;
for (let j = 0; j < m; j++)
{
let c = str[i + j].charCodeAt(0) -
'0' .charCodeAt(0);
if (c < 0 || c > 9)
{
return -1;
}
value = value * 10 + c;
}
return value;
}
function findMissingNumber(str)
{
for (let m = 1; m <= MAX_DIGITS; ++m)
{
let n = getValue(str, 0, m);
if (n == -1)
{
break ;
}
let missingNo = -1;
let fail = false ;
for (let i = m;
i != str.length;
i += 1 + Math.floor(Math.log10(n)))
{
if ((missingNo == -1) &&
(getValue(str, i, Math.floor(1 + Math.log10(n + 2))) == n + 2)) {
missingNo = n + 1;
n += 2;
}
else if (getValue(str, i, Math.floor(
1 + Math.log10(n + 1))) == n + 1)
{
n++;
}
else
{
fail = true ;
break ;
}
}
if (!fail)
{
return missingNo;
}
}
return -1;
}
document.write(findMissingNumber( "99101102" ));
</script>
|
Last Updated :
08 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...