Given two numbers X and d where X represents the number of non empty subsequences and d represents the difference. Output an array of size N having non empty subsequences equal to X where difference between maximum element and minimum element of each subsequence should be less than d.

Constraints:

1 ≤ N ≤ 500

1 ≤ X ≤ 10^{9}

**Examples:**

Input : X = 10, d = 5 Output : 5 50 7 15 6 100 "10" desired subsequences are: [5], [50], [7], [15], [6], [100], [5, 7], [5, 6], [7, 6], [5, 7, 6]. Input : X = 4, d = 10 Output : 10 100 1000 10000 "4" desired subsequences are: [10], [100], [1000], [10000].

**Approach:** It can be clearly seen that if d = 5, then array [1, 7] will have only 2 desired subsequence ([1] and [7] and not [1, 7]). So, similarly, if we add many 1s and many 7s in that array then also there will be no subsequence accepted which have 1 and 7 together. So, elements having difference more than d will always form disjoint sets. For e.g. in [1, 5, 9, 9, 12, 13], if d = 2, then there are 4 disjoint sets [1], [5], [9, 9], [12, 13].

The total number of non empty subsequences in an array of size N equals to -1. Hence, we just need to find the length of disjoint set whose total number of subsequences is just smaller than or equal to X and output the elements of that disjoint set. If it is not equal to X then the same step has to be done on reduced X to find the next disjoint set until X becomes 0. For making all sets disjoint, one way is to take first array element as 1 and then add 1s in 1st disjoint set equal to the length of this disjoint set and then add “d” to the elements in previous disjoint set to make another disjoint set and so on.

For e.g: X = 25, d = 100

1st disjoint set will have 4 elements as it has 15 subsequences. So 1^{st}disjoint set will be { 1, 1, 1, 1 }.

Now X becomes 10 and thus second disjoint set will contain only 3 elements. So 2^{nd}. disjoint will be { 101, 101, 101 } (by adding 100 in 1 to make it disjoint from 1st set). Now X becomes 3 and thus final disjoint set will contain two elements and 3^{rd}disjoint set will be { 201, 201 } (by again adding 100 in 101 to make it disjoint from both the previous sets).

Final Output will be [1, 1, 1, 1, 101, 101, 101, 201, 201].

Below is the implementation of above approach.

## C++

