
- 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 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 :\n"; 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 Articles
- Print all sequences of given length in C++
- Print all n-digit strictly increasing numbers in C++
- Print all sequences starting with n and consecutive difference limited to k in C++
- Building a lexicographically increasing sequence of first n natural numbers in JavaScript
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Sum of first n natural numbers in C Program
- Print all possible strings of length k that can be formed from a set of n characters in C++
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
- Sum of sum of first n natural numbers in C++
- Find the average of first N natural numbers in C++
- Sum of squares of first n natural numbers in C Program?
- Program for cube sum of first n natural numbers in C++
- Find m-th summation of first n natural numbers in C++
- Program to find sum of first n natural numbers in C++
- Average of first n even natural numbers?

Advertisements