
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all sequences of given length in C++
In this problem, we are given two integer values, k, and n. And we have to print all the sequences of length k from numbers from 1 to n in sorted order.
Let’s take an example to understand the topic −
Input:k = 2 ; n = 3 Output: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
So in this problem, we have to print the sequence as stated above.
A simple way to solve this problem is by incrementing integers of the sequence till they get to the max value i.e. n. The following is a detailed description of the solution.
Algorithm
1) Create an array of size k with all values = 1 i.e. {1, 1, ..ktimes}. 2) Repeat step 3 and 4 till the array becomes {n, n, …, n}. 3) Print the array. 4) Increment the value such that the elements of the array become the next value. For example, {1, 1, 1} incremented to {1, 1, 2} and {1, 3, 3} incremented to {2, 1, 1}. For this we need to check the kth element of the array, if it’s equal to n become update, then check k-1 element in the sequence and so on for the same condition.
Example
The following program will make the concept more clear to you.
#include<iostream> using namespace std; void printSequence(int arr[], int size){ for(int i = 0; i < size; i++) cout<<arr[i]<<"\t"; cout<<endl; return; } int nextElement(int arr[], int k, int n){ int s = k - 1; while (arr[s] == n) s--; if (s < 0) return 0; arr[s] = arr[s] + 1; for(int i = s + 1; i < k; i++) arr[i] = 1; return 1; } void generateSequence(int n, int k){ int *arr = new int[k]; for(int i = 0; i < k; i++) arr[i] = 1; while(1){ printSequence(arr, k); if(nextElement(arr, k, n) == 0) break; } return; } int main(){ int n = 3; int k = 2; cout<<"The sequence is :\n"; generateSequence(n, k); return 0; }
Output
The sequence is −
1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
This method is easy to understand but can be made better and more efficient.
This method uses recursion and an extra index to check for sequence offset (value after which the sequence will be flipped). The function will be called recursively and will not update the terms till index. And recure the function for next terms after index.
Example
#include<iostream> using namespace std; void printSequence (int arr[], int size){ for (int i = 0; i < size; i++) cout << arr[i] << "\t"; cout << endl; return; } void generateSequence (int arr[], int n, int k, int index){ int i; if (k == 0){ printSequence (arr, index); } if (k > 0){ for (i = 1; i <= n; ++i){ arr[index] = i; generateSequence (arr, n, k - 1, index + 1); } } } int main (){ int n = 3; int k = 2; int *arr = new int[k]; cout<<"The sequence is:\n"; generateSequence (arr, n, k, 0); return 0; }
Output
The sequence is −
1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
- Related Articles
- Print all increasing sequences of length k from first n natural numbers in C++
- Print all longest common sub-sequences in lexicographical order in C++
- Print all interleavings of given two strings in C++
- Print all sequences starting with n and consecutive difference limited to k in C++
- Print all pairs with given sum in C++
- Print all triplets with given sum in C++
- Print all subsets of given size of a set in C++
- Print all Good numbers in given range in C++
- Print all distinct circular strings of length M in lexicographical order in C++
- Find sum of sum of all sub-sequences in C++
- Print All Distinct Elements of a given integer array in C++
- Program to print all substrings of a given string in C++
- C Program to print all permutations of a given string
- Print all pairs of anagrams in a given array of strings in C++
- C++ Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