`// C++ Program to output an array having exactly X ` `// subsequences where difference between maximum ` `// and minimum element of each subsequence is less ` `// than d ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// This function outputs the desired array. ` `void` `printArray(` `int` `X, ` `int` `d, ` `int` `first_ele) ` `{ ` ` ` `// Iterate till all the disjoint sets are found. ` ` ` `while` `(X > 0) { ` ` ` ` ` `// count_ele the elements in one disjoint ` ` ` `// set. pow_of_two will keep all the ` ` ` `// powers of twos. ` ` ` `int` `count_ele = 0, pow_of_two = 2; ` ` ` ` ` `// Iterate to know the maximum length of ` ` ` `// disjoint set by checking whether X is ` ` ` `// greater than the total number of ` ` ` `// possible not empty sequences of that ` ` ` `// disjoint set. ` ` ` `while` `(X - pow_of_two + 1 >= 0) { ` ` ` `count_ele++; ` ` ` `pow_of_two *= 2; ` ` ` `} ` ` ` ` ` `// now deleting the total subsequences of ` ` ` `// the maximum length disjoint set from X. ` ` ` `X = X - (pow_of_two / 2) + 1; ` ` ` ` ` `// outputing the disjoint set having equal ` ` ` `// elements. ` ` ` `for` `(` `int` `j = 0; j < count_ele; j++) ` ` ` `cout << first_ele << ` `" "` `; ` ` ` ` ` `// by adding d, it makes another disjoint ` ` ` `// set of equal elements. ` ` ` `first_ele += d; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `d = 100, X = 25; ` ` ` `printArray(X, d, 1); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to output ` `// an array having exactly ` `// X subsequences where ` `// difference between maximum ` `// and minimum element of ` `// each subsequence is less ` `// than d ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// This function outputs ` `// the desired array. ` `static` `void` `printArray(` `int` `X, ` `int` `d, ` ` ` `int` `first_ele) ` `{ ` ` ` `// Iterate till all the ` ` ` `// disjoint sets are found. ` ` ` `while` `(X > ` `0` `) ` ` ` `{ ` ` ` ` ` `// count_ele the elements ` ` ` `// in one disjoint set. ` ` ` `// pow_of_two will keep ` ` ` `// all the powers of twos. ` ` ` `int` `count_ele = ` `0` `, ` ` ` `pow_of_two = ` `2` `; ` ` ` ` ` `// Iterate to know the ` ` ` `// maximum length of ` ` ` `// disjoint set by checking ` ` ` `// whether X is greater than ` ` ` `// the total number of possible ` ` ` `// not empty sequences of that ` ` ` `// disjoint set. ` ` ` `while` `(X - pow_of_two + ` `1` `>= ` `0` `) ` ` ` `{ ` ` ` `count_ele++; ` ` ` `pow_of_two *= ` `2` `; ` ` ` `} ` ` ` ` ` `// now deleting the total ` ` ` `// subsequences of the maximum ` ` ` `// length disjoint set from X. ` ` ` `X = X - (pow_of_two / ` `2` `) + ` `1` `; ` ` ` ` ` `// outputing the disjoint ` ` ` `// set having equal elements. ` ` ` `for` `(` `int` `j = ` `0` `; ` ` ` `j < count_ele; j++) ` ` ` `System.out.print(first_ele + ` `" "` `); ` ` ` ` ` `// by adding d, it makes ` ` ` `// another disjoint set ` ` ` `// of equal elements. ` ` ` `first_ele += d; ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `d = ` `100` `, X = ` `25` `; ` ` ` `printArray(X, d, ` `1` `); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 Program to output an array having ` `# exactly X subsequences where difference ` `# between maximum and minimum element of each ` `# subsequence is less than d ` ` ` `# This function outputs the desired array. ` `def` `printArray(X, d, first_ele): ` ` ` ` ` `# Iterate till all the disjoint ` ` ` `# sets are found. ` ` ` `while` `(X > ` `0` `): ` ` ` ` ` `# count_ele the elements in one ` ` ` `# disjoint set. pow_of_two will ` ` ` `# keep all the powers of twos. ` ` ` `count_ele, pow_of_two ` `=` `0` `, ` `2` ` ` ` ` `# Iterate to know the maximum length of ` ` ` `# disjoint set by checking whether X is ` ` ` `# greater than the total number of ` ` ` `# possible not empty sequences of that ` ` ` `# disjoint set. ` ` ` `while` `(X ` `-` `pow_of_two ` `+` `1` `>` `=` `0` `): ` ` ` `count_ele ` `+` `=` `1` ` ` `pow_of_two ` `*` `=` `2` ` ` ` ` `# now deleting the total subsequences of ` ` ` `# the maximum length disjoint set from X. ` ` ` `X ` `=` `X ` `-` `(pow_of_two ` `/` `2` `) ` `+` `1` ` ` ` ` `# outputing the disjoint set having ` ` ` `# equal elements. ` ` ` `for` `j ` `in` `range` `(count_ele): ` ` ` `print` `(first_ele, end ` `=` `" "` `) ` ` ` ` ` `# by adding d, it makes another ` ` ` `# disjoint set of equal elements. ` ` ` `first_ele ` `+` `=` `d ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `d, X ` `=` `100` `, ` `25` ` ` `printArray(X, d, ` `1` `) ` ` ` `# This code is contributed by PrinciRaj19992 ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to output ` `// an array having exactly ` `// X subsequences where ` `// difference between maximum ` `// and minimum element of ` `// each subsequence is less ` `// than d ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// This function outputs ` `// the desired array. ` `static` `void` `printArray(` `int` `X, ` `int` `d, ` ` ` `int` `first_ele) ` `{ ` ` ` `// Iterate till all the ` ` ` `// disjoint sets are found. ` ` ` `while` `(X > 0) ` ` ` `{ ` ` ` ` ` `// count_ele the elements ` ` ` `// in one disjoint set. ` ` ` `// pow_of_two will keep ` ` ` `// all the powers of twos. ` ` ` `int` `count_ele = 0, ` ` ` `pow_of_two = 2; ` ` ` ` ` `// Iterate to know the ` ` ` `// maximum length of ` ` ` `// disjoint set by checking ` ` ` `// whether X is greater than ` ` ` `// the total number of possible ` ` ` `// not empty sequences of that ` ` ` `// disjoint set. ` ` ` `while` `(X - pow_of_two + 1 >= 0) ` ` ` `{ ` ` ` `count_ele++; ` ` ` `pow_of_two *= 2; ` ` ` `} ` ` ` ` ` `// now deleting the total ` ` ` `// subsequences of the maximum ` ` ` `// length disjoint set from X. ` ` ` `X = X - (pow_of_two / 2) + 1; ` ` ` ` ` `// outputing the disjoint ` ` ` `// set having equal elements. ` ` ` `for` `(` `int` `j = 0; ` ` ` `j < count_ele; j++) ` ` ` `Console.Write(first_ele + ` `" "` `); ` ` ` ` ` `// by adding d, it makes ` ` ` `// another disjoint set ` ` ` `// of equal elements. ` ` ` `first_ele += d; ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `d = 100, X = 25; ` ` ` `printArray(X, d, 1); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to output an array ` `// having exactly X subsequences ` `// where difference between maximum ` `// and minimum element of each ` `// subsequence is less than d ` ` ` `// This function outputs the ` `// desired array. ` `function` `printArray(` `$X` `, ` `$d` `,` `$first_ele` `) ` `{ ` ` ` `// Iterate till all the disjoint ` ` ` `// sets are found. ` ` ` `while` `(` `$X` `> 0) ` ` ` `{ ` ` ` ` ` `// count_ele the elements in ` ` ` `// one disjoint set. pow_of_two ` ` ` `// will keep all the powers of twos. ` ` ` `$count_ele` `= 0; ` ` ` `$pow_of_two` `= 2; ` ` ` ` ` `// Iterate to know the maximum ` ` ` `// length of disjoint set by ` ` ` `// checking whether X is greater ` ` ` `// than the total number of possible ` ` ` `// not empty sequences of that ` ` ` `// disjoint set. ` ` ` `while` `(` `$X` `- ` `$pow_of_two` `+ 1 >= 0) ` ` ` `{ ` ` ` `$count_ele` `++; ` ` ` `$pow_of_two` `*= 2; ` ` ` `} ` ` ` ` ` `// now deleting the total subsequences of ` ` ` `// the maximum length disjoint set from X. ` ` ` `$X` `= ` `$X` `- (` `$pow_of_two` `/ 2) + 1; ` ` ` ` ` `// outputing the disjoint set ` ` ` `// having equal elements. ` ` ` `for` `(` `$j` `= 0; ` `$j` `< ` `$count_ele` `; ` `$j` `++) ` ` ` `echo` `$first_ele` `, ` `" "` `; ` ` ` ` ` `// by adding d, it makes another ` ` ` `// disjoint set of equal elements. ` ` ` `$first_ele` `+= ` `$d` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `$d` `= 100; ` `$X` `= 25; ` `printArray(` `$X` `, ` `$d` `, 1); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1 1 1 1 101 101 101 201 201

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count all subsequences having product less than K
- Count of subsequences having maximum distinct elements
- Construct a string that has exactly K subsequences from given string
- Count of subsequences in an array with sum less than or equal to X
- Count the number of subsequences of length k having equal LCM and HCF
- Count of all subsequences having adjacent elements with different parity
- Maximum of minimum difference of all pairs from subsequences of given size
- Find largest number smaller than N with same set of digits
- Count subsequences which contains both the maximum and minimum array element
- Product of all Subsequences of size K except the minimum and maximum Elements
- Find two equal subsequences of maximum length with at least one different index
- Number of subsequences of maximum length K containing no repeated elements
- Count of subsequences whose product is a difference of square of two integers
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Sum of all subsequences of an array
- Subsequences of size three in an array whose sum is divisible by m
- Count of possible subarrays and subsequences using given length of Array
- Maximize count of Decreasing Consecutive Subsequences from an Array
- Maximize count of Decreasing Subsequences from the given Array
- Check if an array can be split into 3 subsequences of equal sum or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.