Subarray and Subsequence

Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

Generate all possible non-empty subarrays.

A subarray is commonly defined as a part or section of an array which the contiguousness is based on the index. Subarray is inside another array.
For an array of size n, there will be n*(n+1)/2 non-empty subarrays.


Input array [1, 2, 3, 4]


All non-empty subarrays are

 1, 2
 1, 2, 3
 1, 2, 3, 4
 2, 3
 2, 3, 4
 3, 4

Time Complexity : O(n^3)


Idea : Run two nested loops picking the start point and endpoint, and print all the elements.

Step 1 : Create a function which takes the input array and prints all the non-empty subarrays.

Step 2 : In first loop pick starting point (i) from 0 to n

Step 3 : pick ending point (j) from i to n

Step 4 : print elements between i and j.

Step 5 : call the function on given array to print all the possible non-empty subarrays.

Algorithm working

C++ Program

#include <bits/stdc++.h>
using namespace std;
// Prints all subarrays in arr[0..n-1]
void PrintAllSubarrays(int arr[], int n)
    //starting point
    for (int i=0; i <n; i++)
        //ending point
        for (int j=i; j<n; j++)
            //Printing elements between the start and end points
            for (int k=i; k<=j; k++)
//Main function
int main()
    int arr[] = {1, 2, 3, 4};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "All possible non-empty subarrays\n";
    PrintAllSubarrays(arr, n);
    return 0;
Try It


Generate all possible non-empty sub-sequences.

A subsequence is a part of an array which is sequence that is derived from another sequence by deleting some elements without changing the order.

For an array of size n, there will be 2n-1 non-empty sub-sequences possible.


Input array : [1,2,3,4]

Output : All non-empty sub-sequences are

 1, 2
 1, 3
 1, 4
 2, 3
 2, 4,
 3, 4
 1, 2, 3
 1, 3, 4
 1, 2, 4
 2, 3, 4
1, 2, 3, 4

Time Complexity : O(n * 2n)


Step 1 : Create a function to print all the possible subarrays.

Step 2 : In the function,

  1. Store the total number of possible sub-sequences in size.
  2. Run a loop from 0 to size.
  3. Loop from 0 to n and if ith bit in counter is set, print ith element for this subsequnce.
  4. Start a new subsequence in new line.

Step 3 : call this function on given input array to print all the possible sub-sequences.

Algorithm working

C++ Program

#include <bits/stdc++.h>
using namespace std;

//using counter to print all possible sub-sequences
void PrintAllSubsequences(int array[], int N)
    //total number of possible non-empty sub-sequences
    unsigned int set_size = pow(2, N) - 1 ;
    for (int i = 1; i < set_size; i++)
        //printing subsequence
        for (int j = 0; j <= N; j++)
            if(i & (1<<j))
//Main function
int main()
    int array[] = {1, 2, 3, 4};
    int N = sizeof(array)/sizeof(array[0]);
    cout << "All non-empty sub-sequences are:\n";
    PrintAllSubsequences(array, N);
    return 0;
Try It

C++ Online Test - Check Your Rank across the World

Next > < Prev
Scroll to Top