- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Print all increasing sequences of length k from first n natural numbers in C++

In this problem, we are given two integers K and n. Our task is to print all increasing sequences of length K using first n natural numbers.

**The increasing sequence** is a sequence of numbers in which the value of the next element is greater than the previous one.

Let’s take an example to understand the problem −

Input: n = 4, K = 2 Output: 1 2 1 3 1 4 2 3 2 4 3 4

To solve this problem, we will create a k length array that stores the current sequence of the array. And for every position in the array, we will check the previous element and select the next elements that are greater than the previous one. We will one by one try to fix all values from 1 to n.

## Example

Program to illustrate the above logic −

#include<iostream> using namespace std; void printSequence(int arr[], int k) { for (int i=0; i<k; i++) cout<<arr[i]<<" "; cout<<endl; } void printKLengthSequence(int n, int k, int &len, int arr[]) { if (len == k) { printSequence(arr, k); return; } int i = (len == 0)? 1 : arr[len-1] + 1; len++; while (i<=n) { arr[len-1] = i; printKLengthSequence(n, k, len, arr); i++; } len--; } void generateSequence(int n, int k) { int arr[k]; int len = 0; printKLengthSequence(n, k, len, arr); } int main() { int k = 3, n = 4; cout<<"Sequence of length "<<k<<" generated using first "<<n<<" natural numbers :

"; generateSequence(n, k); return 0; }

## Output

A sequence of length 3 generated using first 4 natural numbers − 1 2 3 1 2 4 1 3 4 2 3 4

- Related Questions & Answers
- Building a lexicographically increasing sequence of first n natural numbers in JavaScript
- Print all sequences of given length in C++
- Print all n-digit strictly increasing numbers in C++
- Average of first n even natural numbers?
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Sum of square-sums of first n natural numbers
- Sum of all subsets of a set formed by first n natural numbers
- Sum of first n natural numbers in C Program
- Sum of sum of first n natural numbers in C++
- Print all sequences starting with n and consecutive difference limited to k in C++
- Find the average of first N natural numbers in C++
- Finding sum of first n natural numbers in PL/SQL
- Sum of squares of first n natural numbers in C Program?
- C Program for cube sum of first n natural numbers?
- C++ Program for cube sum of first n natural numbers?

Advertisements