Subarray / Substring vs Subsequence and Programs to Generate them | C ++ code to generate all possible subarrays / subArrays Complexity - O ( n ^ 3 ) ; Prints all subarrays in arr [ 0. . n - 1 ] ; Pick starting point ; Pick ending point ; Print subarray between current starting and ending points ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void subArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { for ( int k = i ; k <= j ; k ++ ) cout << arr [ k ] << " ▁ " ; cout << endl ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " All ▁ Non - empty ▁ Subarrays STRNEWLINE " ; subArray ( arr , n ) ; return 0 ; }