Write a java method to reverse the elements of an array using recursion. Your method should take an array as an input parameter and return reverse the array
Question
Write a java method to reverse the elements of an array using recursion. Your method should take an array as an input parameter and return reverse the array
Solution
Sure, here is a Java method that reverses an array using recursion:
public class Main {
static void reverseArray(int arr[], int start, int end) {
int temp;
if (start >= end)
return;
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
reverseArray(arr, start+1, end-1);
}
static void printArray(int arr[], int size) {
for (int i=0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main (String[] args) {
int arr[] = {1, 2, 3, 4, 5, 6};
printArray(arr, 6);
reverseArray(arr, 0, 5);
System.out.println("Reversed array is ");
printArray(arr, 6);
}
}
Here's how it works:
-
The
reverseArraymethod takes an array and two indices (startandend) as input. Thestartindex is initially 0 (the first element) and theendindex is the last element of the array. -
If
startis greater than or equal toend, it means we've traversed half of the array and swapped all the necessary elements, so we return from the method. -
If
startis less thanend, we swap the elements at these indices. -
We then call
reverseArrayagain, but this time withstart+1andend-1. This means we're moving towards the middle of the array from both ends. -
This process continues until
startis no longer less thanend, at which point all elements have been swapped and the array is reversed. -
The
printArraymethod is used to print the array before and after reversing.
Similar Questions
How do you reverse a string in Java?There is no reverse() utility method in the String class. However, you can create a character array from the string and then iterate it from the end to the start. You can append the characters to a string builder and finally return the reversed string.
. How do you reverse a string in Java?
Develop a Java program for sorting and reversing an integer array,character array and floating point array.Use Generic method for the implementation.Finally print the sorted and reversed array.TestcaseInput5(No.of elements (common to all types of array))103020402512.413.311.210.214.6AFDSNOutput403025201014.613.312.411.210.2SNFDA
Reverse an ArrayWrite a program to reverse the elements of an array of integers.Constraints:NAExample:Sample Input:512345Sample Output:5 4 3 2 1 Explanation:First line of input--> size of array (n) -->5n lines of integers into the array-->1 2 3 4 5Output--> Reversed array --> 5 4 3 2 1Public Test Cases:# INPUT EXPECTED OUTPUT1 5123455 4 3 2 1
Which of the following methods can be used to reverse the elements of an array in Python?Optionsrevert()reverse()backward()inverse()
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.