I am trying to do a simple coding challenge that requires me to do the following:

You are given n, return an array ans, composed in such way:

`ans = [n, n - 5, n - 10, ... , m, m + 5, ... , n - 5, n]`, where m stands for the first non-positive integer obtained by subtractions.

Try to solve it without any loop.

Example

For n = 25, the output should be

listWithoutLoop(n) = [25, 20, 15, 10, 5, 0, 5, 10, 15, 20, 25].

I have done this code:

int[] listWithoutLoop(int n)
{
    List<int> test = new List<int>();

    if (test.Count > 2 && test[test.Count - 1] == n)
        return test.ToArray();

    if (n <= 0)
    {
        test.Add(n + 5);
        return listWithoutLoop(n + 5);
    }
    else
    {
        test.Add(n - 5);
        return listWithoutLoop(n - 5);
    }
}

But I keep getting a stack overflow when running it. Is recursion supported by c#? If so, how to prevent getting a stackoverflow exception when running it?

share|improve this question
5  
You keep re-creating test in every recursive call so test.Count > 2 is always false so you never reach the base case. – Lee Dec 14 '17 at 12:53
1  
StackOverflow in recursion is always because of the same thing: your exit condition is never reached. – Pikoh Dec 14 '17 at 12:53
    
What happens if you move List<int> test = new List<int>(); above int[] listWithoutLoop(int n)? – mjwills Dec 14 '17 at 12:54
    
For the last part : of course C# supports recursion. Did you try your code in any other language ? It should raise the same error. – Pac0 Dec 14 '17 at 12:55
3  
Make list argument of your function and pass it to every recursive call. – Evk Dec 14 '17 at 12:59
up vote -1 down vote accepted

To simplify, I splitted the function to add up and add down separately (it's always better to have simple and understandable code at all times)

    static void Main()
    {
        int n = 20;
        int interval = 5;

        List<int> list = new List<int>();
        AddDown(list, n, 0, interval);
        AddUp(list, 0, n, interval);

        int[] arrInt = list.ToArray();

    }

    static void AddDown(List<int> list, int currentNumber, int targetNumber, int interval)
    {
        if(currentNumber > targetNumber)
        {
            list.Add(currentNumber);
            AddDown(list, currentNumber - interval, targetNumber, interval);
        }
    }

    static void AddUp(List<int> list, int currentNumber, int targetNumber, int interval)
    {
        if (currentNumber <= targetNumber)
        {
            list.Add(currentNumber);
            AddUp(list, currentNumber + interval, targetNumber, interval);
        }
    }
share|improve this answer

You must define the test list above the listWithoutLoop() method.

List<int> test = new List<int>();
int[] listWithoutLoop(int n)
{
....
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.