Given an array, generate all the possible subarrays of the given array using recursion.
Examples:
Input : [1, 2, 3]
Output : [1], [1, 2], [2], [1, 2, 3], [2, 3], [3]
Input : [1, 2]
Output : [1], [1, 2], [2]
We have discussed iterative program to generate all subarrays. In this post, recursive is discussed.
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++
#include <bits/stdc++.h>
using namespace std;
void printSubArrays(vector< int > arr, int start, int end)
{
if (end == arr.size())
return ;
else if (start > end)
printSubArrays(arr, 0, end + 1);
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;
}
|
C
#include <stdio.h>
void printSubArrays( int arr[], int start, int end, int size)
{
if (end == size)
return ;
else if (start > end)
printSubArrays(arr, 0, end + 1, size);
else {
printf ( "[" );
for ( int i = start; i < end; i++)
printf ( "%d, " , arr[i]);
printf ( "%d]\n" , arr[end]);
printSubArrays(arr, start + 1, end, size);
}
return ;
}
int main()
{
int arr[] = { 1, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
printSubArrays(arr, 0, 0, n);
return 0;
}
|
Java
class solution {
static void printSubArrays( int [] arr, int start, int end)
{
if (end == arr.length)
return ;
else if (start > end)
printSubArrays(arr, 0 , end + 1 );
else {
System.out.print( "[" );
for ( int i = start; i < end; i++)
System.out.print(arr[i] + ", " );
System.out.println(arr[end] + "]" );
printSubArrays(arr, start + 1 , end);
}
return ;
}
public static void main(String args[])
{
int [] arr = { 1 , 2 , 3 };
printSubArrays(arr, 0 , 0 );
}
}
|
Python3
def printSubArrays(arr, start, end):
if end = = len (arr):
return
elif start > end:
return printSubArrays(arr, 0 , end + 1 )
else :
print (arr[start:end + 1 ])
return printSubArrays(arr, start + 1 , end)
arr = [ 1 , 2 , 3 ]
printSubArrays(arr, 0 , 0 )
|
C#
using System;
class GFG
{
static void printSubArrays( int []arr,
int start, int end)
{
if (end == arr.Length)
return ;
else if (start > end)
printSubArrays(arr, 0, end + 1);
else
{
Console.Write( "[" );
for ( int i = start; i < end; i++)
{
Console.Write(arr[i]+ ", " );
}
Console.WriteLine(arr[end]+ "]" );
printSubArrays(arr, start + 1, end);
}
return ;
}
public static void Main(String []args)
{
int []arr = {1, 2, 3};
printSubArrays(arr, 0, 0);
}
}
|
PHP
<?php
function printSubArrays( $arr , $start , $end )
{
if ( $end == count ( $arr ))
return ;
else if ( $start > $end )
return printSubArrays( $arr , 0,
$end + 1);
else
{
echo "[" ;
for ( $i = $start ; $i < $end + 1; $i ++)
{
echo $arr [ $i ];
if ( $i != $end )
echo ", " ;
}
echo "]\n" ;
return printSubArrays( $arr , $start + 1,
$end );
}
}
$arr = array (1, 2, 3);
printSubArrays( $arr , 0, 0);
?>
|
Javascript
<script>
function printSubArrays(arr, start, end)
{
if (end == arr.length)
return ;
else if (start > end)
printSubArrays(arr, 0, end + 1);
else
{
document.write( "[" );
for ( var i = start; i < end; i++)
{
document.write( arr[i] + ", " );
}
document.write(arr[end] + "]<br>" );
printSubArrays(arr, start + 1, end);
}
return ;
}
var arr = [ 1, 2, 3 ];
printSubArrays(arr, 0, 0);
</script>
|
Output:
[1]
[1, 2]
[2]
[1, 2, 3]
[2, 3]
[3]
Time Complexity: O(2n)
Auxiliary Space: O(2n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!