Anchit.Koul
BAN USERpublic static int MissingNumber(this string input)
{
int i = 0;
int groupFirst = 1;
try
{
while (i < input.Length)
{
var firstNum = GetNumber(input, i, groupFirst);
int groupSecond = GetOrder(firstNum + 1);
var secondNum = GetNumber(input, i + groupFirst, groupSecond);
if (firstNum < secondNum && (firstNum + 1 == secondNum || firstNum + 2 == secondNum))
{
i += groupFirst;
if (firstNum + 2 == secondNum)
{
return firstNum + 1;
}
groupFirst = groupSecond;
}
else
{
var temp = GetNumber(input, i + groupFirst, groupSecond + 1);
if (firstNum < temp && firstNum + 2 == temp)
{
return firstNum + 1;
}
i = 0;
groupFirst++;
}
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return i;
}
private static int GetOrder(int number)
{
int order = 0;
while (number > 0)
{
number = number / 10;
order++;
}
return order;
}
private static int GetNumber(string input, int index, int group)
{
double value = 0;
for (int i = group; i > 0; i--)
{
value += Math.Pow(10, i - 1) * (input[index++] - '0');
}
return Convert.ToInt32(value);
}
- Anchit.Koul December 18, 2013
Idea to do it inplace is to swap the dupes to the end of the array and maintain a variable that tells you end of the unique values , result will be char [] from 0 to end. See code below :-
Note : string builder is just a syntactical sugar , we already have result in input array from 0 to end
}
- Anchit.Koul December 22, 2013