hnravichandran
BAN USERstring str("1000110002100031000510006");
for(int i=1; i< str.length(); i++)
{
int missing = findMissing(str, i);
if(missing > 0)
{
printf(" % d\n\n",missing);
break;
}
}
int findMissing(string & str, int length)
{
int missing = -1 ;
int pos = 0;
string sub;
string nextSub ;
int subNumber ;
int nextSubNumber ;
sub = str.substr(pos,length) ;
subNumber = atoi(sub.c_str()) ;
nextSub = str.substr(pos+length,length) ;
nextSubNumber = atoi(nextSub.c_str()) ;
if( (nextSubNumber - subNumber) > 2 || nextSubNumber < subNumber)
{
return missing ;
}
else
{
while (1) {
pos += length ;
if(pos + length > str.length())
{
break;
}
sub = nextSub ;
subNumber = nextSubNumber ;
nextSub = str.substr(pos,length) ;
nextSubNumber = atoi(nextSub.c_str()) ;
if(nextSubNumber - subNumber == 2)
{
missing = nextSubNumber - 1 ;
break ;
}
}
}
return missing ;
}
works as far as the string length of individual numbers are same
- hnravichandran June 03, 2014