Open In App

Generating All Subarrays

Last Updated : 07 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[], the task is to generate all the possible subarrays of the given array.

Examples: 

Input: arr[] = [1, 2, 3]
Output: [ [1], [1, 2], [2], [1, 2, 3], [2, 3], [3] ]

Input: arr[] = [1, 2]
Output: [ [1], [1, 2], [2] ]

Iterative Approach

To generate a subarray, we need a starting index from the original array. For choosing the starting index, we can run a loop from [0 to n-1] and consider each i as the starting index. For each starting index i, we can select an ending index from the range [i to n-1]. A nested loop from [i to n-1] will help in selecting the ending index. Once we have the starting and ending indices, we need an innermost loop to print the elements in this subarray.

  • Outermost Loop: Picks starting index of current subarray
  • Middle Loop: Picks ending index of current subarray
  • Innermost Loop: Prints the subarray from the starting index to the ending index
C++
#include <iostream>
#include <vector>
using namespace std;

// Prints all subarrays in arr[0..n-1]
void subArray(vector<int> &arr)
{
    int n = arr.size();

    // Pick starting point
    for (int i = 0; i < n; i++)
    {
        // Pick ending poin
        for (int j = i; j < n; j++)
        {
            // Print subarray between current starting
            // and ending points
            for (int k = i; k <= j; k++)
                cout << arr[k] << " ";
            cout << endl;
        }
    }
}

int main()
{
    vector<int> arr = {1, 2, 3, 4};
    cout << "All Non-empty Subarrays\n";
    subArray(arr);
    return 0;
}
C Java Python C# JavaScript

Output
All Non-empty Subarrays
1 
1 2 
1 2 3 
1 2 3 4 
2 
2 3 
2 3 4 
3 
3 4 
4 

Recursive 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++
// C++ code to print all possible subarrays for given array
// using recursion
#include <iostream>
#include <vector>
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 reset the start to 0
    else if (start > end)
        printSubArrays(arr, 0, end + 1);

    // Print the subarray and increment the starting point
    else {
        for (int i = start; i <= end; i++)  
            cout << arr[i] << " ";
        cout << endl;
        printSubArrays(arr, start + 1, end);  
    }
}

int main() {
    vector<int> arr = {1, 2, 3};
    printSubArrays(arr, 0, 0);
    return 0;
}   
C Java Python C# JavaScript

Output
1 
1 2 
2 
1 2 3 
2 3 
3 




Next Article

Similar Reads