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);
}
}
start = 0andend = 4since arrays index starting at zero? And your function prints the entire array on every call, so you are getting it output multiple times.