0

I was wondering how could you write a recursive method that accepts an integer parameter (n) and writes the following sequence: n, n-1, n-2,n-3,..., 0, ... -(n-3), -(n-2), -(n-1), -n. For example: 5,4,3,2,1,0,-1,-2,-3,-4,-5

What would be the base case for this example? How will the method know when to end?

So far I have:

public static void createSequence(int n) {
	if (n== 0)
	    return;
	else{
	    System.out.println(n);
	    createSequence(n-1);
      }
  }

This only creates a sequence of positive integers, how can I fix this code?

2
  • 1
    Hint: you need to write something after the call to createSequence(n - 1) Commented Dec 18, 2016 at 20:43
  • Is the input integer guaranteed to be non-negative? Commented Dec 18, 2016 at 20:44

7 Answers 7

2

You just need to write -n after the recursive call:

public static void createSequence(int n) {
    if (n == 0) {
        System.out.println(n);
        return;
    }
    else { 
        System.out.println(n);
        createSequence(n-1);
        System.out.println(-n);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Nice solution but it's not tail recursive
AFAIK, there's nothing in the JVM to optimize tail-recursion, so that doesn't really matter. I wouldn't implement this algorithm recursively anyway, other than to learn what you can do with recursion.
@Xephi: There's nothing in the question asking for tail-recursion.
@greybeard sure ! That was just to give an extra information, not a real critic, i've still upvote it :)
You can remove the else there. Make it simpler.
1

Looks like I'm late to the party, but here's mine:

public static void createSequence(int n){
    System.out.println(n);
    if(n==0) return;
    createSequence(n-Integer.signum(n));
    System.out.println(-n);
}

Works with positive and negative input.

Comments

0

The easiest, I think, would be to write an auxiliary recursive method:

public static void createSequence(int n) {
    writeSequence(n, -n);
}
private static void writeSequence(int current, int limit) {
    if (current >= limit) {
        System.out.println(current);
        writeSequence(current - 1, limit);
    }
}

Comments

0

In such cases, you usually use a helper method for the recursive call:

public static void createSequence(int n) {
    createSequenceHelper(n, -n); // be sure that 'n' is positive here
}

private static void createSequenceHelper(int n, int limit) {
    if (n >= limit) {
        System.out.println(n);
        createSequenceHelper(n - 1, limit);
    }
}

Comments

0

You can pass original number as a second parameter to do a check :

public static void createSequence(int n, int limit) {
    if (n < limit)
        return;
    else{
        System.out.println(n);
        createSequence(n-1, limit);
    }
}

Also by using : createSequence(5, -5);, it will print:

5
4
3
2
1
0
-1
-2
-3
-4
-5

Comments

0

Fixed !

public static void createSequence(int n) {
    if (n== 0){
        System.out.println(0);
        return;
    }else{
        System.out.println(n);
        createSequence(n-1);
        System.out.println(n*-1);
      }
  }

Comments

0

The best and easy way to solve your issue is the below code. Here start should be initialized to n before calling this recursive function. (start=n;)

public static void createSequence(int n, int start) {
if (start + n == 0)
    return;
else{
    System.out.println(n);
    createSequence(n-1, start);
  } 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.