Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6144285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:37:41+00:00 2026-05-23T18:37:41+00:00

I am trying to write a program to identify the occurrences of 3 consecutive

  • 0

I am trying to write a program to identify the occurrences of 3 consecutive integers in a given array of N numbers and replace them with the middle value by deleting the other two.
For example Input->55 99 99 100 101 101 34 35 36 5 28 7 50 50 51 52 52 24 13 14 15 5 6 7 37 31 37 38 39 36 40
Output->55 100 35 5 28 7 51 24 14 6 37 31 38 36 40

To achieve this i wrote this method which accepts array as an input and it returns the modified array.

//input 
int[] original = new int[] { 1, 3, 4, 5, 5, 6, 8} ;

            List<int> lstoriginal = new List<int>(original);
            List<int> modified = Test(lstoriginal);

//method
    public static List<int> Test(List<int> arrayInput)
        {

            for (i = 0; i < arrayInput.Count; i++)
            {
                if (i + 2 < arrayInput.Count)
                {
                    if (arrayInput[i + 2] == arrayInput[i + 1] + 1
                    && arrayInput[i + 2] == arrayInput[i] + 2)
                    {
                        arrayInput.RemoveAt(i + 2);
                        arrayInput.RemoveAt(i);
                        List<int> temp = arrayInput;
                        Test(temp);
                    }
                }
            }

            return arrayInput;


        }

Follwoing are the execution steps/result which i analyzed-

1-Initially if the test input is 1, 3, 4, 5, 5, 6, 8

2-When i=1 and it finds that 3,4,5 is in sequence it removes 3 and 5 and list becomes 1,4,5,6,8

3-Next time when i=1 then it finds 4,5,6 and it removes 4 and 6 and the new list is 1,5,8

4-i am expecting to exit from loop when i + 2 < arrayInput.Count returns false and trying to retrun the modified array immediately here the return statement gets executed but instead of return the result it again calls the Test(temp); statement few more times and then get exit. Please suggest

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:37:42+00:00Added an answer on May 23, 2026 at 6:37 pm

    You actually don’t need recursion at all. You can perform the task significantly faster by just moving i after you’re removed your sequence. Here’s a function that is much simpler and does the exact same thing. I tested it on tens of thousands of randomly generated unordered sequences.

    public static List<int> Test2(List<int> arrayInput)
    {
    
        for (int i = 0; i < arrayInput.Count - 2; i++)
        {
            if (arrayInput[i + 2] == arrayInput[i + 1] + 1
            && arrayInput[i + 2] == arrayInput[i] + 2)
            {
                arrayInput.RemoveAt(i + 2);
                arrayInput.RemoveAt(i);
                i = Math.Max(-1, i - 3); // -1 'cause i++ in loop will increment it
            }
        }
    
        return arrayInput;
    }
    

    That said, to answer your specific question, the best way to exit a recursive loop like your original is to change the signature of your recursive function to return a bool indicating whether or not it actually made any changes. When the first one returns with no changes, they all can exist, so your call to Test can be wrapped in if (!Test(...)) { return; }.

    Here’s the complete test and test data comparing your original to my modified version:

    public static void Main()
    {
        const int COUNT = 10000;
        var r = new Random();
        int matchCount = 0;
    
        var stopwatch1 = new Stopwatch();
        var stopwatch2 = new Stopwatch();
    
        for (int j = 0; j < COUNT; j++)
        {
            var list = new List<int>(100) {1};
    
            for(int k=1; k<100; k++)
            {
                switch(r.Next(5))
                {
                    case 0:
                    case 1:
                    case 2:
                        list.Add(list[k - 1] + 1);
                        break;
    
                    case 3:
                        list.Add(list[k - 1] + r.Next(2));
                        break;
    
                    case 4:
                        list.Add(list[k - 1] - r.Next(5));
                        break;
                }
            }
    
            stopwatch1.Start();
            List<int> copy1 = Test1(new List<int>(list));
            stopwatch1.Stop();
    
            stopwatch2.Start();
            List<int> copy2 = Test2(new List<int>(list));
            stopwatch2.Stop();
    
    
            string list1 = String.Join(",", copy1);
            string list2 = String.Join(",", copy2);
    
            if (list1 == list2)
            {
                if (copy1.Count == list.Count)
                {
                    Console.WriteLine("No change:" + list1);
                }
                else
                {
                    matchCount++;
                }
            }
            else
            {
                Console.WriteLine("MISMATCH:");
                Console.WriteLine("   Orig  : " + String.Join(",", list));
                Console.WriteLine("   Test1 : " + list1);
                Console.WriteLine("   Test2 : " + list2);
            }
    
        }
        Console.WriteLine("Matches: " + matchCount);
        Console.WriteLine("Elapsed 1: {0:#,##0} ms", stopwatch1.ElapsedMilliseconds);
        Console.WriteLine("Elapsed 2: {0:#,##0} ms", stopwatch2.ElapsedMilliseconds);
    }
    
    
    
    public static List<int> Test1(List<int> arrayInput)
    {
    
        for (int i = 0; i < arrayInput.Count; i++)
        {
            if (i + 2 < arrayInput.Count)
            {
                if (arrayInput[i + 2] == arrayInput[i + 1] + 1
                && arrayInput[i + 2] == arrayInput[i] + 2)
                {
                    arrayInput.RemoveAt(i + 2);
                    arrayInput.RemoveAt(i);
                    List<int> temp = arrayInput;
                    Test1(temp);
                }
            }
            else
            {      // modified part: return the array
                return arrayInput;
            }
        }
    
        return arrayInput;
    }
    
    //method
    public static List<int> Test2(List<int> arrayInput)
    {
    
        for (int i = 0; i < arrayInput.Count - 2; i++)
        {
            if (arrayInput[i + 2] == arrayInput[i + 1] + 1
            && arrayInput[i + 2] == arrayInput[i] + 2)
            {
                arrayInput.RemoveAt(i + 2);
                arrayInput.RemoveAt(i);
                i = Math.Max(-1, i - 3); // -1 'cause i++ in loop will increment it
            }
        }
    
        return arrayInput;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write program calculating average of given numbers stored in an array.
I am trying to write a program that displays the integers between 1 and
I was trying to write a program that would display the prime numbers between
I'm trying to write a program that reads 2 numbers from the user and
I'm trying to write a program that prints all numbers from 0 to 1,000
I'm trying to write a program that reads 2 numbers from the user and
I'm trying to write a program in R that when, given a vector, will
I am trying to write a program that replaces even numbers with the word
Im trying to write a program which get two 6-digit decimal numbers and show
I am trying to write a program that can replace text and replace also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.