(solved)C++ program to print all the contiguous (continuous) subsequences of an array

Write a program to print all the subsequences of an int array. The subsequences must be contiguous. For example, the subsequences of {4, 0, -7} would be: {4}, {4, 0}, {4, 0, -7}, {0}, {0, -7} and {-7}!
(Rev. 19-Mar-2024)

Categories | About |     |  

Parveen,

Table of Contents (top down ↓)

I give various methods of solving this problem

Explanation and Notes

Suppose we have been given an array of certain number of elements. And we have to print subsequences from a starting index to an ending index.

We will use two variables for the two markers: the first will be for the start of a subsequence, and the second for the end.

In the programs below, the variable i has been used as the first or starting marker, and the variable j as the ending marker.

The variable i increments from 0 to end of the array.

To understand the operation consider that i is currently placed at, say, index 2. The variable j will start incrementing from index 2. So we will have the values of i and j like so: (i = 2, j = 2), (i = 2, j = 3), (i = 2, j = 4), and so on. Thus, i and j represent the bounds of various subsequences starting at index 2.

A for-loop of k prints the array between i and j!

Please watch the video solution given below for more detail.

Method 1
(using vectors in C++)

Following program uses a vector to hold the elements of the array. If you are not conversant with vectors, then refer to Method 2 next.

// program to print all subsequences 
// of an array 
#include <iostream>

#include <vector>

using namespace std;

int main()
{

  std::vector<int> vec = { 4, 0, -7 };

  // start marker 
  for (unsigned i = 0; i < vec.size(); i++)
  {

    // end marker 
    for (unsigned j = i; j < vec.size(); j++)
    {

      // print from start to end inclusive 
      for (unsigned k = i; k <= j; k++)
      {

        cout << vec[k] << "; ";

      }

      cout << endl;

    }

  }

  return 0;

}

Method 2
(using arrays in C++)

This program is exactly same as the one given in method 1 above, except that it uses a char array.

// program to print all subsequences 
// of an array 
#include <iostream>

using namespace std;

int main()
{

  int arr[] = { 5, -1, 0, 8, 7, -2 };

  // number of elements in the array 
  const int SIZE = sizeof(arr) / sizeof(*arr);

  // start marker 
  for (int i = 0; i < SIZE; i++)
  {

    // end marker 
    for (int j = i; j < SIZE; j++)
    {

      // print the array from i to j 
      for (int k = i; k <= j; k++)
      {

        cout << arr[k] << "; ";

      }

      cout << endl;

    }

  }

  return 0;

}

Comments and Discussion

Leave your comments on the youtube comments box.

Method 3
(not recommended, but works)

This method is neither more efficient, nor systematic. But I have included here because this was written during one of my attempts. I didn't want to throw it away.

// program to print all subsequencies 
// of an array 
#include <iostream>

using namespace std;

int main()
{

  int arr[] = { 5, -1, 0, 8, 7, -2 };

  // number of elements in the array 
  const int SIZE = sizeof(arr) / sizeof(*arr);

  for (int i = 0; i < SIZE; i++)
  {

    for (int j = 1; j <= (SIZE - i); j++)
    {

      for (int k = 0; k < j; k++)
      {

        cout << arr[i + k] << "; ";

      }

      cout << endl;

    }

  }

  return 0;

}


This Blog Post/Article "(solved)C++ program to print all the contiguous (continuous) subsequences of an array" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.