 Open in App
Not now

# Construct array having X subsequences with maximum difference smaller than d

• Difficulty Level : Medium
• Last Updated : 17 Aug, 2022

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 ≤ 109

Examples:

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

Input : X = 4, d = 10
Output : 10 100 1000 10000
"4" desired subsequences are: , ,
, .```

Approach: It can be clearly seen that if d = 5, then array [1, 7] will have only 2 desired subsequences ( and  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 , , [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 1st disjoint set will be { 1, 1, 1, 1 }.
Now X becomes 10 and thus second disjoint set will contain only 3 elements. So 2nd. 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 3rd 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 ``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;` `        ``// outputting 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;``}`

## 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``;` `        ``// outputting 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.`

## 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` `        ``# outputting 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`

## 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;` `        ``// outputting 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.`

## PHP

 ` 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;` `        ``// outputting 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``?>`

## Javascript

 ``

Output

`1 1 1 1 101 101 101 201 201 `

My Personal Notes arrow_drop_up