Separate the Numbers

  • + 0 comments

    C# solution.

            {
                int start = 0;
                bool f = false;
                int noOfDigits = 1;
                long firstItemOfSequence = 0;
                string string_modified = str;
                while (start <= str.Length)
                {
                    string firstAsString = str.Substring(start, noOfDigits);
                    int currentPosition = firstAsString.Length + str.IndexOf(firstAsString);
                    long firstAsNumber = long.Parse(firstAsString);
                    if (firstItemOfSequence == 0) firstItemOfSequence = firstAsNumber;
                    long secondNumber = firstAsNumber + 1;
                    str = str.Substring(currentPosition, str.Length - currentPosition);
                    if (str.IndexOf(secondNumber.ToString()) == 0)
                    {
                        start = str.IndexOf(secondNumber.ToString());
                        f = true;
                        currentPosition = secondNumber.ToString().Length + str.IndexOf(secondNumber.ToString());
                        noOfDigits = secondNumber.ToString().Length;
                    }
                    else if (firstAsString.Length >= string_modified.Length / 2) break;
                    else
                    {
                        f = false;
                        start = 0;
                        firstItemOfSequence = 0;
                        noOfDigits++;
                        str = string_modified;
                    }
                    if (currentPosition == str.Length) break;
                }
                if (f) Console.WriteLine($"YES {firstItemOfSequence}");
                else Console.WriteLine("NO");
    
            }