// C++ code to print all possible subarrays for given array
// using recursion
#include <bits/stdc++.h>
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;
}
// This code is contributed by Sania Kumari Gupta
Code Example |
---|
Cpp :: how to traverse string like array in cpp |
Cpp :: C++ (ISO) |
Cpp :: unreal ensureMsgf example |
Cpp :: Jython Java Python |
Cpp :: how to run c++ on cmd |
Cpp :: left recursion program in c++ |
Cpp :: C++: Methods of code shortening in competitive programming |
Cpp :: c++ copy pointer vector to static |
Cpp :: choose endianness in cpp |
Cpp :: 2d stl array |
Cpp :: how to display score using SDL in c++ |
Cpp :: wap in c++ to understand function template |
Cpp :: MPI_File_seek |
Cpp :: C++ system("pause") |
Cpp :: onactorbeginoverlap c++ |
Cpp :: pointeur cpp |
Cpp :: executing linux scripts |
Cpp :: online c++ compiler |
Cpp :: subsequence |
Cpp :: if statement in c++ |
Cpp :: c++ for loops |
Cpp :: c++ press any key |
Cpp :: freeing array in c++ |
C :: stop redis server |
C :: buble sort c |
C :: octave dot operator |
C :: c print size_t |
C :: sigaction in c |
C :: get current used proxy windows 7 |
C :: c fractional sleep |