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 the subarray of the given array.

Example:

INPUT:
Arr[4] = {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 the given array are as follows:

to print all subarray of the given array

  1. Set count = 0.
  2. Three nested loops will be taken.
  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(n^3).

C++ Program to print all subarray of the 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:

  1. Program to print kth smallest and kth largest element of an Array.
  2. Program to find equilibrium index of an Array.
  3. Program to find majority element of an Array.
  4. Program to find mean of the Array.
  5. Program to sort an Array of 0s and 1s.
  6. Program to Reverse an Array.
  7. Program to count number of odd numbers and even numbers in an array.
  8. Program to find absolute difference between sum of odd index elements and even index elements.
  9. Program to find smallest and largest element of the array.
  10. Program to count occurrences of a particular element in an array.
  11. Program to search an element in an Array (Linear Search).
  12. Program to Rotate Array Elements by ‘D’ positions.
  13. Program to find most frequent element in an array.
  14. Program to find pair in an array with given sum.
  15. Program to find first repeating element of an array.
  16. Program to merge two sorted arrays.
  17. Program to find missing number in an array.
  18. Program to sort if array is sorted.
  19. Program to print Alternate Elements of an Array.
  20. Program to swap kth element from beginning to kth element from end in an Array.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *