|
| 1 | +# Reverse Array |
| 2 | + |
| 3 | +WAP to reverse an array using recursion |
| 4 | + |
| 5 | +**Example** |
| 6 | + |
| 7 | +``` |
| 8 | +input: [1, 2, 3, 4] |
| 9 | +output: [4, 3, 2, 1] |
| 10 | +``` |
| 11 | + |
| 12 | +## Solution |
| 13 | + |
| 14 | +### [JavaScript Implementation (method 1)](./reverseArray.js) |
| 15 | + |
| 16 | +```js |
| 17 | +/** |
| 18 | + * Reverse an array using recursion |
| 19 | + * Method 1 |
| 20 | + * Implemented by MadhavBahlMD |
| 21 | + * @date 18/01/2019 |
| 22 | + */ |
| 23 | + |
| 24 | +function reverseArray (arr) { |
| 25 | + if (arr.length === 1) return arr; |
| 26 | + return reverseArray(arr.slice(1, arr.length)).concat([arr[0]]); |
| 27 | +} |
| 28 | + |
| 29 | +console.log (reverseArray([1, 2, 3, 4])); |
| 30 | +``` |
| 31 | + |
| 32 | +### [JavaScript Implementation (method 2)](./reverseArray.js) |
| 33 | + |
| 34 | +```js |
| 35 | +/** |
| 36 | + * Reverse an array using recursion |
| 37 | + * Method 1 |
| 38 | + * Implemented by MadhavBahlMD |
| 39 | + * @date 18/01/2019 |
| 40 | + */ |
| 41 | + |
| 42 | +function reverseArray (arr, startIndex, endIndex) { |
| 43 | + if (startIndex >= endIndex) return arr; |
| 44 | + |
| 45 | + let temp = arr[startIndex]; |
| 46 | + arr[startIndex] = arr[endIndex]; |
| 47 | + arr[endIndex] = temp; |
| 48 | + |
| 49 | + reverseArray (arr, startIndex+1, endIndex-1); |
| 50 | +} |
| 51 | + |
| 52 | +let arr = [1, 2, 3, 4]; |
| 53 | +console.log (`Original Array: ${arr}`); |
| 54 | +reverseArray (arr, 0, arr.length-1); |
| 55 | +console.log (`Reversed Array: ${arr}`); |
| 56 | +``` |
| 57 | + |
| 58 | +### [Java Implementation](./ReverseArray.java) |
| 59 | + |
| 60 | +```java |
| 61 | +import java.util.Scanner; |
| 62 | + |
| 63 | +/** |
| 64 | + * Reverse an array using recursion |
| 65 | + * @author MadhavBahlMD |
| 66 | + * @date 18/01/2019 |
| 67 | + */ |
| 68 | + |
| 69 | +public class ReverseArray { |
| 70 | + public static void reverse (int[] arr, int startIndex, int endIndex) { |
| 71 | + if (startIndex >= endIndex) return; |
| 72 | + |
| 73 | + // Swap equidistant elements from start and end |
| 74 | + int temp = arr[startIndex]; |
| 75 | + arr[startIndex] = arr[endIndex]; |
| 76 | + arr[endIndex] = temp; |
| 77 | + |
| 78 | + reverse (arr, startIndex+1, endIndex-1); |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args) { |
| 82 | + Scanner input = new Scanner (System.in); |
| 83 | + System.out.println("/* ===== Sum of Array elements using recursion ===== */"); |
| 84 | + |
| 85 | + // Input the array |
| 86 | + System.out.print("\nEnter the number of elements in the array: "); |
| 87 | + int n = input.nextInt(); |
| 88 | + int arr[] = new int[n]; |
| 89 | + for (int i=0; i<n; i++) { |
| 90 | + System.out.print("Enter arr[" + i + "]: "); |
| 91 | + arr[i] = input.nextInt(); |
| 92 | + } |
| 93 | + |
| 94 | + // Print the original array |
| 95 | + System.out.print("Original Array: "); |
| 96 | + for (int i=0; i<arr.length; i++) |
| 97 | + System.out.print(arr[i] + " "); |
| 98 | + |
| 99 | + // Reverse the array |
| 100 | + reverse(arr, 0, arr.length-1); |
| 101 | + |
| 102 | + // Print the reversed array |
| 103 | + System.out.print("\nReversed Array: "); |
| 104 | + for (int i=0; i<arr.length; i++) |
| 105 | + System.out.print(arr[i] + " "); |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
0 commit comments