Subarray/Substring vs Subsequence and Programs to Generate them
Subarray/Substring
A subarray is a contiguous part of array. An array that is inside another array. For example, consider the array [1, 2, 3, 4], There are 10 non-empty sub-arrays. The subarrays are (1), (2), (3), (4), (1,2), (2,3), (3,4), (1,2,3), (2,3,4) and (1,2,3,4). In general, for an array/string of size n, there are n*(n+1)/2 non-empty subarrays/substrings.
How to generate all subarrays?
We can run two nested loops, the outer loop picks starting element and inner loop considers all elements on right of the picked elements as ending element of subarray.
C++
#include<bits/stdc++.h>
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\n" ;
subArray(arr, n);
return 0;
}
|
Java
class Test
{
static int arr[] = new int []{ 1 , 2 , 3 , 4 };
static void subArray( int n)
{
for ( int i= 0 ; i <n; i++)
{
for ( int j=i; j<n; j++)
{
for ( int k=i; k<=j; k++)
System.out.print(arr[k]+ " " );
}
}
}
public static void main(String[] args)
{
System.out.println( "All Non-empty Subarrays" );
subArray(arr.length);
}
}
|
Python3
def subArray(arr, n):
for i in range ( 0 ,n):
for j in range (i,n):
for k in range (i,j + 1 ):
print (arr[k],end = " " )
print ( "\n" ,end = "")
arr = [ 1 , 2 , 3 , 4 ]
n = len (arr)
print ( "All Non-empty Subarrays" )
subArray(arr, n);
|
C#
using System;
class GFG
{
static int []arr = new int []{1, 2, 3, 4};
static void subArray( int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = i; j < n; j++)
{
for ( int k = i; k <= j; k++)
Console.Write(arr[k]+ " " );
Console.WriteLine( "" );
}
}
}
public static void Main()
{
Console.WriteLine( "All Non-empty Subarrays" );
subArray(arr.Length);
}
}
|
Javascript
let getSubArr = (arr) => {
let arr1 = [];
for (let i=0;i<arr.length;i++) {
let subArr = [];
for (let j=i;j<arr.length;j++) {
subArr.push(arr.slice(i,j+1));
}
arr1.push(subArr);
}
return arr1;
}
let arr = [1,2,3,4];
let arr1 = getSubArr(arr);
console.log(arr1);
|
PHP
<?php
function subArray( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = $i ; $j < $n ; $j ++)
{
for ( $k = $i ; $k <= $j ; $k ++)
echo $arr [ $k ] , " " ;
echo "\n" ;
}
}
}
$arr = array (1, 2, 3, 4);
$n = sizeof( $arr );
echo "All Non-empty Subarrays\n" ;
subArray( $arr , $n );
?>
|
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
Time Complexity: 0(n^3)
Space Complexity: 0(1)
Subsequence
A subsequence is a sequence that can be derived from another sequence by removing zero or more elements, without changing the order of the remaining elements.
For the same example, there are 15 sub-sequences. They are (1), (2), (3), (4), (1,2), (1,3),(1,4), (2,3), (2,4), (3,4), (1,2,3), (1,2,4), (1,3,4), (2,3,4), (1,2,3,4). More generally, we can say that for a sequence of size n, we can have (2n-1) non-empty sub-sequences in total.
A string example to differentiate: Consider strings “geeksforgeeks” and “gks”. “gks” is a subsequence of “geeksforgeeks” but not a substring. “geeks” is both a subsequence and subarray. Every subarray is a subsequence. More specifically, Subsequence is a generalization of substring.
A subarray or substring will always be contiguous, but a subsequence need not be contiguous. That is, subsequences are not required to occupy consecutive positions within the original sequences. But we can say that both contiguous subsequence and subarray are the same.
How to generate all Subsequences?
We can use algorithm to generate power set for generation of all subsequences.
C++
#include<bits/stdc++.h>
using namespace std;
void printSubsequences( int arr[], int n)
{
unsigned int opsize = pow (2, n);
for ( int counter = 1; counter < opsize; counter++)
{
for ( int j = 0; j < n; j++)
{
if (counter & (1<<j))
cout << arr[j] << " " ;
}
cout << endl;
}
}
int main()
{
int arr[] = {1, 2, 3, 4};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << "All Non-empty Subsequences\n" ;
printSubsequences(arr, n);
return 0;
}
|
Java
import java.math.BigInteger;
class Test
{
static int arr[] = new int []{ 1 , 2 , 3 , 4 };
static void printSubsequences( int n)
{
int opsize = ( int )Math.pow( 2 , n);
for ( int counter = 1 ; counter < opsize; counter++)
{
for ( int j = 0 ; j < n; j++)
{
if (BigInteger.valueOf(counter).testBit(j))
System.out.print(arr[j]+ " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
System.out.println( "All Non-empty Subsequences" );
printSubsequences(arr.length);
}
}
|
Python3
import math
def printSubsequences(arr, n) :
opsize = math. pow ( 2 , n)
for counter in range ( 1 , ( int )(opsize)) :
for j in range ( 0 , n) :
if (counter & ( 1 <<j)) :
print ( arr[j], end = " " )
print ()
arr = [ 1 , 2 , 3 , 4 ]
n = len (arr)
print ( "All Non-empty Subsequences" )
printSubsequences(arr, n)
|
C#
using System;
class GFG{
static void printSubsequences( int [] arr, int n)
{
int opsize = ( int )Math.Pow(2, n);
for ( int counter = 1; counter < opsize; counter++)
{
for ( int j = 0; j < n; j++)
{
if ((counter & (1 << j)) != 0)
Console.Write(arr[j] + " " );
}
Console.WriteLine();
}
}
static void Main()
{
int [] arr = { 1, 2, 3, 4 };
int n = arr.Length;
Console.WriteLine( "All Non-empty Subsequences" );
printSubsequences(arr, n);
}
}
|
Javascript
<script>
function printSubsequences(arr, n)
{
let opsize = parseInt(Math.pow(2, n), 10);
for (let counter = 1; counter < opsize; counter++)
{
for (let j = 0; j < n; j++)
{
if ((counter & (1 << j)) != 0)
document.write(arr[j] + " " );
}
document.write( "</br>" );
}
}
let arr = [ 1, 2, 3, 4 ];
let n = arr.length;
document.write( "All Non-empty Subsequences" + "</br>" );
printSubsequences(arr, n);
</script>
|
PHP
<?php
function printSubsequences( $arr , $n )
{
$opsize = pow(2, $n );
for ( $counter = 1;
$counter < $opsize ;
$counter ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
if ( $counter & (1 << $j ))
echo $arr [ $j ], " " ;
}
echo "\n" ;
}
}
$arr = array (1, 2, 3, 4);
$n = sizeof( $arr );
echo "All Non-empty Subsequences\n" ;
printSubsequences( $arr , $n );
?>
|
Output:
All Non-empty Subsequences
1
2
1 2
3
1 3
2 3
1 2 3
4
1 4
2 4
1 2 4
3 4
1 3 4
2 3 4
1 2 3 4
Time Complexity: 0(n*(2^n))
Space Complexity: 0(1)
Last Updated :
28 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...