0

the method below works fine, but I can't find a way to print the reversed array using same method. I tried for-each loop but it print three arrays not just one. for example if the inputs:

size 5, start=1, end=5, arr[] ={1,2,3,4,5}

output:

543215432154321

correct output should be

54321

here is the code:

static void reverse(int[] arr, int start, int end) {
    if (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        reverse(arr, start + 1, end - 1);       
    }   

    for (int pr : arr) {
        System.out.print(pr);
    }
}
5
  • 1
    Shouldn't your initial call be start = 0 and end = 4 since arrays index starting at zero? And your function prints the entire array on every call, so you are getting it output multiple times. Commented Oct 19, 2015 at 16:41
  • return the array as result and then in the calling method print the content in for loop Commented Oct 19, 2015 at 16:43
  • Because it executed n-times and prints the array n-times. Commented Oct 19, 2015 at 16:49
  • I figured that now, thank you. Commented Oct 19, 2015 at 16:50
  • 1
    If it's all working for you now, you should accept an answer. Commented Oct 19, 2015 at 17:01

3 Answers 3

7

You are very very close.

The only mistake you are making is printing the result from inside the reverse method - don't forget it's recursive so you'll get intermediate results printed.

Instead, call in first from another method, then print it out:

public static void main(String[] args) {
    int[] arr = {1,2,3,4,5,6,7,8,9,0};
    reverse(arr,0,arr.length-1);
    for (int pr : arr) {
        System.out.print(pr);
    }
}

static void reverse(int[] arr, int start, int end) {
    if (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        reverse(arr, start + 1, end - 1);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

The second mistake of the OP's which you corrected but didn't mention is indexing the array originally from 0 to length-1 instead of 1 to length. :)
1

You are printing out the array everytime it calls 'reverse' method . I have modified a bit of your code. And its working fine.

static void reverse(int[] arr, int start, int end) {
    if (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
    reverse(arr, start + 1, end - 1);       
    } 
    else {
     for (int pr : arr) {
        System.out.print(pr);
    }

Since size of your array is 5, you should be doing something like this

reverse (myArray,0,4);

Comments

0
public static void main(String[] args) {

 //assume u have create an array;
    System.out.print("this array reversely equal to : ");
    ArrayReverse(array, 0);
}

public static void ArrayReverse(int[] a, int index) {

    if (index > a.length - 1) {
        return;
    }
    ArrayReverse(a, index + 1);
    System.out.print(a[index] + " ");

}

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.