# Print all Subarray of Given Array

Print all Subarray of Given Array” is an important problem to understand the concept of subarrays of arrays. Here, we are given an array of size ‘n’ and our task is to print all subarray of the given array.

Example (Print all Subarray of Given Array):

```INPUT:
Arr = {11, 12, 13, 14}
OUTPUT:
11
11 12
11 12 13
11 12 13 14
12
12 13
12 13 14
13
13 14
14```

The steps required to print all subarray of given array are as follows:

1. Set count = 0.
2. Three nested loops will be used.
3. The outer loop will be used to take the first element of the subarray.
4. The middle loop will be used to take the last element of the subarray.
5. The inner loop will be used to print the subarray from starting element (from outer loop) to ending element (from middle loop). Increment count by 1.

The time complexity of this solution is O(n3).

#### C++ Program to print all subarray of given array is as follows:

```/* Program to print all the subarray of given array */
#include<bits/stdc++.h>
using namespace std;
int main()
{
/* Scan array size */
int n;
cout<<"Enter array size: ";
cin>>n;

/* Scan array elements */
int arr[n];
cout<<"\nEnter array elements:";
for(int i = 0; i < n; i++)
cin>>arr[i];

/* Printing the subarray */
cout<<"All the subarray are:\n";
/* Outer loop for first element of subarray */
for(int i = 0; i < n; i++)
{
/* Outer loop for first element of subarray */
for(int j = i; j < n; j++)
{
/* Printing all the subarray */
for(int k = i; k <= j; k++)
{
cout<<arr[k]<<" ";
}
cout<<"\n";
}
}
}

```
```OUTPUT:
Enter array size: 4
Enter array elements:
11 12 13 14
All the subarray are:
11
11 12
11 12 13
11 12 13 14
12
12 13
12 13 14
13
13 14
14```

Related Posts: