Knowee
Questions
Features
Study Tools

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

🧐 Not the exact question you are looking for?Go ask a question

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:

  1. The reverseArray method takes an array and two indices (start and end) as input. The start index is initially 0 (the first element) and the end index is the last element of the array.

  2. If start is greater than or equal to end, it means we've traversed half of the array and swapped all the necessary elements, so we return from the method.

  3. If start is less than end, we swap the elements at these indices.

  4. We then call reverseArray again, but this time with start+1 and end-1. This means we're moving towards the middle of the array from both ends.

  5. This process continues until start is no longer less than end, at which point all elements have been swapped and the array is reversed.

  6. The printArray method is used to print the array before and after reversing.

This problem has been solved

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()

1/3

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.