Given an array, write a recursive program to print all the subarrays. See the example below –

**Example: **

Input [] = {1, 2, 3, 4} Output: [ 1 ] [ 1 2 ] [ 1 2 3 ] [ 1 2 3 4 ] [ 2 ] [ 2 3 ] [ 2 3 4 ] [ 3 ] [ 3 4 ] [ 4 ] Input [] = {4, 6, 8} Output: [ 4 ] [ 4 6 ] [ 4 6 8 ] [ 6 ] [ 6 8 ] [ 8 ]

**Approach: Recursion**

Please click here to read about a non-recursive approach of this problem.- Print all subarrays of a given array

Let’s take an example of array {1, 2, 3, 4}. If we observe the subarrays starting from 1 are [ 1 ] [ 1 2 ] [ 1 2 3 ] [ 1 2 3 4 ]. So we fix the starting index to 0 and subarray = [1] and iterate till end and during iteration we will keep appending the elements to the subarray and print it so we will print [ 1 ] [ 1 2 ] [ 1 2 3 ] [ 1 2 3 4 ]. Now move the starting index to 1 and subarray = [2] and repeat the same process during iteration and print [ 2 ] [ 2 3 ] [ 2 3 4 ]. So basically fix the index and print all the subarrays which starts from the fixed index and make a recursive call with index+1. See the code below for more understanding.

**Complete Code:**

**Output:**

[ 4 ] [ 4 6 ] [ 4 6 8 ] [ 6 ] [ 6 8 ] [ 8 ]