 # Generating subarrays using recursion

## Generating subarrays using recursion

Given an array, generate all the possible subarrays of the given array using recursion.

Examples:

```Input : [1, 2, 3]
Output : , [1, 2],[1,3], , [1, 2, 3], [2, 3], 

Input : [1, 2]
Output : , [1, 2], ```

Approach: We use two pointers start and end to maintain the starting and ending point of the array and follow the steps given below:

• Stop if we have reached the end of the array
• Increment the end index if start has become greater than end
• Print the subarray from index start to end and increment the starting index

Below is the implementation of the above approach.

 `// C++ code to print all possible subarrays ` `// for given array using recursion` `#include ` `# include ` `using` `namespace` `std;` `// Recursive function to print all possible subarrays ` `// for given array` `void` `printSubArrays(vector<``int``> arr, ``int` `start, ``int` `end)` `{     ` `    ``// Stop if we have reached the end of the array    ` `    ``if` `(end == arr.size())` `        ``return``;` `      ` `    ``// Increment the end point and start from 0` `    ``else` `if` `(start > end)` `        ``printSubArrays(arr, 0, end + 1);` `          ` `    ``// Print the subarray and increment the starting point` `    ``else` `    ``{` `        ``cout << ``"["``;` `        ``for` `(``int` `i = start; i < end; i++){` `            ``cout << arr[i] << ``", "``;` `        ``}` `        ` `        ``cout << arr[end] << ``"]"` `<< endl;` `        ``printSubArrays(arr, start + 1, end);` `    ``}` `    ` `    ``return``;` `}` `int` `main()` `{` `   ``vector<``int``> arr = {1, 2, 3};` `   ``printSubArrays(arr, 0, 0);` `   ``return` `0;` `}`

Output:

```
[1, 2]

[1, 2, 3]
[2, 3]
```

Time Complexity: Last Updated on November 13, 2021 by admin

## Arrays in C/C++Arrays in C/C++

Arrays in C/C++ An array in C/C++ or be it in any programming language is

## How to assign values to variables in C++How to assign values to variables in C++

How to assign values to variables in Python and other languages This article discusses methods

## Count occurrences of a word in stringCount occurrences of a word in string

Count occurrences of a word in string You are given a string and a word

## Program to print the diamond shapeProgram to print the diamond shape

Program to print the diamond shape Given a number n, write a program to print

## Array of Strings in C++Array of Strings in C++

Array of Strings in C++ (All Different Ways to Create) In C and C++, a

## Program to generate all possible valid IP addresses from given stringProgram to generate all possible valid IP addresses from given string

Program to generate all possible valid IP addresses from given string Given a string containing

## Program to print all the numbers divisible by 3 and 5 for a given numberProgram to print all the numbers divisible by 3 and 5 for a given number

Program to print all the numbers divisible by 3 and 5 for a given number

## Program to calculate Electricity BillProgram to calculate Electricity Bill

Program to calculate Electricity Bill Given an integer U denoting the amount of KWh units of electricity